spec/builder_spec.rb in jsonify-0.0.2 vs spec/builder_spec.rb in jsonify-0.0.3
- old
+ new
@@ -61,50 +61,63 @@
}
PRETTY_JSON
json.compile!.should == pretty_results.chomp
end
end
+
+ describe 'array creation' do
+ 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.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"}'
+ end
+ end
end
describe 'arrays' do
it 'simple array should work' do
- json.array! do |ary|
- ary << 1
- ary << 2
- end
+ json << 1
+ json << 2
json.compile!.should == "[1,2]"
end
it 'array of arrays should work' do
- json.array! do |ary|
- ary << json.array! {|a| a << 1}
- ary << json.array! {|b| b << 2}
- ary << 3
- end
- json.compile!.should == "[[1],[2],3]"
+ json << [1]
+ json << [2]
+ json << [3]
+ json.compile!.should == "[[1],[2],[3]]"
end
it 'array of hashes should work' do
- json.array! do |ary|
- ary << {:foo => :bar}
- ary << {:go => :far}
- end
+ json << {:foo => :bar}
+ json << {:go => :far}
json.compile!.should == "[{\"foo\":\"bar\"},{\"go\":\"far\"}]"
end
end
describe 'objects' do
it 'simple object should work' do
- json.object! do |obj|
- obj.add :foo,:bar
- obj.add :go, :far
- end
+ json.foo :bar
+ json.go :far
json.compile!.should == "{\"foo\":\"bar\",\"go\":\"far\"}"
end
it 'should handle arrays' do
- json.object! do |obj|
- obj.add 1, [2, 3]
- obj.add 4, 5
- end
+ json[1] = [2, 3]
+ json[4] = 5
json.compile!.should == '{"1":[2,3],"4":5}'
end
end
describe 'using blocks' do
@@ -134,14 +147,12 @@
json.compile!.should == "{\"foo\":{\"baz\":\"goo\"}}"
end
it 'hash with array' do
json.foo do
- json.array! do |ary|
- ary << 1
- ary << 2
- end
+ json << 1
+ json << 2
end
json.compile!.should == "{\"foo\":[1,2]}"
end
it 'hash with array by iteration' do
@@ -151,26 +162,22 @@
end
json.compile!.should == "{\"foo\":[2,4,6]}"
end
it 'simple array with object' do
- json.array! do |ary|
- ary << 1
- ary << (json.foo 'bar')
- end
+ json << 1
+ json << {:foo => :bar}
json.compile!.should == "[1,{\"foo\":\"bar\"}]"
end
it 'complex hash with array' do
json.foo do
json.bar do
json.baz 'goo'
json.years do
- json.array! do |ary|
- ary << 2011
- ary << 2012
- end
+ json << 2011
+ json << 2012
end
end
end
expected = "{\"foo\":{\"bar\":{\"baz\":\"goo\",\"years\":[2011,2012]}}}"
JSON.parse(json.compile!).should == JSON.parse(expected)
@@ -201,11 +208,11 @@
[
link_class.new('example.com', 'self'),
link_class.new('foo.com', 'parent')
]
}
- it 'should work using array!' do
+ it 'should work using arrays' do
json.result do
json.person do
json.fname 'George'
json.lname 'Burdell'
end
@@ -214,25 +221,7 @@
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
-
- it "should work using map! with argument" do
- json.result do
- json.person do
- json.fname 'George'
- json.lname 'Burdell'
- end
- json.links do
- json.map!(links) do |link|
- { :href => link.url, :rel => link.type}
- end
- 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
-
-
end
\ No newline at end of file