From fbe29e1dc8e0355f6c46f334695a462739cf1abb Mon Sep 17 00:00:00 2001 From: "xiarui.xr" Date: Wed, 18 Oct 2023 12:08:22 +0800 Subject: [PATCH] test: add more test cases on format tool behavior Signed-off-by: xiarui.xr --- kclvm/ast_pretty/src/lib.rs | 2 +- .../different_stmts_line_breaks.golden | 8 ++++++++ .../different_stmts_line_breaks.input | 6 ++++++ .../format_data/list_dict_schema_expr.golden | 20 +++++++++++++++++++ .../format_data/list_dict_schema_expr.input | 20 +++++++++++++++++++ .../test_data/format_data/union_types.golden | 5 +++++ .../test_data/format_data/union_types.input | 5 +++++ kclvm/tools/src/format/tests.rs | 3 +++ 8 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.golden create mode 100644 kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.input create mode 100644 kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.golden create mode 100644 kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.input create mode 100644 kclvm/tools/src/format/test_data/format_data/union_types.golden create mode 100644 kclvm/tools/src/format/test_data/format_data/union_types.input diff --git a/kclvm/ast_pretty/src/lib.rs b/kclvm/ast_pretty/src/lib.rs index 980ba8550..ab5973b04 100644 --- a/kclvm/ast_pretty/src/lib.rs +++ b/kclvm/ast_pretty/src/lib.rs @@ -261,7 +261,7 @@ impl<'p> Printer<'p> { } } -/// Print AST to string +/// Print AST to string. The default format is according to the KCL code style defined here: https://kcl-lang.io/docs/reference/lang/spec/codestyle pub fn print_ast_module(module: &Module) -> String { let mut printer = Printer::default(); printer.write_module(module); diff --git a/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.golden b/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.golden new file mode 100644 index 000000000..1dc12573c --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.golden @@ -0,0 +1,8 @@ +import a + +schema A: + name: str = a.name + +A {} + +# Break one blank line between different statements e.g., import, schema and expression statements. diff --git a/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.input b/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.input new file mode 100644 index 000000000..92b107a1e --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.input @@ -0,0 +1,6 @@ +import a +schema A: + name: str = a.name +A{} + +# Break one blank line between different statements e.g., import, schema and expression statements. diff --git a/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.golden b/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.golden new file mode 100644 index 000000000..a71205411 --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.golden @@ -0,0 +1,20 @@ +dict = { + "key": "value" +} + +dict2 = {"key": "value"} + +list = [ + first + second +] + +list2 = [first, second] + +expr = Person { + name: "Alice" +} + +expr2 = Person {name: "Alice"} + +# It's both acceptable to write each entry in the list/dict/schema expr without line breaks, or separate each entry with one line break. diff --git a/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.input b/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.input new file mode 100644 index 000000000..a71205411 --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.input @@ -0,0 +1,20 @@ +dict = { + "key": "value" +} + +dict2 = {"key": "value"} + +list = [ + first + second +] + +list2 = [first, second] + +expr = Person { + name: "Alice" +} + +expr2 = Person {name: "Alice"} + +# It's both acceptable to write each entry in the list/dict/schema expr without line breaks, or separate each entry with one line break. diff --git a/kclvm/tools/src/format/test_data/format_data/union_types.golden b/kclvm/tools/src/format/test_data/format_data/union_types.golden new file mode 100644 index 000000000..86f5bc5d7 --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/union_types.golden @@ -0,0 +1,5 @@ +workloadType: "Deployment" | "StatefulSet" = "Deployment" +ServiceType: "LoadBalance" | "ClusterIP" = "LoadBalance" +abc: A | B | C = A {} + +# In union types, there should be one and only one whitespace both before and after the union operator | diff --git a/kclvm/tools/src/format/test_data/format_data/union_types.input b/kclvm/tools/src/format/test_data/format_data/union_types.input new file mode 100644 index 000000000..0dd6de4f9 --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/union_types.input @@ -0,0 +1,5 @@ +workloadType: "Deployment"|"StatefulSet" = "Deployment" +ServiceType: "LoadBalance" |"ClusterIP" = "LoadBalance" +abc: A|B|C = A{} + +# In union types, there should be one and only one whitespace both before and after the union operator | diff --git a/kclvm/tools/src/format/tests.rs b/kclvm/tools/src/format/tests.rs index d150f819a..0b483abfe 100644 --- a/kclvm/tools/src/format/tests.rs +++ b/kclvm/tools/src/format/tests.rs @@ -24,6 +24,9 @@ const TEST_CASES: &[&str; 18] = &[ "string", "type_alias", "unary", + // "union_types", + // "different_stmts_line_breaks", + // "list_dict_schema_expr", ]; fn read_data(data_name: &str) -> (String, String) {