|
16 | 16 | assert_equal expected_message, err.message
|
17 | 17 | end
|
18 | 18 |
|
19 |
| - it "rejects newlines in single-quoted strings" do |
| 19 | + it "rejects newlines in single-quoted strings unless escaped" do |
| 20 | + nl_query_string_1 = "{ doStuff(arg: \" |
| 21 | + abc\") }" |
| 22 | + nl_query_string_2 = "{ doStuff(arg: \"\rabc\") }" |
| 23 | + |
20 | 24 | assert_raises(GraphQL::ParseError) {
|
21 |
| - GraphQL.parse("{ doStuff(arg: \" |
22 |
| -abc\") }" |
23 |
| - ) |
| 25 | + GraphQL.parse(nl_query_string_2) |
24 | 26 | }
|
25 | 27 | assert_raises(GraphQL::ParseError) {
|
26 |
| - GraphQL.parse("{ doStuff(arg: \"\rabc\") }") |
| 28 | + GraphQL.parse(nl_query_string_2) |
27 | 29 | }
|
| 30 | + |
| 31 | + assert GraphQL.parse(GraphQL::Language.escape_single_quoted_newlines(nl_query_string_1)) |
| 32 | + assert GraphQL.parse(GraphQL::Language.escape_single_quoted_newlines(nl_query_string_2)) |
| 33 | + end |
| 34 | + |
| 35 | + it "can replace single-quoted newlines" do |
| 36 | + replacements = { |
| 37 | + "{ a(\"\n abc\n\") }" => '{ a("\\n abc\\n") }', |
| 38 | + "{ a(\"\r\n ab\rc\n\") }" => '{ a("\\r\\n ab\\rc\\n") }', |
| 39 | + "{ a(\"\n abc\n\") b(\"\n \\\"abc\n\") }" => '{ a("\\n abc\\n") b("\\n \\"abc\\n") }', |
| 40 | + # No modification to block strings: |
| 41 | + "{ a(\"\"\"\n abc\n\"\"\") }" => "{ a(\"\"\"\n abc\n\"\"\") }", |
| 42 | + "{ a(\"\"\"\r\n abc\r\n\"\"\") }" => "{ a(\"\"\"\r\n abc\r\n\"\"\") }", |
| 43 | + } |
| 44 | + |
| 45 | + replacements.each_with_index do |(before_str, after_str), idx| |
| 46 | + assert_equal after_str, GraphQL::Language.escape_single_quoted_newlines(before_str), "It works for example pair ##{idx + 1} (#{after_str})" |
| 47 | + end |
28 | 48 | end
|
29 | 49 |
|
30 | 50 | describe "when there are no selections" do
|
|
0 commit comments