-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add snapshot tests for compiler errors
Verify good compiler error output by creating several snapshot tests. Also, slightly improve some of the messages.
- Loading branch information
Showing
30 changed files
with
349 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::{ | ||
fmt::{self, Display}, | ||
fs, | ||
}; | ||
|
||
use insta::{assert_snapshot, glob}; | ||
use miette::{Diagnostic, MietteHandler, MietteHandlerOpts, NamedSource, Report, ReportHandler}; | ||
use stef_parser::Schema; | ||
|
||
#[test] | ||
fn compile_invalid_schema() { | ||
struct Wrapper<'a>(&'a MietteHandler, &'a dyn Diagnostic); | ||
|
||
impl<'a> Display for Wrapper<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.debug(self.1, f) | ||
} | ||
} | ||
|
||
let handler = MietteHandlerOpts::new() | ||
.color(false) | ||
.context_lines(3) | ||
.force_graphical(true) | ||
.terminal_links(false) | ||
.width(120) | ||
.build(); | ||
|
||
glob!("inputs/invalid/*.stef", |path| { | ||
let input = fs::read_to_string(path).unwrap(); | ||
let schema = Schema::parse(input.as_str()).unwrap(); | ||
let result = stef_compiler::validate_schema(&schema).unwrap_err(); | ||
let report = Report::new(result).with_source_code(NamedSource::new( | ||
path.file_name().unwrap().to_string_lossy(), | ||
input, | ||
)); | ||
|
||
assert_snapshot!( | ||
"error", | ||
Wrapper(&handler, &*report).to_string(), | ||
stringify!(stef_compiler::validate_schema(&schema).unwrap_err()) | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum Sample<T, T> { | ||
One @1, | ||
Two { value: T @1 } @2, | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/stef-compiler/tests/inputs/invalid/enum_gen_unused.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum Sample<T> { | ||
One @1, | ||
Two { value: string @1 } @2, | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/stef-compiler/tests/inputs/invalid/enum_named_dup_id.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
enum Sample { | ||
One @1, | ||
Two { | ||
field1: string @1, | ||
field2: string @1, | ||
} @2, | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/stef-compiler/tests/inputs/invalid/enum_named_dup_name.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
enum Sample { | ||
One @1, | ||
Two { | ||
field: string @1, | ||
field: string @2, | ||
} @2, | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/stef-compiler/tests/inputs/invalid/enum_unnamed_dup_id.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum Sample { | ||
One @1, | ||
Two(string @1, string @1) @2, | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/stef-compiler/tests/inputs/invalid/enum_variant_dup_id.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum Sample { | ||
One @1, | ||
Two @1, | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/stef-compiler/tests/inputs/invalid/enum_variant_dup_name.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum Sample { | ||
One @1, | ||
One @2, | ||
} |
3 changes: 3 additions & 0 deletions
3
crates/stef-compiler/tests/inputs/invalid/struct_gen_dup.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
struct Sample<T, T> { | ||
value: T @1, | ||
} |
3 changes: 3 additions & 0 deletions
3
crates/stef-compiler/tests/inputs/invalid/struct_gen_unused.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
struct Sample<T> { | ||
value: string @1, | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/stef-compiler/tests/inputs/invalid/struct_named_dup_id.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Sample { | ||
field1: string @1, | ||
field2: string @1, | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/stef-compiler/tests/inputs/invalid/struct_named_dup_name.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Sample { | ||
field: string @1, | ||
field: string @2, | ||
} |
1 change: 1 addition & 0 deletions
1
crates/stef-compiler/tests/inputs/invalid/struct_unnamed_dup_id.stef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
struct Sample(string @1, string @1) |
19 changes: 19 additions & 0 deletions
19
crates/stef-compiler/tests/snapshots/compiler__error@enum_gen_dup.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/enum_gen_dup.stef | ||
--- | ||
× invalid generic type found | ||
├─▶ duplicate generic type name found | ||
╰─▶ duplicate generic type name `T` | ||
╭─[enum_gen_dup.stef:1:1] | ||
1 │ enum Sample<T, T> { | ||
· ┬ ┬ | ||
· │ ╰── used here again | ||
· ╰── first declared here | ||
2 │ One @1, | ||
3 │ Two { value: T @1 } @2, | ||
4 │ } | ||
╰──── | ||
help: the names of each generic type must be unique | ||
|
18 changes: 18 additions & 0 deletions
18
crates/stef-compiler/tests/snapshots/compiler__error@enum_gen_unused.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/enum_gen_unused.stef | ||
--- | ||
× invalid generic type found | ||
├─▶ unused generic type argument found | ||
╰─▶ unused generic type argument `T` | ||
╭─[enum_gen_unused.stef:1:1] | ||
1 │ enum Sample<T> { | ||
· ┬ | ||
· ╰── declared here | ||
2 │ One @1, | ||
3 │ Two { value: string @1 } @2, | ||
4 │ } | ||
╰──── | ||
help: each declared generic must be used in some way | ||
|
23 changes: 23 additions & 0 deletions
23
crates/stef-compiler/tests/snapshots/compiler__error@enum_named_dup_id.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/enum_named_dup_id.stef | ||
--- | ||
× duplicate ID found | ||
├─▶ duplicate ID in a field | ||
╰─▶ duplicate ID 1 in field `field2`, already used in `field1` | ||
╭─[enum_named_dup_id.stef:1:1] | ||
1 │ enum Sample { | ||
2 │ One @1, | ||
3 │ Two { | ||
4 │ field1: string @1, | ||
· ─┬ | ||
· ╰── first declared here | ||
5 │ field2: string @1, | ||
· ─┬ | ||
· ╰── used here again | ||
6 │ } @2, | ||
7 │ } | ||
╰──── | ||
help: the IDs for each field must be unique | ||
|
23 changes: 23 additions & 0 deletions
23
crates/stef-compiler/tests/snapshots/compiler__error@enum_named_dup_name.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/enum_named_dup_name.stef | ||
--- | ||
× duplicate name found | ||
├─▶ duplicate name in a field | ||
╰─▶ duplicate field name `field` | ||
╭─[enum_named_dup_name.stef:1:1] | ||
1 │ enum Sample { | ||
2 │ One @1, | ||
3 │ Two { | ||
4 │ field: string @1, | ||
· ──┬── | ||
· ╰── first declared here | ||
5 │ field: string @2, | ||
· ──┬── | ||
· ╰── used here again | ||
6 │ } @2, | ||
7 │ } | ||
╰──── | ||
help: the names of each field must be unique | ||
|
19 changes: 19 additions & 0 deletions
19
crates/stef-compiler/tests/snapshots/compiler__error@enum_unnamed_dup_id.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/enum_unnamed_dup_id.stef | ||
--- | ||
× duplicate ID found | ||
├─▶ duplicate ID in a field | ||
╰─▶ duplicate ID 1 in position 2, already used at 1 | ||
╭─[enum_unnamed_dup_id.stef:1:1] | ||
1 │ enum Sample { | ||
2 │ One @1, | ||
3 │ Two(string @1, string @1) @2, | ||
· ─┬ ─┬ | ||
· │ ╰── used here again | ||
· ╰── first declared here | ||
4 │ } | ||
╰──── | ||
help: the IDs for each field must be unique | ||
|
20 changes: 20 additions & 0 deletions
20
crates/stef-compiler/tests/snapshots/compiler__error@enum_variant_dup_id.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/enum_variant_dup_id.stef | ||
--- | ||
× duplicate ID found | ||
├─▶ duplicate ID in an enum variant | ||
╰─▶ duplicate ID 1 in enum variant `Two`, already used in `One` | ||
╭─[enum_variant_dup_id.stef:1:1] | ||
1 │ enum Sample { | ||
2 │ One @1, | ||
· ─┬ | ||
· ╰── first declared here | ||
3 │ Two @1, | ||
· ─┬ | ||
· ╰── used here again | ||
4 │ } | ||
╰──── | ||
help: the IDs for each variant of an enum must be unique | ||
|
20 changes: 20 additions & 0 deletions
20
crates/stef-compiler/tests/snapshots/compiler__error@enum_variant_dup_name.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/enum_variant_dup_name.stef | ||
--- | ||
× duplicate name found | ||
├─▶ duplicate name in an enum variant | ||
╰─▶ duplicate variant name `One` in enum | ||
╭─[enum_variant_dup_name.stef:1:1] | ||
1 │ enum Sample { | ||
2 │ One @1, | ||
· ─┬─ | ||
· ╰── first declared here | ||
3 │ One @2, | ||
· ─┬─ | ||
· ╰── used here again | ||
4 │ } | ||
╰──── | ||
help: the names of each variant must be unique | ||
|
18 changes: 18 additions & 0 deletions
18
crates/stef-compiler/tests/snapshots/compiler__error@struct_gen_dup.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/struct_gen_dup.stef | ||
--- | ||
× invalid generic type found | ||
├─▶ duplicate generic type name found | ||
╰─▶ duplicate generic type name `T` | ||
╭─[struct_gen_dup.stef:1:1] | ||
1 │ struct Sample<T, T> { | ||
· ┬ ┬ | ||
· │ ╰── used here again | ||
· ╰── first declared here | ||
2 │ value: T @1, | ||
3 │ } | ||
╰──── | ||
help: the names of each generic type must be unique | ||
|
17 changes: 17 additions & 0 deletions
17
crates/stef-compiler/tests/snapshots/compiler__error@struct_gen_unused.stef.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
source: crates/stef-compiler/tests/compiler.rs | ||
expression: "stef_compiler :: validate_schema(& schema).unwrap_err()" | ||
input_file: crates/stef-compiler/tests/inputs/invalid/struct_gen_unused.stef | ||
--- | ||
× invalid generic type found | ||
├─▶ unused generic type argument found | ||
╰─▶ unused generic type argument `T` | ||
╭─[struct_gen_unused.stef:1:1] | ||
1 │ struct Sample<T> { | ||
· ┬ | ||
· ╰── declared here | ||
2 │ value: string @1, | ||
3 │ } | ||
╰──── | ||
help: each declared generic must be used in some way | ||
|
Oops, something went wrong.