spec/builder_spec.rb in jsonify-0.0.3 vs spec/builder_spec.rb in jsonify-0.0.4
- old
+ new
@@ -68,24 +68,22 @@
it 'with the append operator (<<)' do
json << 1 << 2
json.compile!.should == "[1,2]"
end
it 'with the append! method' do
- json.append!( 1,2 )
- .append! 3
+ json.append!( 1,2 ).append! 3
json.compile!.should == "[1,2,3]"
end
end
describe 'object creation' do
it 'should support the element assignment operator( []= )' do
json["foo"] = 'bar'
json.compile!.should == '{"foo":"bar"}'
end
it 'should support the store! message' do
- json.store!( "foo", "bar" )
- .store!( 'no', "whar" )
- json.compile!.should == '{"foo":"bar","no":"whar"}'
+ json.store!( "foo", "bar" ).store!( 'no', "whar" )
+ JSON.parse(json.compile!).should == JSON.parse('{"foo":"bar","no":"whar"}')
end
end
end
describe 'arrays' do
@@ -101,19 +99,20 @@
json.compile!.should == "[[1],[2],[3]]"
end
it 'array of hashes should work' do
json << {:foo => :bar}
json << {:go => :far}
- json.compile!.should == "[{\"foo\":\"bar\"},{\"go\":\"far\"}]"
+ json.compile!.should == '[{"foo":"bar"},{"go":"far"}]'
end
end
describe 'objects' do
it 'simple object should work' do
json.foo :bar
json.go :far
- json.compile!.should == "{\"foo\":\"bar\",\"go\":\"far\"}"
+ expected = '{"foo":"bar","go":"far"}'
+ JSON.parse(json.compile!).should == JSON.parse(expected)
end
it 'should handle arrays' do
json[1] = [2, 3]
json[4] = 5
json.compile!.should == '{"1":[2,3],"4":5}'
@@ -126,50 +125,58 @@
json.tag!("foo foo") do
json.tag!("bar bar") do
json.tag!('buzz buzz','goo goo')
end
end
- json.compile!.should == "{\"foo foo\":{\"bar bar\":{\"buzz buzz\":\"goo goo\"}}}"
+ expected = '{"foo foo":{"bar bar":{"buzz buzz":"goo goo"}}}'
+ JSON.parse(json.compile!).should == JSON.parse(expected)
end
it 'complex hash' do
json.foo do
json.bar do
json.baz 'goo'
end
end
- json.compile!.should == "{\"foo\":{\"bar\":{\"baz\":\"goo\"}}}"
+ json.compile!.should == '{"foo":{"bar":{"baz":"goo"}}}'
end
it 'simple hash' do
json.foo do
json.baz :goo
end
- json.compile!.should == "{\"foo\":{\"baz\":\"goo\"}}"
+ json.compile!.should == '{"foo":{"baz":"goo"}}'
end
it 'hash with array' do
json.foo do
json << 1
json << 2
end
- json.compile!.should == "{\"foo\":[1,2]}"
+ json.compile!.should == '{"foo":[1,2]}'
end
it 'hash with array by iteration' do
ary = [1,2,3]
json.foo(ary) do |n|
n * 2
end
- json.compile!.should == "{\"foo\":[2,4,6]}"
+ json.compile!.should == '{"foo":[2,4,6]}'
end
it 'simple array with object' do
json << 1
json << {:foo => :bar}
- json.compile!.should == "[1,{\"foo\":\"bar\"}]"
+ json.compile!.should == '[1,{"foo":"bar"}]'
end
+
+ it 'simple array with object via method_missing' do
+ json << 1
+ json << 2
+ json.foo :bar
+ json.compile!.should == "[1,2,{\"foo\":\"bar\"}]"
+ end
it 'complex hash with array' do
json.foo do
json.bar do
json.baz 'goo'
@@ -220,8 +227,53 @@
{ :href => link.url, :rel => link.type}
end
end
expected = "{\"result\":{\"person\":{\"fname\":\"George\",\"lname\":\"Burdell\"},\"links\":[{\"href\":\"example.com\",\"rel\":\"self\"},{\"href\":\"foo.com\",\"rel\":\"parent\"}]}}"
JSON.parse(json.compile!).should == JSON.parse(expected)
+ end
+ end
+
+ describe 'ingest!' do
+ context 'a json object' do
+ let(:json_string) { '{"my girl":"Friday","my daughter":"Wednesday"}' }
+ context 'into' do
+ it 'nothing -- should replace it' do
+ json.ingest! json_string
+ JSON.parse(json.compile!).should == JSON.parse(json_string)
+ end
+ it 'json object -- should merge' do
+ json["my boy"] = "Monday"
+ json["my girl"] = "Sunday"
+ json.ingest! json_string
+ expected = '{"my boy":"Monday","my girl":"Friday","my daughter":"Wednesday"}'
+ JSON.parse(json.compile!).should == JSON.parse(expected)
+ end
+ it 'json array -- should add' do
+ json << 1 << 2
+ json.ingest! json_string
+ expected = '[1,2,{"my girl":"Friday","my daughter":"Wednesday"}]'
+ JSON.parse(json.compile!).should == JSON.parse(expected)
+ end
+ end
+ end
+ context 'a json array' do
+ let(:json_string) { '[1,2,3]' }
+ context 'into' do
+ it 'nothing -- should replace it' do
+ json.ingest! json_string
+ JSON.parse(json.compile!).should == JSON.parse(json_string)
+ end
+ it 'json object -- should raise error' do
+ json["my boy"] = "Monday"
+ json["my girl"] = "Sunday"
+ lambda{ json.ingest! json_string }.should raise_error( ArgumentError )
+ end
+ it 'json array -- should add' do
+ json << 1 << 2
+ json.ingest! json_string
+ expected = '[1,2,[1,2,3]]'
+ JSON.parse(json.compile!).should == JSON.parse(expected)
+ end
+ end
end
end
end
\ No newline at end of file