Skip to content

Commit

Permalink
Make projection parser behave the same as regular parser
Browse files Browse the repository at this point in the history
The projection parser should raise the same exceptions that the regular
JSON parser raises when parsing a bad document
  • Loading branch information
tenderlove committed Dec 12, 2017
1 parent a5c4030 commit 1e50529
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/yajl/yajl_ext.c
Expand Up @@ -792,7 +792,7 @@ static void rb_yajl_projector_ignore_value(yajl_event_stream_t parser) {
return;
}

rb_raise(cStandardError, "unknown value type to ignore %s", yajl_tok_name(value_event.token));
rb_raise(cParseError, "unknown value type to ignore %s", yajl_tok_name(value_event.token));
}

/*
Expand Down
33 changes: 33 additions & 0 deletions spec/projection/projection.rb
Expand Up @@ -42,6 +42,39 @@
}.to raise_error(Yajl::ParseError)
end

it "should behave the same way as the regular parser on bad tokens like comma" do
bad_json = '{"name": "keith", "age":, 27}'
stream = StringIO.new(bad_json)
projector = Yajl::Projector.new(stream)
expect {
projector.project({"name" => nil})
}.to raise_error(capture_exception_for(bad_json).class)
end

it "should behave the same way as the regular parser on bad tokens like colon" do
bad_json = '{"name": "keith", "age":: 27}'
stream = StringIO.new(bad_json)
projector = Yajl::Projector.new(stream)
expect {
projector.project({"name" => nil})
}.to raise_error(capture_exception_for(bad_json).class)
end

it "should behave the same way as the regular parser on not enough json" do
bad_json = '{"name": "keith", "age":'
stream = StringIO.new(bad_json)
projector = Yajl::Projector.new(stream)
expect {
projector.project({"name" => nil})
}.to raise_error(capture_exception_for(bad_json).class)
end

def capture_exception_for(bad_json)
Yajl::Parser.new.parse(bad_json)
rescue Exception => e
e
end

def project(schema, over: "", json: nil, stream: nil)
if stream.nil?
if json.nil?
Expand Down

0 comments on commit 1e50529

Please sign in to comment.