Repository: apollographql/apollo-rs
Pull request #274 (2022-08-10 17:55:07)

chore(lexer, parser): add extra tests for #267


Patch:
diff --git a/crates/apollo-compiler/src/queries/values.rs b/crates/apollo-compiler/src/queries/values.rs
index 0ed8993..d371284 100644
--- a/crates/apollo-compiler/src/queries/values.rs
+++ b/crates/apollo-compiler/src/queries/values.rs
@@ -433,7 +433,7 @@ impl<'a> From<&'a str> for OperationType {
 pub struct VariableDefinition {
     pub(crate) name: String,
     pub(crate) ty: Type,
-    pub(crate) default_value: Option<Value>,
+    pub(crate) default_value: Option<DefaultValue>,
     pub(crate) directives: Arc<Vec<Directive>>,
     pub(crate) ast_ptr: SyntaxNodePtr,
 }
@@ -450,7 +450,7 @@ impl VariableDefinition {
     }
 
     /// Get a reference to the variable definition's default value.
-    pub fn default_value(&self) -> Option<&Value> {
+    pub fn default_value(&self) -> Option<&DefaultValue> {
         self.default_value.as_ref()
     }
 
@@ -1436,7 +1436,7 @@ impl InputValueDefinition {
     }
 
     /// Get a refernce to inpul value definition's default_value.
-    pub fn default_value(&self) -> Option<&Value> {
+    pub fn default_value(&self) -> Option<&DefaultValue> {
         self.default_value.as_ref()
     }
 }
diff --git a/crates/apollo-parser/src/lexer/mod.rs b/crates/apollo-parser/src/lexer/mod.rs
index 1334260..6b36120 100644
--- a/crates/apollo-parser/src/lexer/mod.rs
+++ b/crates/apollo-parser/src/lexer/mod.rs
@@ -165,10 +165,7 @@ impl Cursor<'_> {
                     while !self.is_eof() {
                         buf.push(self.bump().unwrap());
                     }
-                    self.add_err(Error::new(
-                        "expected a closing \" for the string value",
-                        buf.clone(),
-                    ));
+                    self.add_err(Error::new("unterminated string value", buf.clone()));
                 }
 
                 if let Some(mut err) = self.err() {
diff --git a/crates/apollo-parser/src/parser/grammar/value.rs b/crates/apollo-parser/src/parser/grammar/value.rs
index bd97271..5db0dd8 100644
--- a/crates/apollo-parser/src/parser/grammar/value.rs
+++ b/crates/apollo-parser/src/parser/grammar/value.rs
@@ -50,14 +50,6 @@ pub(crate) fn value(p: &mut Parser, pop_on_error: bool) {
         }
         Some(T!['[']) => list_value(p),
         Some(T!['{']) => object_value(p),
-        Some(token) => {
-            let error_message = format!("expected a valid Value got {token:?}");
-            if pop_on_error {
-                p.err_and_pop(&error_message);
-            } else {
-                p.err(&error_message);
-            }
-        }
         _ => {
             let error_message = "expected a valid Value";
             if pop_on_error {
@@ -324,7 +316,7 @@ query GraphQuery($graph_id: ID!, $variant: String) {
         let parser = Parser::new(input);
         let ast = parser.parse();
 
-        assert!(ast.errors().next().is_none());
+        assert!(ast.errors.is_empty());
     }
 
     #[test]
@@ -339,7 +331,7 @@ query GraphQuery($graph_id: ID!, $variant: String) {
         let parser = Parser::new(input);
         let ast = parser.parse();
 
-        assert!(ast.errors().next().is_none());
+        assert!(ast.errors.is_empty());
     }
 
     #[test]
@@ -354,7 +346,7 @@ query GraphQuery($graph_id: ID!, $variant: String) {
         let parser = Parser::new(input);
         let ast = parser.parse();
 
-        assert!(ast.errors().next().is_none());
+        assert!(ast.errors.is_empty());
     }
 
     #[test]
@@ -369,7 +361,7 @@ query GraphQuery($graph_id: ID!, $variant: String) {
         let parser = Parser::new(input);
         let ast = parser.parse();
 
-        assert!(ast.errors().next().is_some());
+        assert!(!ast.errors.is_empty());
     }
 
     #[test]


Test patch:
diff --git a/crates/apollo-parser/test_data/lexer/err/0006_unterminated_string_value_in_list_value_argument.graphql b/crates/apollo-parser/test_data/lexer/err/0006_unterminated_string_value_in_list_value_argument.graphql
new file mode 100644
index 0000000..266d34b
--- /dev/null
+++ b/crates/apollo-parser/test_data/lexer/err/0006_unterminated_string_value_in_list_value_argument.graphql
@@ -0,0 +1,11 @@
+extend schema
+  @link(url: "https://specs.apollo.dev/federation/v2.0",
+        import: ["@key", "@external])
+
+type Vehicle @key(fields: "id") {
+  id: ID!,
+  type: String,
+  modelCode: String,
+  brandName: String,
+  launchDate: String
+}
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/lexer/err/0006_unterminated_string_value_in_list_value_argument.txt b/crates/apollo-parser/test_data/lexer/err/0006_unterminated_string_value_in_list_value_argument.txt
new file mode 100644
index 0000000..ba2513d
--- /dev/null
+++ b/crates/apollo-parser/test_data/lexer/err/0006_unterminated_string_value_in_list_value_argument.txt
@@ -0,0 +1,30 @@
+NAME@0:6 "extend"
+WHITESPACE@6:7 " "
+NAME@7:13 "schema"
+WHITESPACE@13:16 "\n  "
+AT@16:17 "@"
+NAME@17:21 "link"
+L_PAREN@21:22 "("
+NAME@22:25 "url"
+COLON@25:26 ":"
+WHITESPACE@26:27 " "
+STRING_VALUE@27:69 "\"https://specs.apollo.dev/federation/v2.0\""
+COMMA@69:70 ","
+WHITESPACE@70:79 "\n        "
+NAME@79:85 "import"
+COLON@85:86 ":"
+WHITESPACE@86:87 " "
+L_BRACKET@87:88 "["
+STRING_VALUE@88:94 "\"@key\""
+COMMA@94:95 ","
+WHITESPACE@95:96 " "
+R_CURLY@234:235 "}"
+EOF@235:235
+ERROR@96:234 "unterminated string value" "@external])
+type Vehicle @key(fields: "id") {
+  id: ID!,
+  type: String,
+  modelCode: String,
+  brandName: String,
+  launchDate: String
+}
diff --git a/crates/apollo-parser/test_data/lexer/err/0007_unterminated_string_value_in_object_value_argument.graphql b/crates/apollo-parser/test_data/lexer/err/0007_unterminated_string_value_in_object_value_argument.graphql
new file mode 100644
index 0000000..cda40ac
--- /dev/null
+++ b/crates/apollo-parser/test_data/lexer/err/0007_unterminated_string_value_in_object_value_argument.graphql
@@ -0,0 +1,7 @@
+mutation {
+  createStore(draft: {
+    name: [{ locale: "en", value: "my store }]
+  }) {
+    name(locale: "en")
+  }
+}
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/lexer/err/0007_unterminated_string_value_in_object_value_argument.txt b/crates/apollo-parser/test_data/lexer/err/0007_unterminated_string_value_in_object_value_argument.txt
new file mode 100644
index 0000000..fcae1a1
--- /dev/null
+++ b/crates/apollo-parser/test_data/lexer/err/0007_unterminated_string_value_in_object_value_argument.txt
@@ -0,0 +1,32 @@
+NAME@0:8 "mutation"
+WHITESPACE@8:9 " "
+L_CURLY@9:10 "{"
+WHITESPACE@10:13 "\n  "
+NAME@13:24 "createStore"
+L_PAREN@24:25 "("
+NAME@25:30 "draft"
+COLON@30:31 ":"
+WHITESPACE@31:32 " "
+L_CURLY@32:33 "{"
+WHITESPACE@33:38 "\n    "
+NAME@38:42 "name"
+COLON@42:43 ":"
+WHITESPACE@43:44 " "
+L_BRACKET@44:45 "["
+L_CURLY@45:46 "{"
+WHITESPACE@46:47 " "
+NAME@47:53 "locale"
+COLON@53:54 ":"
+WHITESPACE@54:55 " "
+STRING_VALUE@55:59 "\"en\""
+COMMA@59:60 ","
+WHITESPACE@60:61 " "
+NAME@61:66 "value"
+COLON@66:67 ":"
+WHITESPACE@67:68 " "
+R_CURLY@115:116 "}"
+EOF@116:116
+ERROR@68:115 "unterminated string value" "my store }]  }) {
+    name(locale: "en")
+  }
+}
diff --git a/crates/apollo-parser/test_data/lexer/err/0008_unterminated_string_value.graphql b/crates/apollo-parser/test_data/lexer/err/0008_unterminated_string_value.graphql
new file mode 100644
index 0000000..cc25b0c
--- /dev/null
+++ b/crates/apollo-parser/test_data/lexer/err/0008_unterminated_string_value.graphql
@@ -0,0 +1,5 @@
+mutation {
+  createstore {
+    name(locale: "en)
+  }
+}
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/lexer/err/0008_unterminated_string_value.txt b/crates/apollo-parser/test_data/lexer/err/0008_unterminated_string_value.txt
new file mode 100644
index 0000000..a55b1e6
--- /dev/null
+++ b/crates/apollo-parser/test_data/lexer/err/0008_unterminated_string_value.txt
@@ -0,0 +1,17 @@
+NAME@0:8 "mutation"
+WHITESPACE@8:9 " "
+L_CURLY@9:10 "{"
+WHITESPACE@10:13 "\n  "
+NAME@13:24 "createstore"
+WHITESPACE@24:25 " "
+L_CURLY@25:26 "{"
+WHITESPACE@26:31 "\n    "
+NAME@31:35 "name"
+L_PAREN@35:36 "("
+NAME@36:42 "locale"
+COLON@42:43 ":"
+WHITESPACE@43:44 " "
+R_CURLY@53:54 "}"
+EOF@54:54
+ERROR@44:53 "unterminated string value" "en)  }
+}
diff --git a/crates/apollo-parser/test_data/lexer/err/0009_unterminated_string_value_as_default.graphql b/crates/apollo-parser/test_data/lexer/err/0009_unterminated_string_value_as_default.graphql
new file mode 100644
index 0000000..d50eb37
--- /dev/null
+++ b/crates/apollo-parser/test_data/lexer/err/0009_unterminated_string_value_as_default.graphql
@@ -0,0 +1,4 @@
+type Person {
+  name: String
+  picture(url: String = "https://spec.graphql.org/October2021/#DefaultValue): Url
+}
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/lexer/err/0009_unterminated_string_value_as_default.txt b/crates/apollo-parser/test_data/lexer/err/0009_unterminated_string_value_as_default.txt
new file mode 100644
index 0000000..1887a49
--- /dev/null
+++ b/crates/apollo-parser/test_data/lexer/err/0009_unterminated_string_value_as_default.txt
@@ -0,0 +1,23 @@
+NAME@0:4 "type"
+WHITESPACE@4:5 " "
+NAME@5:11 "Person"
+WHITESPACE@11:12 " "
+L_CURLY@12:13 "{"
+WHITESPACE@13:16 "\n  "
+NAME@16:20 "name"
+COLON@20:21 ":"
+WHITESPACE@21:22 " "
+NAME@22:28 "String"
+WHITESPACE@28:31 "\n  "
+NAME@31:38 "picture"
+L_PAREN@38:39 "("
+NAME@39:42 "url"
+COLON@42:43 ":"
+WHITESPACE@43:44 " "
+NAME@44:50 "String"
+WHITESPACE@50:51 " "
+EQ@51:52 "="
+WHITESPACE@52:53 " "
+R_CURLY@111:112 "}"
+EOF@112:112
+ERROR@53:111 "unterminated string value" "https://spec.graphql.org/October2021/#DefaultValue): Url}
diff --git a/crates/apollo-parser/test_data/parser/err/0031_argument_with_erronous_string_value.txt b/crates/apollo-parser/test_data/parser/err/0031_argument_with_erronous_string_value.txt
index 16d01d5..d41a60e 100644
--- a/crates/apollo-parser/test_data/parser/err/0031_argument_with_erronous_string_value.txt
+++ b/crates/apollo-parser/test_data/parser/err/0031_argument_with_erronous_string_value.txt
@@ -17,5 +17,5 @@
                     - WHITESPACE@14..15 "\n"
             - R_CURLY@15..16 "}"
 - ERROR@13:25 "unexpected escaped character" "\string ID"
-- ERROR@25:26 "expected a valid Value got RParen" )
+- ERROR@25:26 "expected a valid Value" )
 recursion limit: 4096, high: 2
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/parser/err/0033_directive_with_erronous_string_value.txt b/crates/apollo-parser/test_data/parser/err/0033_directive_with_erronous_string_value.txt
index 269e335..adb61ad 100644
--- a/crates/apollo-parser/test_data/parser/err/0033_directive_with_erronous_string_value.txt
+++ b/crates/apollo-parser/test_data/parser/err/0033_directive_with_erronous_string_value.txt
@@ -67,5 +67,5 @@
                     - WHITESPACE@117..118 "\n"
             - R_CURLY@118..119 "}"
 - ERROR@105:128 "unexpected escaped character" "\ errronous string \""
-- ERROR@128:129 "expected a valid Value got RParen" )
+- ERROR@128:129 "expected a valid Value" )
 recursion limit: 4096, high: 0
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/parser/err/0034_directive_with_unfinished_string_value.graphql b/crates/apollo-parser/test_data/parser/err/0034_unterminated_string_value_in_list.graphql
similarity index 100%
rename from crates/apollo-parser/test_data/parser/err/0034_directive_with_unfinished_string_value.graphql
rename to crates/apollo-parser/test_data/parser/err/0034_unterminated_string_value_in_list.graphql
diff --git a/crates/apollo-parser/test_data/parser/err/0034_directive_with_unfinished_string_value.txt b/crates/apollo-parser/test_data/parser/err/0034_unterminated_string_value_in_list.txt
similarity index 94%
rename from crates/apollo-parser/test_data/parser/err/0034_directive_with_unfinished_string_value.txt
rename to crates/apollo-parser/test_data/parser/err/0034_unterminated_string_value_in_list.txt
index 94dde27..6e29459 100644
--- a/crates/apollo-parser/test_data/parser/err/0034_directive_with_unfinished_string_value.txt
+++ b/crates/apollo-parser/test_data/parser/err/0034_unterminated_string_value_in_list.txt
@@ -32,7 +32,7 @@
                                 - COMMA@94..95 ","
                                 - WHITESPACE@95..96 " "
                                 - WHITESPACE@96..97 "\n"
-- ERROR@96:236 "expected a closing \" for the string value" "@external])
+- ERROR@96:236 "unterminated string value" "@external])
 
 type Vehicle @key(fields: "id") {
   id: ID!,
diff --git a/crates/apollo-parser/test_data/parser/err/0035_unterminated_string_value_in_object_value.graphql b/crates/apollo-parser/test_data/parser/err/0035_unterminated_string_value_in_object_value.graphql
new file mode 100644
index 0000000..cda40ac
--- /dev/null
+++ b/crates/apollo-parser/test_data/parser/err/0035_unterminated_string_value_in_object_value.graphql
@@ -0,0 +1,7 @@
+mutation {
+  createStore(draft: {
+    name: [{ locale: "en", value: "my store }]
+  }) {
+    name(locale: "en")
+  }
+}
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/parser/err/0035_unterminated_string_value_in_object_value.txt b/crates/apollo-parser/test_data/parser/err/0035_unterminated_string_value_in_object_value.txt
new file mode 100644
index 0000000..6d676d8
--- /dev/null
+++ b/crates/apollo-parser/test_data/parser/err/0035_unterminated_string_value_in_object_value.txt
@@ -0,0 +1,55 @@
+- DOCUMENT@0..68
+    - OPERATION_DEFINITION@0..68
+        - OPERATION_TYPE@0..9
+            - mutation_KW@0..8 "mutation"
+            - WHITESPACE@8..9 " "
+        - SELECTION_SET@9..68
+            - L_CURLY@9..10 "{"
+            - WHITESPACE@10..13 "\n  "
+            - FIELD@13..68
+                - NAME@13..24
+                    - IDENT@13..24 "createStore"
+                - ARGUMENTS@24..68
+                    - L_PAREN@24..25 "("
+                    - ARGUMENT@25..68
+                        - NAME@25..30
+                            - IDENT@25..30 "draft"
+                        - COLON@30..31 ":"
+                        - WHITESPACE@31..32 " "
+                        - OBJECT_VALUE@32..68
+                            - L_CURLY@32..33 "{"
+                            - WHITESPACE@33..38 "\n    "
+                            - OBJECT_FIELD@38..68
+                                - NAME@38..42
+                                    - IDENT@38..42 "name"
+                                - COLON@42..43 ":"
+                                - WHITESPACE@43..44 " "
+                                - LIST_VALUE@44..68
+                                    - L_BRACK@44..45 "["
+                                    - OBJECT_VALUE@45..68
+                                        - L_CURLY@45..46 "{"
+                                        - WHITESPACE@46..47 " "
+                                        - OBJECT_FIELD@47..61
+                                            - NAME@47..53
+                                                - IDENT@47..53 "locale"
+                                            - COLON@53..54 ":"
+                                            - WHITESPACE@54..55 " "
+                                            - STRING_VALUE@55..61
+                                                - STRING@55..59 "\"en\""
+                                                - COMMA@59..60 ","
+                                                - WHITESPACE@60..61 " "
+                                        - OBJECT_FIELD@61..68
+                                            - NAME@61..66
+                                                - IDENT@61..66 "value"
+                                            - COLON@66..67 ":"
+                                            - WHITESPACE@67..68 " "
+- ERROR@68:115 "unterminated string value" "my store }]  }) {
+    name(locale: "en")
+  }
+}
+- ERROR@115:116 "expected a valid Value" }
+- ERROR@116:116 "expected }" EOF
+- ERROR@116:116 "expected }" EOF
+- ERROR@116:116 "expected R_PAREN, got EOF" EOF
+- ERROR@116:116 "expected R_CURLY, got EOF" EOF
+recursion limit: 4096, high: 2
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/parser/err/0036_unterminated_string_in_default_value.graphql b/crates/apollo-parser/test_data/parser/err/0036_unterminated_string_in_default_value.graphql
new file mode 100644
index 0000000..d50eb37
--- /dev/null
+++ b/crates/apollo-parser/test_data/parser/err/0036_unterminated_string_in_default_value.graphql
@@ -0,0 +1,4 @@
+type Person {
+  name: String
+  picture(url: String = "https://spec.graphql.org/October2021/#DefaultValue): Url
+}
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/parser/err/0036_unterminated_string_in_default_value.txt b/crates/apollo-parser/test_data/parser/err/0036_unterminated_string_in_default_value.txt
new file mode 100644
index 0000000..473909a
--- /dev/null
+++ b/crates/apollo-parser/test_data/parser/err/0036_unterminated_string_in_default_value.txt
@@ -0,0 +1,42 @@
+- DOCUMENT@0..54
+    - OBJECT_TYPE_DEFINITION@0..54
+        - type_KW@0..4 "type"
+        - WHITESPACE@4..5 " "
+        - NAME@5..12
+            - IDENT@5..11 "Person"
+            - WHITESPACE@11..12 " "
+        - FIELDS_DEFINITION@12..54
+            - L_CURLY@12..13 "{"
+            - WHITESPACE@13..16 "\n  "
+            - FIELD_DEFINITION@16..31
+                - NAME@16..20
+                    - IDENT@16..20 "name"
+                - COLON@20..21 ":"
+                - WHITESPACE@21..22 " "
+                - NAMED_TYPE@22..28
+                    - NAME@22..28
+                        - IDENT@22..28 "String"
+                - WHITESPACE@28..31 "\n  "
+            - FIELD_DEFINITION@31..53
+                - NAME@31..38
+                    - IDENT@31..38 "picture"
+                - ARGUMENTS_DEFINITION@38..53
+                    - L_PAREN@38..39 "("
+                    - INPUT_VALUE_DEFINITION@39..53
+                        - NAME@39..42
+                            - IDENT@39..42 "url"
+                        - COLON@42..43 ":"
+                        - WHITESPACE@43..44 " "
+                        - NAMED_TYPE@44..50
+                            - NAME@44..50
+                                - IDENT@44..50 "String"
+                        - WHITESPACE@50..51 " "
+                        - DEFAULT_VALUE@51..53
+                            - EQ@51..52 "="
+                            - WHITESPACE@52..53 " "
+            - R_CURLY@53..54 "}"
+- ERROR@53:111 "unterminated string value" "https://spec.graphql.org/October2021/#DefaultValue): Url}
+- ERROR@111:112 "expected a valid Value" }
+- ERROR@111:112 "expected R_PAREN, got }" }
+- ERROR@111:112 "expected a type" }
+recursion limit: 4096, high: 0
\ No newline at end of file