test: assert_module

This commit is contained in:
rvcas 2023-06-30 17:21:03 -04:00
parent da0b969865
commit 715752718d
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 70 additions and 73 deletions

View File

@ -94,25 +94,37 @@ pub fn type_name_with_args() -> impl Parser<Token, (String, Option<Vec<String>>)
) )
} }
#[cfg(test)] #[macro_export]
#[macro_use] macro_rules! assert_expr {
mod macros { ($code:expr) => {
#[macro_export] let $crate::parser::lexer::LexInfo { tokens, .. } = $crate::parser::lexer::run($code).unwrap();
macro_rules! assert_expr {
($code:expr) => {
let $crate::parser::lexer::LexInfo { tokens, .. } = $crate::parser::lexer::run($code).unwrap();
let stream = chumsky::Stream::from_iter($crate::ast::Span::create(tokens.len()), tokens.into_iter()); let stream = chumsky::Stream::from_iter($crate::ast::Span::create(tokens.len()), tokens.into_iter());
let result = $crate::parser::expr::sequence().parse(stream).unwrap(); let result = $crate::parser::expr::sequence().parse(stream).unwrap();
insta::with_settings!({ insta::with_settings!({
description => concat!("Code:\n\n", $code), description => concat!("Code:\n\n", $code),
prepend_module_to_snapshot => false, prepend_module_to_snapshot => false,
omit_expression => true omit_expression => true
}, { }, {
insta::assert_debug_snapshot!(result); insta::assert_debug_snapshot!(result);
}); });
}; };
} }
#[macro_export]
macro_rules! assert_module {
($code:expr) => {
let (module, _) =
parser::module(indoc::indoc!{ $code }, ast::ModuleKind::Validator).expect("Failed to parse code");
insta::with_settings!({
description => concat!("Code:\n\n", indoc::indoc! { $code }),
prepend_module_to_snapshot => false,
omit_expression => true
}, {
insta::assert_debug_snapshot!(module);
});
};
} }

View File

@ -1,28 +1,13 @@
use crate::{ast, parser}; use crate::{assert_module, ast, parser};
macro_rules! assert_parse {
($code:expr) => {
let (module, _) =
parser::module(indoc::indoc!{ $code }, ast::ModuleKind::Validator).expect("Failed to parse code");
insta::with_settings!({
description => concat!("Code:\n\n", indoc::indoc! { $code }),
prepend_module_to_snapshot => false,
omit_expression => true
}, {
insta::assert_debug_snapshot!(module);
});
};
}
#[test] #[test]
fn windows_newline() { fn windows_newline() {
assert_parse!("use aiken/list\r\n"); assert_module!("use aiken/list\r\n");
} }
#[test] #[test]
fn can_handle_comments_at_end_of_file() { fn can_handle_comments_at_end_of_file() {
assert_parse!( assert_module!(
r#" r#"
use aiken use aiken
@ -33,7 +18,7 @@ fn can_handle_comments_at_end_of_file() {
#[test] #[test]
fn type_annotation_with_module_prefix() { fn type_annotation_with_module_prefix() {
assert_parse!( assert_module!(
r#" r#"
use aiken use aiken
@ -46,7 +31,7 @@ fn type_annotation_with_module_prefix() {
#[test] #[test]
fn test_fail() { fn test_fail() {
assert_parse!( assert_module!(
r#" r#"
!test invalid_inputs() { !test invalid_inputs() {
expect True = False expect True = False
@ -59,7 +44,7 @@ fn test_fail() {
#[test] #[test]
fn validator() { fn validator() {
assert_parse!( assert_module!(
r#" r#"
validator { validator {
fn foo(datum, rdmr, ctx) { fn foo(datum, rdmr, ctx) {
@ -72,7 +57,7 @@ fn validator() {
#[test] #[test]
fn double_validator() { fn double_validator() {
assert_parse!( assert_module!(
r#" r#"
validator { validator {
fn foo(datum, rdmr, ctx) { fn foo(datum, rdmr, ctx) {
@ -89,7 +74,7 @@ fn double_validator() {
#[test] #[test]
fn import() { fn import() {
assert_parse!( assert_module!(
r#" r#"
use std/list use std/list
"# "#
@ -98,7 +83,7 @@ fn import() {
#[test] #[test]
fn unqualified_imports() { fn unqualified_imports() {
assert_parse!( assert_module!(
r#" r#"
use std/address.{Address as A, thing as w} use std/address.{Address as A, thing as w}
"# "#
@ -107,7 +92,7 @@ fn unqualified_imports() {
#[test] #[test]
fn import_alias() { fn import_alias() {
assert_parse!( assert_module!(
r#" r#"
use std/tx as t use std/tx as t
"# "#
@ -116,7 +101,7 @@ fn import_alias() {
#[test] #[test]
fn custom_type() { fn custom_type() {
assert_parse!( assert_module!(
r#" r#"
type Option<a> { type Option<a> {
Some(a, Int) Some(a, Int)
@ -129,7 +114,7 @@ fn custom_type() {
#[test] #[test]
fn opaque_type() { fn opaque_type() {
assert_parse!( assert_module!(
r#" r#"
pub opaque type User { pub opaque type User {
name: _w name: _w
@ -140,7 +125,7 @@ fn opaque_type() {
#[test] #[test]
fn type_alias() { fn type_alias() {
assert_parse!( assert_module!(
r#" r#"
type Thing = Option<Int> type Thing = Option<Int>
"# "#
@ -149,7 +134,7 @@ fn type_alias() {
#[test] #[test]
fn pub_type_alias() { fn pub_type_alias() {
assert_parse!( assert_module!(
r#" r#"
pub type Me = Option<String> pub type Me = Option<String>
"# "#
@ -158,7 +143,7 @@ fn pub_type_alias() {
#[test] #[test]
fn empty_function() { fn empty_function() {
assert_parse!( assert_module!(
r#" r#"
pub fn run() {} pub fn run() {}
"# "#
@ -167,7 +152,7 @@ fn empty_function() {
#[test] #[test]
fn expect() { fn expect() {
assert_parse!( assert_module!(
r#" r#"
pub fn run() { pub fn run() {
expect Some(x) = something.field expect Some(x) = something.field
@ -179,7 +164,7 @@ fn expect() {
#[test] #[test]
fn plus_binop() { fn plus_binop() {
assert_parse!( assert_module!(
r#" r#"
pub fn add_one(a) -> Int { pub fn add_one(a) -> Int {
a + 1 a + 1
@ -190,7 +175,7 @@ fn plus_binop() {
#[test] #[test]
fn pipeline() { fn pipeline() {
assert_parse!( assert_module!(
r#" r#"
pub fn thing(thing a: Int) { pub fn thing(thing a: Int) {
a + 2 a + 2
@ -203,7 +188,7 @@ fn pipeline() {
#[test] #[test]
fn if_expression() { fn if_expression() {
assert_parse!( assert_module!(
r#" r#"
fn ifs() { fn ifs() {
if True { if True {
@ -222,7 +207,7 @@ fn if_expression() {
#[test] #[test]
fn let_bindings() { fn let_bindings() {
assert_parse!( assert_module!(
r#" r#"
pub fn wow(a: Int) { pub fn wow(a: Int) {
let x = let x =
@ -242,7 +227,7 @@ fn let_bindings() {
#[test] #[test]
fn block() { fn block() {
assert_parse!( assert_module!(
r#" r#"
pub fn wow2(a: Int){ pub fn wow2(a: Int){
let b = { let b = {
@ -259,7 +244,7 @@ fn block() {
#[test] #[test]
fn when() { fn when() {
assert_parse!( assert_module!(
r#" r#"
pub fn wow2(a: Int){ pub fn wow2(a: Int){
when a is { when a is {
@ -278,7 +263,7 @@ fn when() {
#[test] #[test]
fn anonymous_function() { fn anonymous_function() {
assert_parse!( assert_module!(
r#" r#"
pub fn such() -> Int { pub fn such() -> Int {
let add_one = fn (a: Int) -> Int { a + 1 } let add_one = fn (a: Int) -> Int { a + 1 }
@ -291,7 +276,7 @@ fn anonymous_function() {
#[test] #[test]
fn field_access() { fn field_access() {
assert_parse!( assert_module!(
r#" r#"
fn name(user: User) { fn name(user: User) {
user.name user.name
@ -302,7 +287,7 @@ fn field_access() {
#[test] #[test]
fn call() { fn call() {
assert_parse!( assert_module!(
r#" r#"
fn calls() { fn calls() {
let x = add_one(3) let x = add_one(3)
@ -317,7 +302,7 @@ fn call() {
#[test] #[test]
fn record_update() { fn record_update() {
assert_parse!( assert_module!(
r#" r#"
fn update_name(user: User, name: ByteArray) -> User { fn update_name(user: User, name: ByteArray) -> User {
User { ..user, name: "Aiken", age } User { ..user, name: "Aiken", age }
@ -328,7 +313,7 @@ fn record_update() {
#[test] #[test]
fn record_create_labeled() { fn record_create_labeled() {
assert_parse!( assert_module!(
r#" r#"
fn create() { fn create() {
User { name: "Aiken", age, thing: 2 } User { name: "Aiken", age, thing: 2 }
@ -339,7 +324,7 @@ fn record_create_labeled() {
#[test] #[test]
fn record_create_labeled_with_field_access() { fn record_create_labeled_with_field_access() {
assert_parse!( assert_module!(
r#" r#"
fn create() { fn create() {
some_module.User { name: "Aiken", age, thing: 2 } some_module.User { name: "Aiken", age, thing: 2 }
@ -350,7 +335,7 @@ fn record_create_labeled_with_field_access() {
#[test] #[test]
fn cargo_create_unlabeled() { fn cargo_create_unlabeled() {
assert_parse!( assert_module!(
r#" r#"
fn create() { fn create() {
some_module.Thing(1, a) some_module.Thing(1, a)
@ -361,7 +346,7 @@ fn cargo_create_unlabeled() {
#[test] #[test]
fn parse_tuple() { fn parse_tuple() {
assert_parse!( assert_module!(
r#" r#"
fn foo() { fn foo() {
let tuple = (1, 2, 3, 4) let tuple = (1, 2, 3, 4)
@ -373,7 +358,7 @@ fn parse_tuple() {
#[test] #[test]
fn parse_tuple2() { fn parse_tuple2() {
assert_parse!( assert_module!(
r#" r#"
fn foo() { fn foo() {
let a = foo(14) let a = foo(14)
@ -385,7 +370,7 @@ fn parse_tuple2() {
#[test] #[test]
fn plain_bytearray_literals() { fn plain_bytearray_literals() {
assert_parse!( assert_module!(
r#" r#"
pub const my_policy_id = #[0, 170, 255] pub const my_policy_id = #[0, 170, 255]
"# "#
@ -394,7 +379,7 @@ fn plain_bytearray_literals() {
#[test] #[test]
fn base16_bytearray_literals() { fn base16_bytearray_literals() {
assert_parse!( assert_module!(
r#" r#"
pub const my_policy_id = #"00aaff" pub const my_policy_id = #"00aaff"
@ -407,7 +392,7 @@ fn base16_bytearray_literals() {
#[test] #[test]
fn function_def() { fn function_def() {
assert_parse!( assert_module!(
r#" r#"
fn foo() {} fn foo() {}
"# "#
@ -416,7 +401,7 @@ fn function_def() {
#[test] #[test]
fn function_invoke() { fn function_invoke() {
assert_parse!( assert_module!(
r#" r#"
fn foo() { fn foo() {
let a = bar(42) let a = bar(42)
@ -427,7 +412,7 @@ fn function_invoke() {
#[test] #[test]
fn function_ambiguous_sequence() { fn function_ambiguous_sequence() {
assert_parse!( assert_module!(
r#" r#"
fn foo_1() { fn foo_1() {
let a = bar let a = bar
@ -453,7 +438,7 @@ fn function_ambiguous_sequence() {
#[test] #[test]
fn tuple_type_alias() { fn tuple_type_alias() {
assert_parse!( assert_module!(
r#" r#"
type RoyaltyToken = (PolicyId, AssetName) type RoyaltyToken = (PolicyId, AssetName)
"# "#
@ -462,7 +447,7 @@ fn tuple_type_alias() {
#[test] #[test]
fn int_parsing_hex_bytes() { fn int_parsing_hex_bytes() {
assert_parse!( assert_module!(
r#" r#"
fn foo() { fn foo() {
#[ 0x01, 0xa2, 0x03 ] #[ 0x01, 0xa2, 0x03 ]
@ -473,7 +458,7 @@ fn int_parsing_hex_bytes() {
#[test] #[test]
fn test_parsing_numeric_underscore() { fn test_parsing_numeric_underscore() {
assert_parse!( assert_module!(
r#" r#"
fn foo() { fn foo() {
let i = 1_234_567 let i = 1_234_567
@ -486,7 +471,7 @@ fn test_parsing_numeric_underscore() {
#[test] #[test]
fn first_class_binop() { fn first_class_binop() {
assert_parse!( assert_module!(
r#" r#"
fn foo() { fn foo() {
compare_with(a, >, b) compare_with(a, >, b)