Module: GraphQL::Compatibility::QueryParserSpecification::ParseErrorSpecification

Defined in:
lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb

Instance Method Summary collapse

Instance Method Details

#assert_empty_document(query_string) ⇒ Object


58
59
60
61
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 58

def assert_empty_document(query_string)
  doc = parse(query_string)
  assert_equal 0, doc.definitions.length
end

#assert_raises_parse_error(query_string) ⇒ Object


5
6
7
8
9
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 5

def assert_raises_parse_error(query_string)
  assert_raises(GraphQL::ParseError) {
    parse(query_string)
  }
end

#assert_rejects_character(char) ⇒ Object


32
33
34
35
36
37
38
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 32

def assert_rejects_character(char)
  err = assert_raises_parse_error("{ field#{char} }")
  expected_char = char.inspect.gsub('"', '').downcase
  msg_downcase = err.message.downcase
  # Case-insensitive for UTF-8 printing
  assert_includes(msg_downcase, expected_char, "The message includes the invalid character")
end

#test_it_includes_line_and_columnObject


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 11

def test_it_includes_line_and_column
  err = assert_raises_parse_error("
    query getCoupons {
      allCoupons: {data{id}}
    }
  ")

  assert_includes(err.message, '{')
  assert_equal(3, err.line)
  assert_equal(27, err.col)
end

#test_it_parses_blank_queriesObject


63
64
65
66
67
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 63

def test_it_parses_blank_queries
  assert_empty_document("")
  assert_empty_document(" ")
  assert_empty_document("\t \t")
end

#test_it_rejects_bad_unicodeObject


49
50
51
52
53
54
55
56
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 49

def test_it_rejects_bad_unicode
  assert_raises_parse_error(%|{ field(arg:"\\x") }|)
  assert_raises_parse_error(%|{ field(arg:"\\u1") }|)
  assert_raises_parse_error(%|{ field(arg:"\\u0XX1") }|)
  assert_raises_parse_error(%|{ field(arg:"\\uXXXX") }|)
  assert_raises_parse_error(%|{ field(arg:"\\uFXXX") }|)
  assert_raises_parse_error(%|{ field(arg:"\\uXXXF") }|)
end

#test_it_rejects_invalid_charactersObject


40
41
42
43
44
45
46
47
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 40

def test_it_rejects_invalid_characters
  assert_rejects_character(";")
  assert_rejects_character("\a")
  assert_rejects_character("\xef")
  assert_rejects_character("\v")
  assert_rejects_character("\f")
  assert_rejects_character("\xa0")
end

#test_it_rejects_unexpected_endsObject


28
29
30
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 28

def test_it_rejects_unexpected_ends
  assert_raises_parse_error("query { stuff { thing }")
end

#test_it_rejects_unterminated_stringsObject


23
24
25
26
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 23

def test_it_rejects_unterminated_strings
  assert_raises_parse_error('{ " }')
  assert_raises_parse_error(%|{ "\n" }|)
end

#test_it_restricts_onObject


69
70
71
72
# File 'lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb', line 69

def test_it_restricts_on
  assert_raises_parse_error("{ ...on }")
  assert_raises_parse_error("fragment on on Type { field }")
end