spec/builder_spec.rb in jsonify-0.0.1 vs spec/builder_spec.rb in jsonify-0.0.2
- old
+ new
@@ -15,18 +15,23 @@
json.compile!.should == "{}"
end
end
describe 'with verify set' do
it 'should report a parse error if the result is not parseable' do
- json = Jsonify::Builder.new(:verify => true)
+
# Hackery to come up with a failing case
+ class TestBuilder < Jsonify::Builder
+ attr_accessor :stack
+ end
class FooBar
def evaluate
"foobar"
end
end
- json.instance_variable_set(:@stack, [FooBar.new])
+
+ json = TestBuilder.new(:verify => true)
+ json.stack << FooBar.new
lambda{ json.compile! }.should raise_error(JSON::ParserError)
end
end
describe 'unicode characters' do
it 'should properly encode' do
@@ -76,60 +81,79 @@
end
json.compile!.should == "[[1],[2],3]"
end
it 'array of hashes should work' do
json.array! do |ary|
- ary << {foo: :bar}
- ary << {go: :far}
+ ary << {:foo => :bar}
+ ary << {:go => :far}
end
json.compile!.should == "[{\"foo\":\"bar\"},{\"go\":\"far\"}]"
end
end
describe 'objects' do
it 'simple object should work' do
json.object! do |obj|
- obj << [:foo,:bar]
- obj << [:go, :far]
+ obj.add :foo,:bar
+ obj.add :go, :far
end
json.compile!.should == "{\"foo\":\"bar\",\"go\":\"far\"}"
end
- it 'should treat the first element of an array as a key' do
+ it 'should handle arrays' do
json.object! do |obj|
- obj << [1, 2, 3]
- obj << [4, 5]
+ obj.add 1, [2, 3]
+ obj.add 4, 5
end
json.compile!.should == '{"1":[2,3],"4":5}'
end
end
describe 'using blocks' do
-
+
+ it 'should allow names with spaces using tag!' do
+ 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\"}}}"
+ end
+
it 'complex hash' do
json.foo do
json.bar do
json.baz 'goo'
end
end
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\"}}"
end
+
it 'hash with array' do
json.foo do
json.array! do |ary|
ary << 1
ary << 2
end
end
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]}"
+ end
+
it 'simple array with object' do
json.array! do |ary|
ary << 1
ary << (json.foo 'bar')
end
@@ -146,21 +170,31 @@
ary << 2012
end
end
end
end
- json.compile!.should == "{\"foo\":{\"bar\":{\"baz\":\"goo\",\"years\":[2011,2012]}}}"
+ expected = "{\"foo\":{\"bar\":{\"baz\":\"goo\",\"years\":[2011,2012]}}}"
+ JSON.parse(json.compile!).should == JSON.parse(expected)
end
end
describe 'without blocks' do
+
describe 'complex array' do
it 'should work' do
json.bar [1,2,{:foo => 'goo'}]
json.compile!.should == "{\"bar\":[1,2,{\"foo\":\"goo\"}]}"
end
end
+
+ describe 'object with null' do
+ it 'should handle missing argument' do
+ json.foo
+ json.compile!.should == '{"foo":null}'
+ end
+ end
+
end
describe 'super complex example' do
let(:links) {
link_class = Struct.new(:url,:type)
@@ -173,37 +207,31 @@
json.result do
json.person do
json.fname 'George'
json.lname 'Burdell'
end
- json.links do
- json.array! do |ary|
- links.each do |link|
- ary << { href: link.url, rel: link.type}
- end
- end
+ json.links(links) do |link|
+ { :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.compile!.should == expected
+ JSON.parse(json.compile!).should == JSON.parse(expected)
end
- [:map!, :collect!].each do |method|
- it "should work using #{method} with argument" do
- json.result do
- json.person do
- json.fname 'George'
- json.lname 'Burdell'
+ 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
- json.links do
- json.send(method,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.compile!.should == expected
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