Use error's description for snapshot assertions + sort reported expected tokens

Turns out the snapshot test would randomly fail because the expected
  tokens are a HashSet, which is unordered by construction. So, using
  the auto-derived `Debug` instance to compare it with snapshot would
  only succeed if the tokens happened to be in the same order in the
  HashSet, which only happens by accident.

Signed-off-by: KtorZ <matthias.benkort@gmail.com>
This commit is contained in:
KtorZ 2025-03-02 13:45:05 +01:00
parent eadbc60a72
commit 862fc490c1
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
9 changed files with 14 additions and 127 deletions

View File

@ -3,6 +3,7 @@ use crate::{
parser::token::Token, parser::token::Token,
}; };
use indoc::formatdoc; use indoc::formatdoc;
use itertools::Itertools;
use miette::Diagnostic; use miette::Diagnostic;
use owo_colors::{OwoColorize, Stream::Stdout}; use owo_colors::{OwoColorize, Stream::Stdout};
use std::collections::HashSet; use std::collections::HashSet;
@ -18,6 +19,7 @@ use std::collections::HashSet;
"I am looking for one of the following patterns:\n{}", "I am looking for one of the following patterns:\n{}",
expected expected
.iter() .iter()
.sorted()
.map(|x| format!( .map(|x| format!(
"→ {}", "→ {}",
x.to_aiken() x.to_aiken()
@ -320,7 +322,7 @@ fn fmt_unknown_curve(curve: &String, point: &Option<String>) -> String {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash, Diagnostic, thiserror::Error)] #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Diagnostic, thiserror::Error)]
pub enum Pattern { pub enum Pattern {
#[error("I found an unexpected char '{0:?}'.")] #[error("I found an unexpected char '{0:?}'.")]
#[diagnostic(help("Try removing it!"))] #[diagnostic(help("Try removing it!"))]

View File

@ -2,14 +2,4 @@
source: crates/aiken-lang/src/parser/expr/assignment.rs source: crates/aiken-lang/src/parser/expr/assignment.rs
description: "Invalid code (parse error):\n\nlet a =\n// foo\nlet b = 42\n" description: "Invalid code (parse error):\n\nlet a =\n// foo\nlet b = 42\n"
--- ---
[ I spotted an unfinished assignment.
ParseError {
kind: UnfinishedAssignmentRightHandSide,
span: 0..25,
while_parsing: None,
expected: {},
label: Some(
"invalid assignment right-hand side",
),
},
]

View File

@ -2,71 +2,4 @@
source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs source: crates/aiken-lang/src/parser/expr/fail_todo_trace.rs
description: "Invalid code (parse error):\n\nlet debug = fn() {\n trace \"foo\":\n}\n" description: "Invalid code (parse error):\n\nlet debug = fn() {\n trace \"foo\":\n}\n"
--- ---
[ I found an unexpected token '}'.
ParseError {
kind: Unexpected(
Token(
RightBrace,
),
),
span: 34..35,
while_parsing: None,
expected: {
Token(
If,
),
Token(
Expect,
),
Token(
Minus,
),
Token(
When,
),
Token(
And,
),
Token(
Or,
),
Token(
LeftParen,
),
Token(
Todo,
),
Token(
LeftSquare,
),
Token(
NewLineMinus,
),
Token(
Bang,
),
Token(
Fail,
),
Token(
NewLineLeftParen,
),
Token(
LeftBrace,
),
Token(
Trace,
),
Token(
Let,
),
Token(
Hash,
),
Token(
Fn,
),
},
label: None,
},
]

View File

@ -2,14 +2,4 @@
source: crates/aiken-lang/src/parser/expr/when/mod.rs source: crates/aiken-lang/src/parser/expr/when/mod.rs
description: "Invalid code (parse error):\n\nwhen a is {\n 2 if x > 1 -> 3\n _ -> 1\n}\n" description: "Invalid code (parse error):\n\nwhen a is {\n 2 if x > 1 -> 3\n _ -> 1\n}\n"
--- ---
[ I found a now-deprecated clause guard in a when/is expression.
ParseError {
kind: DeprecatedWhenClause,
span: 14..29,
while_parsing: None,
expected: {},
label: Some(
"deprecated",
),
},
]

View File

@ -2,14 +2,4 @@
source: crates/aiken-lang/src/parser/pattern/bytearray.rs source: crates/aiken-lang/src/parser/pattern/bytearray.rs
description: "Invalid code (parse error):\n\nwhen foo is {\n #<Bls12_381, G1>\"950dfd33da2682260c76038dfb8bad6e84ae9d599a3c151815945ac1e6ef6b1027cd917f3907479d20d636ce437a41f5\" -> False\n _ -> True\n}\n" description: "Invalid code (parse error):\n\nwhen foo is {\n #<Bls12_381, G1>\"950dfd33da2682260c76038dfb8bad6e84ae9d599a3c151815945ac1e6ef6b1027cd917f3907479d20d636ce437a41f5\" -> False\n _ -> True\n}\n"
--- ---
[ I choked on a curve point in a bytearray pattern.
ParseError {
kind: PatternMatchOnCurvePoint,
span: 18..132,
while_parsing: None,
expected: {},
label: Some(
"cannot pattern-match on curve point",
),
},
]

View File

@ -2,14 +2,4 @@
source: crates/aiken-lang/src/parser/pattern/bytearray.rs source: crates/aiken-lang/src/parser/pattern/bytearray.rs
description: "Invalid code (parse error):\n\nwhen foo is {\n #<Bls12_381, G2>\"b0629fa1158c2d23a10413fe91d381a84d25e31d041cd0377d25828498fd02011b35893938ced97535395e4815201e67108bcd4665e0db25d602d76fa791fab706c54abf5e1a9e44b4ac1e6badf3d2ac0328f5e30be341677c8bac5dda7682f1\" -> False\n _ -> True\n}\n" description: "Invalid code (parse error):\n\nwhen foo is {\n #<Bls12_381, G2>\"b0629fa1158c2d23a10413fe91d381a84d25e31d041cd0377d25828498fd02011b35893938ced97535395e4815201e67108bcd4665e0db25d602d76fa791fab706c54abf5e1a9e44b4ac1e6badf3d2ac0328f5e30be341677c8bac5dda7682f1\" -> False\n _ -> True\n}\n"
--- ---
[ I choked on a curve point in a bytearray pattern.
ParseError {
kind: PatternMatchOnCurvePoint,
span: 18..228,
while_parsing: None,
expected: {},
label: Some(
"cannot pattern-match on curve point",
),
},
]

View File

@ -2,14 +2,4 @@
source: crates/aiken-lang/src/parser/pattern/string.rs source: crates/aiken-lang/src/parser/pattern/string.rs
description: "Invalid code (parse error):\n\nwhen foo is {\n @\"foo\" -> True\n}\n" description: "Invalid code (parse error):\n\nwhen foo is {\n @\"foo\" -> True\n}\n"
--- ---
[ I refuse to cooperate and match a utf-8 string.
ParseError {
kind: PatternMatchOnString,
span: 16..22,
while_parsing: None,
expected: {},
label: Some(
"cannot pattern-match on string",
),
},
]

View File

@ -1,12 +1,14 @@
use std::fmt; use std::fmt;
#[derive(Clone, Debug, PartialEq, Hash, Eq, Copy, serde::Serialize, serde::Deserialize)] #[derive(
Clone, Debug, PartialEq, PartialOrd, Ord, Hash, Eq, Copy, serde::Serialize, serde::Deserialize,
)]
pub enum Base { pub enum Base {
Decimal { numeric_underscore: bool }, Decimal { numeric_underscore: bool },
Hexadecimal, Hexadecimal,
} }
#[derive(Clone, Debug, PartialEq, Hash, Eq)] #[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Hash, Eq)]
pub enum Token { pub enum Token {
Error(char), Error(char),
Name { name: String }, Name { name: String },

View File

@ -45,7 +45,7 @@ macro_rules! assert_expr {
prepend_module_to_snapshot => false, prepend_module_to_snapshot => false,
omit_expression => true omit_expression => true
}, { }, {
insta::assert_debug_snapshot!(err); insta::assert_snapshot!(err.into_iter().map(|e| e.to_string()).collect::<Vec<_>>().join("\n"));
}) })
} }
} }