From 9f82aceb9fbdb42f332d68f4a423123bd0788b2c Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sat, 17 Dec 2016 18:34:47 -0800 Subject: refactor modelica parser into separate crate --- modelica-parser-lalrpop/tests/ast.rs | 56 +++++++++++++++++++++++++++++++++ modelica-parser-lalrpop/tests/parser.rs | 40 +++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 modelica-parser-lalrpop/tests/ast.rs create mode 100644 modelica-parser-lalrpop/tests/parser.rs (limited to 'modelica-parser-lalrpop/tests') diff --git a/modelica-parser-lalrpop/tests/ast.rs b/modelica-parser-lalrpop/tests/ast.rs new file mode 100644 index 0000000..f9047cb --- /dev/null +++ b/modelica-parser-lalrpop/tests/ast.rs @@ -0,0 +1,56 @@ + +extern crate modelica_parser; + +use std::collections::HashSet; +use std::iter::FromIterator; +use modelica_parser::ast::*; + +fn set_eq(a: Vec, b: Vec) -> bool { + let set_a: HashSet = HashSet::from_iter(a); + let set_b: HashSet = HashSet::from_iter(b); + return set_a == set_b; +} + +#[test] +fn test_expr_identifiers() { + use modelica_parser::ast::Expr::*; + + assert!(set_eq( + vec![], + Integer(0).identifiers())); + assert!(set_eq( + vec!["x".to_string()], + Ident("x".to_string()).identifiers())); + assert!(set_eq( + vec!["x".to_string(), "y".to_string(), "z".to_string()], + BinExpr(BinOperator::Add, + Box::new(Abs(Box::new(Ident("z".to_string())))), + Box::new(BinExpr(BinOperator::Add, + Box::new(Abs(Box::new(Ident("x".to_string())))), + Box::new(Abs(Box::new(Ident("y".to_string()))))))).identifiers())); + assert!(set_eq( + vec!["z".to_string()], + BinExpr(BinOperator::Add, + Box::new(Ident("z".to_string())), + Box::new(Ident("z".to_string()))).identifiers())); +} + +#[test] +fn test_eqn_identifiers() { + use modelica_parser::ast::Expr::*; + + assert!(set_eq( + vec![], + SimpleEquation{ + lhs: Integer(0), + rhs: Integer(0), + }.identifiers())); + assert!(set_eq( + vec!["z".to_string()], + SimpleEquation{ + lhs: Ident("z".to_string()), + rhs: BinExpr(BinOperator::Add, + Box::new(Ident("z".to_string())), + Box::new(Ident("z".to_string()))), + }.identifiers())); +} diff --git a/modelica-parser-lalrpop/tests/parser.rs b/modelica-parser-lalrpop/tests/parser.rs new file mode 100644 index 0000000..f5f493a --- /dev/null +++ b/modelica-parser-lalrpop/tests/parser.rs @@ -0,0 +1,40 @@ + +extern crate modelica_parser; + +use modelica_parser::parser::{parse_integer, parse_float, parse_model}; + +#[test] +fn test_lexical() { + assert_eq!(&format!("{:?}", parse_integer("+123").unwrap()), + "123"); + assert_eq!(&format!("{:?}", parse_integer("-9").unwrap()), + "-9"); + assert_eq!(&format!("{:?}", parse_float("-1.0e0").unwrap()), + "-1"); + assert_eq!(&format!("{:?}", parse_float("123.456").unwrap()), + "123.456"); +} + +#[test] +fn test_parse() { + let example1 = +r#"model MinimalModel + Real x; +equation + x = 1; +end MinimalModel; +"#; + assert_eq!(&format!("{:?}", parse_model(example1).unwrap()), example1); + + let example2 = +r#"model MinimalModel + parameter Real a; + Real b; +equation + connect(a, b); + a = 1; + b = ((abs(a) + 2) / 4); +end MinimalModel; +"#; + assert_eq!(&format!("{:?}", parse_model(example2).unwrap()), example2); +} -- cgit v1.2.3