From 8f640b6d665926c6687d6138c8f6485b43c3a857 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Sat, 14 Jun 2014 16:50:46 -0700 Subject: support yajl parser options --- spec/ffi_yajl/parser_spec.rb | 108 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/ffi_yajl/parser_spec.rb b/spec/ffi_yajl/parser_spec.rb index 0ce848f..4f6186e 100644 --- a/spec/ffi_yajl/parser_spec.rb +++ b/spec/ffi_yajl/parser_spec.rb @@ -35,24 +35,114 @@ describe "FFI_Yajl::Parser" do end end + context "when json has multiline comments" do + let(:json) { %Q{{"key": \n/*\n this is a multiline comment \n*/\n "value"}} } + + context "when allow_comments is false" do + let(:options) { { :allow_comments => false } } + + it "should not parse" do + expect{parser}.to raise_error(FFI_Yajl::ParseError) + end + end + + context "when allow_comments is true" do + let(:options) { { :allow_comments => true } } + + it "should parse" do + expect(parser).to eq({"key"=>"value"}) + end + end + end + + context "when json has inline comments" do + let(:json) { %Q{{"key": \n// this is an inline comment\n "value"}} } + + context "when allow_comments is false" do + let(:options) { { :allow_comments => false } } + + it "should not parse" do + expect{parser}.to raise_error(FFI_Yajl::ParseError) + end + end + + context "when allow_comments is true" do + let(:options) { { :allow_comments => true } } + + it "should parse" do + expect(parser).to eq({"key"=>"value"}) + end + end + end + context "when json is invalid UTF8" do let(:json) { "[\"#{"\201\203"}\"]" } + it "should not parse by default" do + expect{parser}.to raise_error(FFI_Yajl::ParseError) + end + + context "when :dont_validate_strings is set to true" do + let(:options) { { :dont_validate_strings => true } } + + it "should parse" do + expect(parser).to eq(["\x81\x83"]) + end + end + + context "when :dont_validate_strings is set to false" do + let(:options) { { :dont_validate_strings => false } } + + it "should not parse" do + expect{parser}.to raise_error(FFI_Yajl::ParseError) + end + end + context "when :check_utf8 is set to true" do let(:options) { { :check_utf8 => true } } it "should not parse" do - skip "implement :check_utf8" expect{parser}.to raise_error(FFI_Yajl::ParseError) end + + context "when :dont_validate_strings is set to true" do + let(:options) { { :check_utf8 => true, :dont_validate_strings => true } } + + it "should raise an ArgumentError" do + expect{parser}.to raise_error(ArgumentError) + end + end + + context "when :dont_validate_strings is set to false" do + let(:options) { { :check_utf8 => true, :dont_validate_strings => false } } + + it "should not parse" do + expect{parser}.to raise_error(FFI_Yajl::ParseError) + end + end end context "when :check_utf8 is set to false" do let(:options) { { :check_utf8 => false } } it "should parse" do - skip "implement :check_utf8" - expect(parser).to eq({}) # FIXME + expect(parser).to eq(["\x81\x83"]) + end + + context "when :dont_validate_strings is set to true" do + let(:options) { { :check_utf8 => false, :dont_validate_strings => true } } + + it "should parse" do + expect(parser).to eq(["\x81\x83"]) + end + end + + context "when :dont_validate_strings is set to false" do + let(:options) { { :check_utf8 => false, :dont_validate_strings => false } } + + it "should raise an ArgumentError" do + expect{parser}.to raise_error(ArgumentError) + end end end end @@ -194,7 +284,7 @@ describe "FFI_Yajl::Parser" do # NOTE: this fixes yajl-ruby being too permissive context "when dealing with too much or too little input" do context "when trailing braces are missing" do - let(:json) { '{{"foo": 1234}' } + let(:json) { '{"foo":{"foo": 1234}' } it "raises an exception" do expect { parser }.to raise_error(FFI_Yajl::ParseError) @@ -210,11 +300,19 @@ describe "FFI_Yajl::Parser" do end context "when an extra brace is present" do - let(:json) { '{{"foo": 1234}}}' } + let(:json) { '{"foo":{"foo": 1234}}}' } it "raises an exception" do expect { parser }.to raise_error(FFI_Yajl::ParseError) end + + context "with allow_trailing_garbage" do + let(:options) { { :allow_trailing_garbage => true } } + it "parses" do + expect(parser).to eq({"foo"=>{"foo"=>1234}}) + end + end + end context "when an extra bracket is present" do -- cgit v1.2.1