spec/builder_spec.rb in jsonify-0.1.3 vs spec/builder_spec.rb in jsonify-0.2.0
- old
+ new
@@ -182,25 +182,18 @@
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
+ json.foo do
+ ary.each do |n|
+ json << (n * 2)
+ end
end
json.compile!.should == '{"foo":[2,4,6]}'
end
- it 'hash with empty array by iteration' do
- ary = []
- json.foo(ary) do |n|
- n * 2
- end
- expected = '{"foo":[]}'
- JSON.parse(json.compile!).should == JSON.parse(expected)
- end
-
it 'simple array with object' do
json << 1
json << {:foo => :bar}
json.compile!.should == '[1,{"foo":"bar"}]'
end
@@ -259,11 +252,12 @@
json.person do
json.fname 'George'
json.lname 'Burdell'
end
json.links(links) do |link|
- { :href => link.url, :rel => link.type}
+ json.href link.url
+ json.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
@@ -311,6 +305,33 @@
JSON.parse(json.compile!).should == JSON.parse(expected)
end
end
end
end
-end
\ No newline at end of file
+
+ describe 'with new array style' do
+ it 'should work' do
+ results =[
+ {:id => 1, :kids => [{:id => 'a'},{:id => 'b'}]},
+ {:id => 2, :kids => [{:id => 'c'},{:id => 'd'}]},
+ ]
+
+ json.results(results) do |result|
+ json.id result[:id]
+ json.children(result[:kids]) do |kid|
+ json.id kid[:id]
+ end
+ end
+
+ expected = '{"results":[{"id":1,"children":[{"id":"a"},{"id":"b"}]},{"id":2,"children":[{"id":"c"},{"id":"d"}]}]}'
+ JSON.parse(json.compile!).should == JSON.parse(expected)
+ end
+ it 'simple append' do
+ json.letters('a'..'c') do |letter|
+ json << letter.upcase
+ end
+ expected = '{"letters":["A","B","C"]}'
+ JSON.parse(json.compile!).should == JSON.parse(expected)
+ end
+
+ end
+end