utils_spec.rb 834B

12345678910111213141516171819202122
  1. require 'spec_helper'
  2. describe Utils do
  3. describe "#value_at" do
  4. it "returns the value at a JSON path" do
  5. Utils.value_at({ :foo => { :bar => :baz }}.to_json, "foo.bar").should == "baz"
  6. Utils.value_at({ :foo => { :bar => { :bing => 2 } }}, "foo.bar.bing").should == 2
  7. end
  8. it "returns nil when the path cannot be followed" do
  9. Utils.value_at({ :foo => { :bar => :baz }}, "foo.bing").should be_nil
  10. end
  11. end
  12. describe "#values_at" do
  13. it "returns arrays of matching values" do
  14. Utils.values_at({ :foo => { :bar => :baz }}, "foo.bar").should == %w[baz]
  15. Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar").should == %w[baz bing]
  16. Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar").should == %w[baz bing]
  17. end
  18. end
  19. end