spec/edn/transform_spec.rb in edn-0.9.0 vs spec/edn/transform_spec.rb in edn-0.9.1

- old
+ new

@@ -1,18 +1,22 @@ require 'spec_helper' describe EDN::Transform do context "integer" do it "should emit an integer" do - subject.apply(:integer => "1").should == 1 + subject.apply(:integer => "1", :precision => nil).should == 1 end end context "float" do it "should emit an float" do - subject.apply(:float => "1.0").should == 1.0 + subject.apply(:float => "1.0", :precision => nil).should == 1.0 end + + it "should emit a BigDecimal if suffixed with M" do + subject.apply(:float => "1.0", :precision => "M").should == BigDecimal("1.0") + end end context "string" do it "should emit a string with control characters substituted" do subject.apply(:string => 'hello\nworld').should == "hello\nworld" @@ -61,25 +65,25 @@ end context "vector" do it "should emit an array" do subject.apply(:vector => []).should == [] - subject.apply(:vector => [{:integer => "1"}, {:string => "abc"}]).should == [1, "abc"] - subject.apply(:vector => [{:vector => [{:integer => "1"}, {:string => "abc"}]}, {:float => "3.14"}]).should == [[1, "abc"], 3.14] + subject.apply(:vector => [{:integer => "1", :precision => nil}, {:string => "abc"}]).should == [1, "abc"] + subject.apply(:vector => [{:vector => [{:integer => "1", :precision => nil}, {:string => "abc"}]}, {:float => "3.14", :precision => nil}]).should == [[1, "abc"], 3.14] end end context "list" do it "should emit a list" do subject.apply(:list => []).should == EDN::Type::List.new - subject.apply(:list => [{:integer => "1"}, {:string => "abc"}]).should == EDN::Type::List.new(1, "abc") - subject.apply(:list => [{:list => [{:integer => "1"}, {:string => "abc"}]}, {:float => "3.14"}]).should == \ + subject.apply(:list => [{:integer => "1", :precision => nil}, {:string => "abc"}]).should == EDN::Type::List.new(1, "abc") + subject.apply(:list => [{:list => [{:integer => "1", :precision => nil}, {:string => "abc"}]}, {:float => "3.14", :precision => nil}]).should == \ EDN::Type::List.new(EDN::Type::List.new(1, "abc"), 3.14) end it "should be type-compatible with arrays" do - subject.apply(:list => [{:integer => "1"}, {:string => "abc"}]).should == [1, "abc"] + subject.apply(:list => [{:integer => "1", :precision => nil}, {:string => "abc"}]).should == [1, "abc"] end end context "set" do it "should emit a set" do @@ -89,41 +93,64 @@ end context "map" do it "should emit a hash" do map_tree = {:map=> - [ {:key=>{:keyword=>{:symbol=>"a"}}, :value=>{:integer=>"1"}}, - {:key=>{:keyword=>{:symbol=>"b"}}, :value=>{:integer=>"2"}} + [ {:key=>{:keyword=>{:symbol=>"a"}}, :value=>{:integer=>"1", :precision => nil}}, + {:key=>{:keyword=>{:symbol=>"b"}}, :value=>{:integer=>"2", :precision => nil}} ] } subject.apply(map_tree).should == {:a => 1, :b => 2} end it "should work with nested maps" do map_tree = {:map=> - [{:key=>{:keyword=>{:symbol=>"a"}}, :value=>{:integer=>"1"}}, - {:key=>{:keyword=>{:symbol=>"b"}}, :value=>{:integer=>"2"}}, + [{:key=>{:keyword=>{:symbol=>"a"}}, :value=>{:integer=>"1", :precision => nil}}, + {:key=>{:keyword=>{:symbol=>"b"}}, :value=>{:integer=>"2", :precision => nil}}, {:key=> {:map=> - [{:key=>{:keyword=>{:symbol=>"c"}}, :value=>{:integer=>"3"}}]}, - :value=>{:integer=>"4"}}]} + [{:key=>{:keyword=>{:symbol=>"c"}}, :value=>{:integer=>"3", :precision => nil}}]}, + :value=>{:integer=>"4", :precision => nil}}]} subject.apply(map_tree).should == {:a => 1, :b => 2, {:c => 3} => 4} end end context "tagged value" do it "should emit a EDN::Type::Unknown if the tag is not registered" do - subject.apply(:tagged_value => { - :tag => 'uri', :value => {:string => 'http://google.com'} - }).should == EDN::Type::Unknown.new("uri", "http://google.com") + subject.apply({:tag => {:symbol => 'uri'}, :value => {:string => 'http://google.com'}}).should == EDN::Type::Unknown.new("uri", "http://google.com") end it "should emit the transformed value if the tag is registered" do EDN.register("uri", lambda { |uri| URI(uri) }) - subject.apply(:tagged_value => { - :tag => 'uri', :value => {:string => 'http://google.com'} - }).should == URI("http://google.com") + subject.apply({:tag => {:symbol => 'uri'}, :value => {:string => 'http://google.com'}}).should == URI("http://google.com") EDN.unregister("uri") # cleanup + end + + it "should work with nested values" do + tree = { + :tag=>{:symbol=>"cnd/awesome"}, + :value=> + {:vector=> + [{:keyword=>{:symbol=>"a"}}, + {:list=> + [{:integer=>"1", :precision=>nil}, + {:list=> + [{:integer=>"2", :precision=>nil}, + {:integer=>"3", :precision=>nil}]}]}, + {:keyword=>{:symbol=>"b"}}, + {:keyword=>{:symbol=>"c"}}, + {:set=> + [{:integer=>"1", :precision=>nil}, + {:integer=>"2", :precision=>nil}, + {:integer=>"3", :precision=>nil}]}, + {:vector=> + [{:vector=> + [{:vector=>[{:integer=>"42", :precision=>nil}]}, + {:integer=>"42", :precision=>nil}]}]}]}} + expected = EDN::Type::Unknown.new('cnd/awesome', + [:a, [1, [2, 3]], :b, :c, + Set.new([1, 2, 3]), [[[42], 42]]]) + subject.apply(tree).should == expected end end end