use super::*;
use crate::lexer::Lexer;
fn parse_input(input: &str) -> Result<Program, Box<CompileError>> {
let mut lexer = Lexer::new(input);
let tokens = lexer.tokenize();
let mut parser = Parser::new(tokens);
parser.parse()
}
#[test]
fn the_quoted_possessive_elapsed_seconds_parses() {
let input = "Print the 'job timer''s elapsed seconds.";
let result = parse_input(input).expect("elapsed seconds after 'the ...'s' should parse");
assert_eq!(result.statements.len(), 1);
match &result.statements[0] {
Statement::Print {
value: Expr::DurationCast { value, unit },
..
} => {
assert!(matches!(
value.as_ref(),
Expr::PropertyAccess { object, property: ObjectProperty::Elapsed }
if object == "job timer"
));
assert!(matches!(unit, ast::TimeUnit::Seconds));
}
other => panic!("Expected Print of a DurationCast, got {:?}", other),
}
}
#[test]
fn the_quoted_possessive_duration_in_seconds_parses() {
let input = "Print the 'job timer''s duration in seconds.";
let result = parse_input(input).expect("duration in seconds after 'the ...'s' should parse");
match &result.statements[0] {
Statement::Print {
value: Expr::DurationCast { value, unit },
..
} => {
assert!(matches!(
value.as_ref(),
Expr::PropertyAccess { property: ObjectProperty::Duration, .. }
));
assert!(matches!(unit, ast::TimeUnit::Seconds));
}
other => panic!("Expected Print of a DurationCast, got {:?}", other),
}
}
#[test]
fn the_quoted_possessive_single_word_property_still_parses() {
let input = "Print the 'job timer''s 'start time'.";
let result = parse_input(input).expect("single-word property should still parse");
match &result.statements[0] {
Statement::Print {
value: Expr::PropertyAccess { object, property: ObjectProperty::StartTime },
..
} => {
assert_eq!(object, "job timer");
}
other => panic!("Expected Print of a PropertyAccess, got {:?}", other),
}
}