Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Dec 11, 2023
1 parent 62c621b commit 11db91f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
33 changes: 27 additions & 6 deletions lib/graphql/language/recursive_descent_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def initialize(graphql_str, filename: nil, trace: Tracing::NullTrace)
if graphql_str.nil?
raise GraphQL::ParseError.new("No query string was present", nil, nil, nil)
end
@lexer = Lexer.new(graphql_str)
@lexer = Lexer.new(graphql_str, filename: filename)
@graphql_str = graphql_str
@filename = filename
@trace = trace
Expand Down Expand Up @@ -61,7 +61,11 @@ def pos
end

def document
advance_token
any_tokens = advance_token
if !any_tokens
# Only ignored characters is not a valid document
raise GraphQL::ParseError.new("Unexpected end of document", nil, nil, @graphql_str)
end
defns = []
while !@lexer.eos?
defns << definition
Expand Down Expand Up @@ -710,18 +714,19 @@ def expect_token_value(tok)
# token_value works for when the scanner matched something
# which is usually fine and it's good for it to be fast at that.
def debug_token_value
if Lexer::Punctuation.const_defined?(token_name)
if token_name && Lexer::Punctuation.const_defined?(token_name)
Lexer::Punctuation.const_get(token_name)
elsif token_name == :ELLIPSIS
"..."
else
token_value
@lexer.token_value
end
end

class Lexer
def initialize(graphql_str)
def initialize(graphql_str, filename: nil)
@string = graphql_str
@filename = filename
@scanner = StringScanner.new(graphql_str)
@pos = nil
end
Expand Down Expand Up @@ -777,12 +782,28 @@ def advance
if @scanner.skip(BLOCK_STRING_REGEXP) || @scanner.skip(QUOTED_STRING_REGEXP)
:STRING
else
raise_parse_error("Expected string or block string, but it was malformed")
raise GraphQL::ParseError.new(
"Expected string or block string, but it was malformed",
line_number,
column_number,
@string,
filename: @filename
)
end
else
@scanner.pos += 1
:UNKNOWN_CHAR
end
rescue ArgumentError => err
if err.message == "invalid byte sequence in UTF-8"
raise GraphQL::ParseError.new(
"Parse error on bad Unicode escape sequence",
nil,
nil,
@string,
filename: @filename
)
end
end

def token_value
Expand Down
8 changes: 7 additions & 1 deletion spec/graphql/language/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
err = assert_raises GraphQL::ParseError do
subject.parse("{ foo(query: \"\xBF\") }")
end
expected_message = 'Parse error on bad Unicode escape sequence: "{ foo(query: \"\xBF\") }" (error) at [1, 1]'
expected_message = if using_recursive_descent_parser?
'Parse error on bad Unicode escape sequence'
else
'Parse error on bad Unicode escape sequence: "{ foo(query: \"\xBF\") }" (error) at [1, 1]'
end
assert_equal expected_message, err.message
end

Expand All @@ -30,6 +34,8 @@
}
expected_msg = if USING_C_PARSER
"syntax error, unexpected invalid token (\"\\xF0\"), expecting LCURLY at [1, 7]"
elsif using_recursive_descent_parser?
"Expected LCURLY, actual: UNKNOWN_CHAR (\"\\xF0\") at [1, 7]"
else
"Parse error on \"😘\" (error) at [1, 7]"
end
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def self.after_query(q)
expected_err = "syntax error, unexpected end of file at [1, 2]"
expected_locations = [{"line" => 1, "column" => 2}]
elsif using_recursive_descent_parser?
expected_err = "Expected NAME, actual: NOTHING (\" \") at [1, 3]"
expected_err = "Expected NAME, actual: (none) (\" \") at [1, 3]"
expected_locations = [{"line" => 1, "column" => 3}]
else
expected_err = "Unexpected end of document"
Expand Down
4 changes: 2 additions & 2 deletions spec/graphql/tracing/data_dog_trace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def prepare_span(trace_key, data, span)
it "sets custom tags tags" do
DataDogTraceTest::CustomTracerTestSchema.execute("{ thing { str } }")
expected_custom_tags = [
["custom:lex", "query_string"],
(using_recursive_descent_parser? ? nil : ["custom:parse", "query_string"]),
(using_recursive_descent_parser? ? nil : ["custom:lex", "query_string"]),
["custom:parse", "query_string"],
["custom:execute_multiplex", "multiplex"],
["custom:analyze_multiplex", "multiplex"],
["custom:validate", "query,validate"],
Expand Down

0 comments on commit 11db91f

Please sign in to comment.