[fine] Tweak syntax for assertions to make me happier

This commit is contained in:
John Doty 2024-01-06 07:54:00 -08:00
parent 758aef4db9
commit 8835d9eaf2
12 changed files with 104 additions and 25 deletions

View file

@ -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(' ')

79
fine/tests/README.md Normal file
View file

@ -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
```

View file

@ -1,4 +1,4 @@
// concrete:
// @concrete:
// | File
// | ExpressionStatement
// | BinaryExpression
@ -21,4 +21,4 @@
//
1 * 2 + -3 * 4;
// type: 532 f64
// @type: 532 f64

View file

@ -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

View file

@ -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
// @type: 664 f64

View file

@ -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)
// @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)

View file

@ -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)

View file

@ -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

View file

@ -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 (())

View file

@ -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

View file

@ -1,4 +1,4 @@
// concrete:
// @concrete:
// | File
// | ExpressionStatement
// | LiteralExpression
@ -7,4 +7,4 @@
42;
// type: 129 f64
// @type: 129 f64

View file

@ -1,4 +1,4 @@
// concrete:
// @concrete:
// | File
// | ExpressionStatement
// | BinaryExpression
@ -11,4 +11,4 @@
//
"Hello " + 'world!';
// type: 261 string
// @type: 261 string