utils_spec.rb 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. require 'spec_helper'
  2. describe Utils do
  3. describe "#unindent" do
  4. it "unindents to the level of the greatest consistant indention" do
  5. Utils.unindent(<<-MD).should == "Hello World"
  6. Hello World
  7. MD
  8. Utils.unindent(<<-MD).should == "Hello World\nThis is\nnot indented"
  9. Hello World
  10. This is
  11. not indented
  12. MD
  13. Utils.unindent(<<-MD).should == "Hello World\n This is\n indented\nthough"
  14. Hello World
  15. This is
  16. indented
  17. though
  18. MD
  19. Utils.unindent("Hello\n I am indented").should == "Hello\n I am indented"
  20. a = " Events will have the fields you specified. Your options look like:\n\n {\n \"url\": {\n \"css\": \"#comic img\",\n \"attr\": \"src\"\n },\n \"title\": {\n \"css\": \"#comic img\",\n \"attr\": \"title\"\n }\n }\"\n"
  21. Utils.unindent(a).should == "Events will have the fields you specified. Your options look like:\n\n {\n \"url\": {\n\"css\": \"#comic img\",\n\"attr\": \"src\"\n },\n \"title\": {\n\"css\": \"#comic img\",\n\"attr\": \"title\"\n }\n }\""
  22. end
  23. end
  24. describe "#value_at" do
  25. it "returns the value at a JSON path" do
  26. Utils.value_at({ :foo => { :bar => :baz }}.to_json, "foo.bar").should == "baz"
  27. Utils.value_at({ :foo => { :bar => { :bing => 2 } }}, "foo.bar.bing").should == 2
  28. end
  29. it "returns nil when the path cannot be followed" do
  30. Utils.value_at({ :foo => { :bar => :baz }}, "foo.bing").should be_nil
  31. end
  32. it "does not eval" do
  33. lambda {
  34. Utils.value_at({ :foo => 2 }, "foo[?(@ > 1)]")
  35. }.should raise_error(RuntimeError, /Cannot use .*? eval/)
  36. end
  37. end
  38. describe "#values_at" do
  39. it "returns arrays of matching values" do
  40. Utils.values_at({ :foo => { :bar => :baz }}, "foo.bar").should == %w[baz]
  41. Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar").should == %w[baz bing]
  42. Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar").should == %w[baz bing]
  43. end
  44. it "should allow escaping" do
  45. Utils.values_at({ :foo => { :bar => "escape this!?" }}, "escape $.foo.bar").should == ["escape+this%21%3F"]
  46. end
  47. end
  48. end