diff --git a/rust/kcl-lib/src/simulation_tests.rs b/rust/kcl-lib/src/simulation_tests.rs index be5c95078..1f48a0a0f 100644 --- a/rust/kcl-lib/src/simulation_tests.rs +++ b/rust/kcl-lib/src/simulation_tests.rs @@ -996,6 +996,27 @@ mod import_cycle1 { super::execute(TEST_NAME, false).await } } +mod import_only_at_top_level { + const TEST_NAME: &str = "import_only_at_top_level"; + + /// Test parsing KCL. + #[test] + fn parse() { + super::parse(TEST_NAME) + } + + /// Test that parsing and unparsing KCL produces the original KCL input. + #[tokio::test(flavor = "multi_thread")] + async fn unparse() { + super::unparse(TEST_NAME).await + } + + /// Test that KCL is executed correctly. + #[tokio::test(flavor = "multi_thread")] + async fn kcl_test_execute() { + super::execute(TEST_NAME, false).await + } +} mod import_function_not_sketch { const TEST_NAME: &str = "import_function_not_sketch"; diff --git a/rust/kcl-lib/tests/import_only_at_top_level/artifact_commands.snap b/rust/kcl-lib/tests/import_only_at_top_level/artifact_commands.snap new file mode 100644 index 000000000..fab6bebbc --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/artifact_commands.snap @@ -0,0 +1,32 @@ +--- +source: kcl-lib/src/simulation_tests.rs +description: Artifact commands import_only_at_top_level.kcl +--- +[ + { + "cmdId": "[uuid]", + "range": [], + "command": { + "type": "edge_lines_visible", + "hidden": false + } + }, + { + "cmdId": "[uuid]", + "range": [], + "command": { + "type": "object_visible", + "object_id": "[uuid]", + "hidden": true + } + }, + { + "cmdId": "[uuid]", + "range": [], + "command": { + "type": "object_visible", + "object_id": "[uuid]", + "hidden": true + } + } +] diff --git a/rust/kcl-lib/tests/import_only_at_top_level/artifact_graph_flowchart.snap b/rust/kcl-lib/tests/import_only_at_top_level/artifact_graph_flowchart.snap new file mode 100644 index 000000000..fab9f0725 --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/artifact_graph_flowchart.snap @@ -0,0 +1,6 @@ +--- +source: kcl-lib/src/simulation_tests.rs +description: Artifact graph flowchart import_only_at_top_level.kcl +extension: md +snapshot_kind: binary +--- diff --git a/rust/kcl-lib/tests/import_only_at_top_level/artifact_graph_flowchart.snap.md b/rust/kcl-lib/tests/import_only_at_top_level/artifact_graph_flowchart.snap.md new file mode 100644 index 000000000..13e533509 --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/artifact_graph_flowchart.snap.md @@ -0,0 +1,3 @@ +```mermaid +flowchart LR +``` diff --git a/rust/kcl-lib/tests/import_only_at_top_level/ast.snap b/rust/kcl-lib/tests/import_only_at_top_level/ast.snap new file mode 100644 index 000000000..2c7d02eb3 --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/ast.snap @@ -0,0 +1,129 @@ +--- +source: kcl-lib/src/simulation_tests.rs +description: Result of parsing import_only_at_top_level.kcl +--- +{ + "Ok": { + "body": [ + { + "commentStart": 0, + "declaration": { + "commentStart": 0, + "end": 0, + "id": { + "commentStart": 0, + "end": 0, + "name": "main", + "start": 0, + "type": "Identifier" + }, + "init": { + "body": { + "body": [ + { + "commentStart": 0, + "end": 0, + "path": { + "type": "Kcl", + "filename": "empty.kcl" + }, + "selector": { + "type": "None", + "alias": null + }, + "start": 0, + "type": "ImportStatement", + "type": "ImportStatement" + }, + { + "argument": { + "commentStart": 0, + "end": 0, + "raw": "0", + "start": 0, + "type": "Literal", + "type": "Literal", + "value": { + "value": 0.0, + "suffix": "None" + } + }, + "commentStart": 0, + "end": 0, + "start": 0, + "type": "ReturnStatement", + "type": "ReturnStatement" + } + ], + "commentStart": 0, + "end": 0, + "start": 0 + }, + "commentStart": 0, + "end": 0, + "params": [], + "start": 0, + "type": "FunctionExpression", + "type": "FunctionExpression" + }, + "start": 0, + "type": "VariableDeclarator" + }, + "end": 0, + "kind": "fn", + "start": 0, + "type": "VariableDeclaration", + "type": "VariableDeclaration" + }, + { + "commentStart": 0, + "end": 0, + "expression": { + "callee": { + "abs_path": false, + "commentStart": 0, + "end": 0, + "name": { + "commentStart": 0, + "end": 0, + "name": "main", + "start": 0, + "type": "Identifier" + }, + "path": [], + "start": 0, + "type": "Name" + }, + "commentStart": 0, + "end": 0, + "start": 0, + "type": "CallExpressionKw", + "type": "CallExpressionKw", + "unlabeled": null + }, + "start": 0, + "type": "ExpressionStatement", + "type": "ExpressionStatement" + } + ], + "commentStart": 0, + "end": 0, + "nonCodeMeta": { + "nonCodeNodes": { + "0": [ + { + "commentStart": 0, + "end": 0, + "start": 0, + "type": "NonCodeNode", + "value": { + "type": "newLine" + } + } + ] + }, + "startNodes": [] + }, + "start": 0 + } +} diff --git a/rust/kcl-lib/tests/import_only_at_top_level/empty.kcl b/rust/kcl-lib/tests/import_only_at_top_level/empty.kcl new file mode 100644 index 000000000..e69de29bb diff --git a/rust/kcl-lib/tests/import_only_at_top_level/execution_error.snap b/rust/kcl-lib/tests/import_only_at_top_level/execution_error.snap new file mode 100644 index 000000000..c41163932 --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/execution_error.snap @@ -0,0 +1,30 @@ +--- +source: kcl-lib/src/simulation_tests.rs +description: Error from executing import_only_at_top_level.kcl +--- +KCL Semantic error + + × semantic: Imports are only supported at the top-level of a file. + ╭─[2:3] + 1 │ fn main() { + 2 │ import "empty.kcl" + · ─────────┬──────── + · ╰── tests/import_only_at_top_level/input.kcl + 3 │ return 0 + ╰──── + ╭─[6:1] + 5 │ + 6 │ main() + · ───┬── + · ╰── tests/import_only_at_top_level/input.kcl + ╰──── + ╰─▶ KCL Semantic error + + × semantic: Imports are only supported at the top-level of a file. + ╭─[2:3] + 1 │ fn main() { + 2 │ import "empty.kcl" + · ─────────┬──────── + · ╰── tests/import_only_at_top_level/input.kcl + 3 │ return 0 + ╰──── diff --git a/rust/kcl-lib/tests/import_only_at_top_level/input.kcl b/rust/kcl-lib/tests/import_only_at_top_level/input.kcl new file mode 100644 index 000000000..a131b64b2 --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/input.kcl @@ -0,0 +1,6 @@ +fn main() { + import "empty.kcl" + return 0 +} + +main() diff --git a/rust/kcl-lib/tests/import_only_at_top_level/ops.snap b/rust/kcl-lib/tests/import_only_at_top_level/ops.snap new file mode 100644 index 000000000..b6e2c52dc --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/ops.snap @@ -0,0 +1,32 @@ +--- +source: kcl-lib/src/simulation_tests.rs +description: Operations executed import_only_at_top_level.kcl +--- +[ + { + "type": "GroupBegin", + "group": { + "type": "ModuleInstance", + "name": "empty.kcl", + "moduleId": 0 + }, + "sourceRange": [] + }, + { + "type": "GroupBegin", + "group": { + "type": "FunctionCall", + "name": "main", + "functionSourceRange": [], + "unlabeledArg": null, + "labeledArgs": {} + }, + "sourceRange": [] + }, + { + "type": "GroupEnd" + }, + { + "type": "GroupEnd" + } +] diff --git a/rust/kcl-lib/tests/import_only_at_top_level/unparsed.snap b/rust/kcl-lib/tests/import_only_at_top_level/unparsed.snap new file mode 100644 index 000000000..cdf822e8c --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/unparsed.snap @@ -0,0 +1,10 @@ +--- +source: kcl-lib/src/simulation_tests.rs +description: Result of unparsing import_only_at_top_level.kcl +--- +fn main() { + import "empty.kcl" + return 0 +} + +main() diff --git a/rust/kcl-lib/tests/import_only_at_top_level/unparsed@empty.kcl.snap b/rust/kcl-lib/tests/import_only_at_top_level/unparsed@empty.kcl.snap new file mode 100644 index 000000000..b919ca2fe --- /dev/null +++ b/rust/kcl-lib/tests/import_only_at_top_level/unparsed@empty.kcl.snap @@ -0,0 +1,5 @@ +--- +source: kcl-lib/src/simulation_tests.rs +description: Result of unparsing tests/import_only_at_top_level/empty.kcl +--- +