Skip to content

Commit

Permalink
Add unit tests for import/export directives with guards
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarx committed Jan 7, 2025
1 parent bfbd6c3 commit bda2ca7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
47 changes: 46 additions & 1 deletion nemo/src/parser/ast/directive/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<Vec<_>>();
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(_));
}
}
}
}
}
48 changes: 46 additions & 2 deletions nemo/src/parser/ast/directive/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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::<Vec<_>>();
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(_));
}
}
}
}
}

0 comments on commit bda2ca7

Please sign in to comment.