From bda2ca7d7a04a79f1b856e1bb39a928a14f1dbbe Mon Sep 17 00:00:00 2001 From: Maximilian Marx Date: Tue, 7 Jan 2025 17:54:37 +0100 Subject: [PATCH] Add unit tests for import/export directives with guards --- nemo/src/parser/ast/directive/export.rs | 47 +++++++++++++++++++++++- nemo/src/parser/ast/directive/import.rs | 48 +++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/nemo/src/parser/ast/directive/export.rs b/nemo/src/parser/ast/directive/export.rs index cfe16edd..37fb1c84 100644 --- a/nemo/src/parser/ast/directive/export.rs +++ b/nemo/src/parser/ast/directive/export.rs @@ -116,10 +116,17 @@ impl<'a> ProgramAST<'a> for Export<'a> { #[cfg(test)] mod test { + use std::assert_matches::assert_matches; + use nom::combinator::all_consuming; use crate::parser::{ - ast::{directive::export::Export, ProgramAST}, + ast::{ + directive::export::Export, + expression::{complex::infix::InfixExpressionKind, Expression}, + guard::Guard, + ProgramAST, + }, input::ParserInput, ParserState, }; @@ -147,4 +154,42 @@ mod test { ); } } + + #[test] + fn parse_export_with_guards() { + let parser_input = ParserInput::new( + r#"@export predicate :- csv { resource = f"{?x}.{?y}" }, ?x = "test", ?y = "csv""#, + ParserState::default(), + ); + let result = all_consuming(Export::parse)(parser_input); + + assert!(result.is_ok()); + + let (_, result) = result.unwrap(); + + assert_eq!(result.predicate().to_string(), "predicate".to_string()); + assert_eq!( + result.instructions().tag().unwrap().to_string(), + "csv".to_string() + ); + + assert!(result.guards().is_some()); + + if let Some(sequence) = result.guards() { + let guards = sequence.iter().collect::>(); + assert_eq!(guards.len(), 2); + + for guard in guards { + assert_matches!(guard, Guard::Infix(_)); + + if let Guard::Infix(infix) = guard { + assert_eq!(infix.kind(), InfixExpressionKind::Equality); + let (left, right) = infix.pair(); + + assert_matches!(left, Expression::Variable(_)); + assert_matches!(right, Expression::String(_)); + } + } + } + } } diff --git a/nemo/src/parser/ast/directive/import.rs b/nemo/src/parser/ast/directive/import.rs index 1bfbe9c0..820ad09f 100644 --- a/nemo/src/parser/ast/directive/import.rs +++ b/nemo/src/parser/ast/directive/import.rs @@ -118,9 +118,15 @@ impl<'a> ProgramAST<'a> for Import<'a> { #[cfg(test)] mod test { use nom::combinator::all_consuming; + use std::assert_matches::assert_matches; use crate::parser::{ - ast::{directive::import::Import, ProgramAST}, + ast::{ + directive::import::Import, + expression::{complex::infix::InfixExpressionKind, Expression}, + guard::Guard, + ProgramAST, + }, input::ParserInput, ParserState, }; @@ -143,9 +149,47 @@ mod test { expected, ( result.1.predicate().to_string(), - result.1.instructions().tag().unwrap().to_string() + result.1.instructions().tag().unwrap().to_string(), ) ); } } + + #[test] + fn parse_import_with_guards() { + let parser_input = ParserInput::new( + r#"@import predicate :- csv { resource = f"{?x}.{?y}" }, ?x = "test", ?y = "csv""#, + ParserState::default(), + ); + let result = all_consuming(Import::parse)(parser_input); + + assert!(result.is_ok()); + + let (_, result) = result.unwrap(); + + assert_eq!(result.predicate().to_string(), "predicate".to_string()); + assert_eq!( + result.instructions().tag().unwrap().to_string(), + "csv".to_string() + ); + + assert!(result.guards().is_some()); + + if let Some(sequence) = result.guards() { + let guards = sequence.iter().collect::>(); + assert_eq!(guards.len(), 2); + + for guard in guards { + assert_matches!(guard, Guard::Infix(_)); + + if let Guard::Infix(infix) = guard { + assert_eq!(infix.kind(), InfixExpressionKind::Equality); + let (left, right) = infix.pair(); + + assert_matches!(left, Expression::Variable(_)); + assert_matches!(right, Expression::String(_)); + } + } + } + } }