From 8835d9eaf22fb3e71a4ad078a7ccf4fb5d924b09 Mon Sep 17 00:00:00 2001 From: John Doty Date: Sat, 6 Jan 2024 07:54:00 -0800 Subject: [PATCH] [fine] Tweak syntax for assertions to make me happier --- fine/build.rs | 6 +- fine/tests/README.md | 79 +++++++++++++++++++ fine/tests/expression/arithmetic.fine | 4 +- fine/tests/expression/boolean.fine | 4 +- fine/tests/expression/conditional.fine | 16 ++-- .../expression/errors/binary_mismatch.fine | 4 +- .../expression/errors/if_mismatched_arms.fine | 2 +- fine/tests/expression/errors/if_not_bool.fine | 2 +- .../expression/errors/if_requires_else.fine | 2 +- .../expression/errors/unary_mismatch.fine | 2 +- fine/tests/expression/number.fine | 4 +- fine/tests/expression/strings.fine | 4 +- 12 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 fine/tests/README.md diff --git a/fine/build.rs b/fine/build.rs index 24565124..78b94784 100644 --- a/fine/build.rs +++ b/fine/build.rs @@ -17,7 +17,7 @@ fn generate_test_for_file(path: PathBuf) -> String { }; let line = line.trim(); - if line == "concrete:" { + if line == "@concrete:" { let mut concrete = String::new(); while let Some(line) = lines.next() { let line = match line.strip_prefix("// | ") { @@ -32,7 +32,7 @@ fn generate_test_for_file(path: PathBuf) -> String { assertions.push(quote! { crate::assert_concrete(&_tree, #concrete, #display_path); }); - } else if let Some(line) = line.strip_prefix("type:") { + } else if let Some(line) = line.strip_prefix("@type:") { let (pos, expected) = line .trim() .split_once(' ') @@ -45,7 +45,7 @@ fn generate_test_for_file(path: PathBuf) -> String { assertions.push(quote! { crate::assert_type_at(&_tree, &_lines, #pos, #expected, #display_path); }); - } else if let Some(line) = line.strip_prefix("type-error:") { + } else if let Some(line) = line.strip_prefix("@type-error:") { let (pos, expected) = line .trim() .split_once(' ') diff --git a/fine/tests/README.md b/fine/tests/README.md new file mode 100644 index 00000000..62bedeec --- /dev/null +++ b/fine/tests/README.md @@ -0,0 +1,79 @@ +# Snapshot Tests + +The `.fine` files in this directory and its descendants are processed +by `build.rs` into a series of snapshot-tests. The various test +assertions are specified by `@` directives in comments in the file. + +e.g., a test might look like this: + +```fine +// @concrete: +// | File +// | ExpressionStatement +// | BinaryExpression +// | BinaryExpression +// | LiteralExpression +// | Number:'"1"' +// | Star:'"*"' +// | LiteralExpression +// | Number:'"2"' +// | Plus:'"+"' +// | BinaryExpression +// | UnaryExpression +// | Minus:'"-"' +// | LiteralExpression +// | Number:'"3"' +// | Star:'"*"' +// | LiteralExpression +// | Number:'"4"' +// | Semicolon:'";"' +// +1 * 2 + -3 * 4; + +// @type: 532 f64 +``` + +## Assertions + +The various assertions are as follows: + +- The `// @concrete:` assertion says that the following lines + (prefixed with `// | `, as above) describe the concrete syntax tree + of the file after parsing. + + e.g.: + + ```fine + // @concrete: + // | File + // | ExpressionStatement + // | LiteralExpression + // | String:'"\"Hello world\""' + // | Semicolon:'";"' + // + "Hello world"; + ``` + +- The `// @type:` assertion says that the type of the tree at the + given point will match the given type. `@type` assertions usually go + after the contents of the file to make the probe points more stable + in the face of new assertions and whatnot. + + e.g.: + + ```fine + "Hello world!" + // @type: 2 string + ``` + +- The `// @type-error:` assertion says that the type of the tree at + the given point should be an error, and that the error message + provided should be among the generated errors. (Like `@type`, these + usually go after the code, for stability.) + + e.g.: + + ```fine + - "twenty five"; + // @type-error: 0 cannot apply unary operator '-' to value of type string + ``` diff --git a/fine/tests/expression/arithmetic.fine b/fine/tests/expression/arithmetic.fine index f5ac71e3..610838d1 100644 --- a/fine/tests/expression/arithmetic.fine +++ b/fine/tests/expression/arithmetic.fine @@ -1,4 +1,4 @@ -// concrete: +// @concrete: // | File // | ExpressionStatement // | BinaryExpression @@ -21,4 +21,4 @@ // 1 * 2 + -3 * 4; -// type: 532 f64 \ No newline at end of file +// @type: 532 f64 \ No newline at end of file diff --git a/fine/tests/expression/boolean.fine b/fine/tests/expression/boolean.fine index 01f570f1..e3405c51 100644 --- a/fine/tests/expression/boolean.fine +++ b/fine/tests/expression/boolean.fine @@ -1,4 +1,4 @@ -// concrete: +// @concrete: // | File // | ExpressionStatement // | BinaryExpression @@ -21,4 +21,4 @@ // true and false or false and !true; -// type: 549 bool +// @type: 549 bool diff --git a/fine/tests/expression/conditional.fine b/fine/tests/expression/conditional.fine index a8c03cd7..c5383036 100644 --- a/fine/tests/expression/conditional.fine +++ b/fine/tests/expression/conditional.fine @@ -1,4 +1,4 @@ -// concrete: +// @concrete: // | File // | IfStatement // | ConditionalExpression @@ -27,18 +27,18 @@ if true { "discarded"; 23 } else { 45 } // Here come some type probes! // (type of the condition) -// type: 667 bool +// @type: 667 bool // // (the discarded expression) -// type: 674 string +// @type: 674 string // // (the "then" clause) -// type: 686 f64 -// type: 689 f64 +// @type: 686 f64 +// @type: 689 f64 // // (the "else" clause) -// type: 696 f64 -// type: 699 f64 +// @type: 696 f64 +// @type: 699 f64 // // (the overall expression) -// type: 664 f64 \ No newline at end of file +// @type: 664 f64 \ No newline at end of file diff --git a/fine/tests/expression/errors/binary_mismatch.fine b/fine/tests/expression/errors/binary_mismatch.fine index 9de7ab12..9f3e3629 100644 --- a/fine/tests/expression/errors/binary_mismatch.fine +++ b/fine/tests/expression/errors/binary_mismatch.fine @@ -1,4 +1,4 @@ 112 - "twenty five"; "twenty five" - 112; -// type-error: 4 cannot apply binary operator '-' to expressions of type 'f64' (on the left) and 'string' (on the right) -// type-error: 35 cannot apply binary operator '-' to expressions of type 'string' (on the left) and 'f64' (on the right) \ No newline at end of file +// @type-error: 4 cannot apply binary operator '-' to expressions of type 'f64' (on the left) and 'string' (on the right) +// @type-error: 35 cannot apply binary operator '-' to expressions of type 'string' (on the left) and 'f64' (on the right) \ No newline at end of file diff --git a/fine/tests/expression/errors/if_mismatched_arms.fine b/fine/tests/expression/errors/if_mismatched_arms.fine index 65dbf06b..101ca2c4 100644 --- a/fine/tests/expression/errors/if_mismatched_arms.fine +++ b/fine/tests/expression/errors/if_mismatched_arms.fine @@ -1,2 +1,2 @@ if true { "blarg" } else { 23 } -// type-error: 0 the type of the `then` branch (string) must match the type of the `else` branch (f64) +// @type-error: 0 the type of the `then` branch (string) must match the type of the `else` branch (f64) diff --git a/fine/tests/expression/errors/if_not_bool.fine b/fine/tests/expression/errors/if_not_bool.fine index 01d436af..770b28e1 100644 --- a/fine/tests/expression/errors/if_not_bool.fine +++ b/fine/tests/expression/errors/if_not_bool.fine @@ -1,2 +1,2 @@ if 23 { "what" } else { "the" } -// type-error: 0 conditions must yield a boolean +// @type-error: 0 conditions must yield a boolean diff --git a/fine/tests/expression/errors/if_requires_else.fine b/fine/tests/expression/errors/if_requires_else.fine index 274fcb10..90468291 100644 --- a/fine/tests/expression/errors/if_requires_else.fine +++ b/fine/tests/expression/errors/if_requires_else.fine @@ -1,2 +1,2 @@ if (if false { true }) { 32 } else { 23 } -// type-error: 4 the type of the `then` branch (bool) must match the type of the `else` branch (()) +// @type-error: 4 the type of the `then` branch (bool) must match the type of the `else` branch (()) diff --git a/fine/tests/expression/errors/unary_mismatch.fine b/fine/tests/expression/errors/unary_mismatch.fine index 1f8b79e0..9f9a4c87 100644 --- a/fine/tests/expression/errors/unary_mismatch.fine +++ b/fine/tests/expression/errors/unary_mismatch.fine @@ -1,2 +1,2 @@ - "twenty five"; -// type-error: 0 cannot apply unary operator '-' to value of type string +// @type-error: 0 cannot apply unary operator '-' to value of type string diff --git a/fine/tests/expression/number.fine b/fine/tests/expression/number.fine index ae01ea54..4995ea31 100644 --- a/fine/tests/expression/number.fine +++ b/fine/tests/expression/number.fine @@ -1,4 +1,4 @@ -// concrete: +// @concrete: // | File // | ExpressionStatement // | LiteralExpression @@ -7,4 +7,4 @@ 42; -// type: 129 f64 +// @type: 129 f64 diff --git a/fine/tests/expression/strings.fine b/fine/tests/expression/strings.fine index 808ff8ff..b179635f 100644 --- a/fine/tests/expression/strings.fine +++ b/fine/tests/expression/strings.fine @@ -1,4 +1,4 @@ -// concrete: +// @concrete: // | File // | ExpressionStatement // | BinaryExpression @@ -11,4 +11,4 @@ // "Hello " + 'world!'; -// type: 261 string \ No newline at end of file +// @type: 261 string \ No newline at end of file