fix: format snapshot tests

This commit is contained in:
rvcas 2024-08-08 12:56:01 -04:00 committed by KtorZ
parent 7f26db401c
commit 00907c2bcc
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
10 changed files with 119 additions and 72 deletions

View File

@ -232,7 +232,15 @@ impl<'comments> Formatter<'comments> {
return_annotation,
end_position,
..
}) => self.definition_fn(public, name, args, return_annotation, body, *end_position),
}) => self.definition_fn(
public,
name,
args,
return_annotation,
body,
*end_position,
false,
),
Definition::Validator(Validator {
end_position,
@ -513,12 +521,18 @@ impl<'comments> Formatter<'comments> {
return_annotation: &'a Option<Annotation>,
body: &'a UntypedExpr,
end_location: usize,
is_validator: bool,
) -> Document<'a> {
// Fn name and args
let head = pub_(*public)
.append("fn ")
.append(name)
.append(wrap_args(args.iter().map(|e| (self.fn_arg(e), false))));
let head = if !is_validator {
pub_(*public)
.append("fn ")
.append(name)
.append(wrap_args(args.iter().map(|e| (self.fn_arg(e), false))))
} else {
name.to_doc()
.append(wrap_args(args.iter().map(|e| (self.fn_arg(e), false))))
};
// Add return annotation
let head = match return_annotation {
@ -613,6 +627,7 @@ impl<'comments> Formatter<'comments> {
&handler.return_annotation,
&handler.body,
handler.end_position,
true,
)
.group();
@ -632,6 +647,7 @@ impl<'comments> Formatter<'comments> {
&fallback.return_annotation,
&fallback.body,
fallback.end_position,
true,
)
.group();

View File

@ -129,7 +129,7 @@ Validator(
},
doc: None,
location: 0..0,
name: "fallback",
name: "else",
public: true,
return_annotation: None,
return_type: (),

View File

@ -87,7 +87,7 @@ Validator(
},
doc: None,
location: 0..0,
name: "fallback",
name: "else",
public: true,
return_annotation: None,
return_type: (),

View File

@ -69,7 +69,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
doc: None,
location: ast::Span::empty(),
end_position: span.end - 1,
name: "fallback".to_string(),
name: "else".to_string(),
public: true,
return_annotation: None,
return_type: (),

View File

@ -111,8 +111,8 @@ fn bls12_381_ml_result_in_data_type() {
#[test]
fn validator_illegal_return_type() {
let source_code = r#"
validator {
fn foo(d, r, c) {
validator foo {
spend(d, r, c) {
1
}
}
@ -140,8 +140,8 @@ fn implicitly_discard_void() {
#[test]
fn validator_illegal_arity() {
let source_code = r#"
validator {
fn foo(c) {
validator foo {
mint(c) {
True
}
}
@ -318,8 +318,8 @@ fn mark_constructors_as_used_via_field_access() {
bar: Int,
}
validator {
fn foo(d: Datum, _r, _c) {
validator foo {
spend(d: Datum, _r, _c) {
when d is {
D0(params) -> params.foo == 1
D1(_params) -> False
@ -359,8 +359,8 @@ fn expect_multi_patterns() {
#[test]
fn validator_correct_form() {
let source_code = r#"
validator {
fn foo(d, r, c) {
validator foo {
spend(d, r, c) {
True
}
}
@ -372,8 +372,8 @@ fn validator_correct_form() {
#[test]
fn validator_in_lib_warning() {
let source_code = r#"
validator {
fn foo(c) {
validator foo {
spend(c) {
True
}
}
@ -390,12 +390,12 @@ fn validator_in_lib_warning() {
#[test]
fn multi_validator() {
let source_code = r#"
validator(foo: ByteArray, bar: Int) {
fn spend(_d, _r, _c) {
validator foo(foo: ByteArray, bar: Int) {
spend(_d, _r, _c) {
foo == #"aabb"
}
fn mint(_r, _c) {
mint(_r, _c) {
bar == 0
}
}
@ -409,12 +409,12 @@ fn multi_validator() {
#[test]
fn multi_validator_warning() {
let source_code = r#"
validator(foo: ByteArray, bar: Int) {
fn spend(_d, _r, _c) {
validator foo(foo: ByteArray, bar: Int) {
spend(_d, _r, _c) {
foo == #"aabb"
}
fn mint(_r, _c) {
mint(_r, _c) {
True
}
}
@ -460,7 +460,7 @@ fn exhaustiveness_simple() {
fn validator_args_no_annotation() {
let source_code = r#"
validator hello(d) {
foo (a, b, c) {
spend(a, b, c) {
True
}
}
@ -2472,8 +2472,8 @@ fn validator_private_type_leak() {
bar: Int,
}
validator {
pub fn bar(datum: Datum, redeemer: Redeemer, _ctx) {
validator bar {
spend(datum: Datum, redeemer: Redeemer, _ctx) {
datum.foo == redeemer.bar
}
}
@ -2496,8 +2496,8 @@ fn validator_public() {
bar: Int,
}
validator {
pub fn bar(datum: Datum, redeemer: Redeemer, _ctx) {
validator bar {
spend(datum: Datum, redeemer: Redeemer, _ctx) {
datum.foo == redeemer.bar
}
}
@ -2517,8 +2517,8 @@ fn validator_private_everything() {
bar: Int,
}
validator {
fn bar(datum: Datum, redeemer: Redeemer, _ctx) {
validator bar {
spend(datum: Datum, redeemer: Redeemer, _ctx) {
datum.foo == redeemer.bar
}
}

View File

@ -198,18 +198,18 @@ fn format_preserve_newline_after_bool_expect() {
fn format_validator() {
assert_format!(
r#"
validator ( ) {
validator thing ( ) {
// What is the purpose of life
fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
spend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True
}
}
// What?
validator {
validator foo {
/// Some documentation for foo
fn foo() {
foo() {
Void
}
@ -223,12 +223,12 @@ fn format_validator() {
fn format_double_validator() {
assert_format!(
r#"
validator ( param1 : ByteArray ) {
fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
validator foo( param1 : ByteArray ) {
spend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True
}
/// This is bar
fn bar(r: Redeemer, ctx : ScriptContext ) -> Bool { True }
mint(r: Redeemer, ctx : ScriptContext ) -> Bool { True }
}
"#
);
@ -238,12 +238,12 @@ fn format_double_validator() {
fn format_double_validator_public() {
assert_format!(
r#"
validator ( param1 : ByteArray ) {
pub fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
validator foo ( param1 : ByteArray ) {
spend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True
}
/// This is bar
pub fn bar(r: Redeemer, ctx : ScriptContext ) -> Bool { True }
mint(r: Redeemer, ctx : ScriptContext ) -> Bool { True }
}
"#
);
@ -967,20 +967,20 @@ fn format_anon_fn_pattern() {
fn format_validator_pattern() {
assert_format!(
r#"
validator(Foo { a, b, .. }) {
fn foo() { todo }
validator foo(Foo { a, b, .. }) {
spend() { todo }
}
validator([Bar] : List<Bar>) {
fn bar() { todo }
validator foo([Bar] : List<Bar>) {
spend() { todo }
}
validator((Baz, Baz) as x) {
fn baz() { todo }
validator foo((Baz, Baz) as x) {
mint() { todo }
}
validator((fst, snd) as x: Pair<Int, Int>) {
fn fiz() { todo }
validator fiz((fst, snd) as x: Pair<Int, Int>) {
spend() { todo }
}
"#
);

View File

@ -1,14 +1,18 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\n validator ( param1 : ByteArray ) {\n fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {\n True\n }\n /// This is bar\nfn bar(r: Redeemer, ctx : ScriptContext ) -> Bool { True }\n }\n"
description: "Code:\n\n validator foo( param1 : ByteArray ) {\n spend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {\n True\n }\n /// This is bar\nmint(r: Redeemer, ctx : ScriptContext ) -> Bool { True }\n }\n"
---
validator(param1: ByteArray) {
fn foo(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
validator foo(param1: ByteArray) {
spend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True
}
/// This is bar
fn bar(r: Redeemer, ctx: ScriptContext) -> Bool {
mint(r: Redeemer, ctx: ScriptContext) -> Bool {
True
}
else(_ctx) {
fail
}
}

View File

@ -1,14 +1,18 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\n validator ( param1 : ByteArray ) {\n pub fn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {\n True\n }\n /// This is bar\npub fn bar(r: Redeemer, ctx : ScriptContext ) -> Bool { True }\n }\n"
description: "Code:\n\n validator foo ( param1 : ByteArray ) {\n spend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {\n True\n }\n /// This is bar\nmint(r: Redeemer, ctx : ScriptContext ) -> Bool { True }\n }\n"
---
validator(param1: ByteArray) {
pub fn foo(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
validator foo(param1: ByteArray) {
spend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True
}
/// This is bar
pub fn bar(r: Redeemer, ctx: ScriptContext) -> Bool {
mint(r: Redeemer, ctx: ScriptContext) -> Bool {
True
}
else(_ctx) {
fail
}
}

View File

@ -1,21 +1,28 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\nvalidator ( ) {\n// What is the purpose of life\n\nfn foo (d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {\nTrue\n}\n}\n\n// What?\nvalidator {\n /// Some documentation for foo\n fn foo() {\n Void\n }\n\n // I am lost\n}\n"
description: "Code:\n\nvalidator thing ( ) {\n// What is the purpose of life\n\nspend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {\nTrue\n}\n}\n\n// What?\nvalidator foo {\n /// Some documentation for foo\n foo() {\n Void\n }\n\n // I am lost\n}\n"
---
validator {
validator thing {
// What is the purpose of life
fn foo(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
spend(d: Datum, r: Redeemer, ctx: ScriptContext) -> Bool {
True
}
else(_ctx) {
fail
}
}
// What?
validator {
validator foo {
/// Some documentation for foo
fn foo() {
foo() {
Void
}
// I am lost
else(_ctx) {
fail
// I am lost
}
}

View File

@ -1,27 +1,43 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\nvalidator(Foo { a, b, .. }) {\n fn foo() { todo }\n}\n\nvalidator([Bar] : List<Bar>) {\n fn bar() { todo }\n}\n\nvalidator((Baz, Baz) as x) {\n fn baz() { todo }\n}\n\nvalidator((fst, snd) as x: Pair<Int, Int>) {\n fn fiz() { todo }\n}\n"
description: "Code:\n\nvalidator foo(Foo { a, b, .. }) {\n spend() { todo }\n}\n\nvalidator foo([Bar] : List<Bar>) {\n spend() { todo }\n}\n\nvalidator foo((Baz, Baz) as x) {\n mint() { todo }\n}\n\nvalidator fiz((fst, snd) as x: Pair<Int, Int>) {\n spend() { todo }\n}\n"
---
validator(Foo { a, b, .. }) {
fn foo() {
validator foo(Foo { a, b, .. }) {
spend() {
todo
}
else(_ctx) {
fail
}
}
validator([Bar]: List<Bar>) {
fn bar() {
validator foo([Bar]: List<Bar>) {
spend() {
todo
}
else(_ctx) {
fail
}
}
validator((Baz, Baz) as x) {
fn baz() {
validator foo((Baz, Baz) as x) {
mint() {
todo
}
else(_ctx) {
fail
}
}
validator((fst, snd) as x: Pair<Int, Int>) {
fn fiz() {
validator fiz((fst, snd) as x: Pair<Int, Int>) {
spend() {
todo
}
else(_ctx) {
fail
}
}