diff --git a/ast/src/annotation.rs b/ast/src/annotation.rs index 50348b7dfe..83784bc2f2 100644 --- a/ast/src/annotation.rs +++ b/ast/src/annotation.rs @@ -36,7 +36,21 @@ pub fn load_annotation( match ast_definition { Definition::Import(_) => unimplemented!("annotated imports are not supported yet"), Definition::Circuit(_) => unimplemented!("annotated circuits are not supported yet"), - Definition::Function(_) => unimplemented!("annotated functions are not supported yet"), + // TODO need someone to take functions annotated with @test to be moved from function to tests. + Definition::Function(function) => match ast_annotation.name { + AnnotationName::Test(_) | AnnotationName::TestWithContext(_) => { + println!("here?"); + let ident = Identifier::from(function.identifier.clone()); + _functions.remove(&ident.clone()); + + let test_function = leo_grammar::functions::TestFunction::from(function); + let test = TestFunction::from(test_function); + tests.insert(ident, test.clone()); + + load_annotated_test(test, ast_annotation, tests); + } + _ => unimplemented!("annotated functions are not supported yet"), + }, Definition::TestFunction(ast_test) => { let test = TestFunction::from(ast_test); load_annotated_test(test, ast_annotation, tests) @@ -50,6 +64,8 @@ pub fn load_annotated_test(test: TestFunction, annotation: Annotation, tests: &m let ast_arguments = annotation.arguments; match name { + AnnotationName::Test(_) => (), + AnnotationName::TestWithContext(_) => load_annotated_test_context(test, ast_arguments, tests), AnnotationName::Context(_) => load_annotated_test_context(test, ast_arguments, tests), } } diff --git a/grammar/src/annotations/annotation_name.rs b/grammar/src/annotations/annotation_name.rs index 5ceb953ec2..475adb2870 100644 --- a/grammar/src/annotations/annotation_name.rs +++ b/grammar/src/annotations/annotation_name.rs @@ -20,10 +20,30 @@ use pest::Span; use pest_ast::FromPest; use serde::Serialize; +// TODO rename this to test for consistency? + #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[pest_ast(rule(Rule::annotation_name))] pub enum AnnotationName<'ast> { Context(Context<'ast>), + Test(Test<'ast>), + TestWithContext(TestWithContext<'ast>), +} + +#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] +#[pest_ast(rule(Rule::test))] +pub struct Test<'ast> { + #[pest_ast(outer())] + #[serde(with = "SpanDef")] + pub span: Span<'ast>, +} + +#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] +#[pest_ast(rule(Rule::test))] +pub struct TestWithContext<'ast> { + #[pest_ast(outer())] + #[serde(with = "SpanDef")] + pub span: Span<'ast>, } #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] diff --git a/grammar/src/functions/test_function.rs b/grammar/src/functions/test_function.rs index 54222deafd..44ca161606 100644 --- a/grammar/src/functions/test_function.rs +++ b/grammar/src/functions/test_function.rs @@ -28,3 +28,10 @@ pub struct TestFunction<'ast> { #[serde(with = "SpanDef")] pub span: Span<'ast>, } + +impl<'ast> From> for TestFunction<'ast> { + fn from(function: Function<'ast>) -> Self { + let span = function.span.clone(); + TestFunction { function, span } + } +} diff --git a/grammar/src/leo.pest b/grammar/src/leo.pest index 5f7e83dbf3..65df5752ee 100644 --- a/grammar/src/leo.pest +++ b/grammar/src/leo.pest @@ -514,7 +514,7 @@ formatted_container = { "{" ~ "}"} /// Annotations // Declared in annotations/annotation.rs -annotation = ${annotation_symbol ~ annotation_name ~ annotation_arguments} +annotation = ${annotation_symbol ~ annotation_name ~ annotation_arguments? } // Declared in annotations/annotation_symbol.rs annotation_symbol = ${"@"} @@ -522,10 +522,12 @@ annotation_symbol = ${"@"} // Declared in annotations/annotation_name.rs annotation_name = { context + | test } // Declared in annotations/context.rs context = {"context"} +test = {"test"} // Declared in annotations/annotation_argument.rs annotation_arguments = !{"(" ~ annotation_argument ~ ("," ~ annotation_argument)* ~ ","? ~ NEWLINE* ~ ")"}