From 53f703416730ed277cd1b76d886d4256a0063bcb Mon Sep 17 00:00:00 2001 From: bnewbold Date: Wed, 30 Nov 2016 21:44:52 -0800 Subject: move tests from in-file to separate dir --- src/lib.rs | 64 ----------------------------------------------- src/modelica_ast.rs | 52 +------------------------------------- tests/lib.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/modelica_ast.rs | 56 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 115 deletions(-) create mode 100644 tests/lib.rs create mode 100644 tests/modelica_ast.rs diff --git a/src/lib.rs b/src/lib.rs index e38d10f..7020a9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -182,67 +182,3 @@ pub fn pp_parseerror(raw: &str, pe: ParseError) -> Str format!("{} {:?}", "parse error:".red().bold(), pe) }, } } - -/* ******************************** Tests ******************************* */ - -#[test] -fn test_parse_metadata() { - let raw = -r#" -[model] -name-en = "Bogus Dummy Model" -[variables] -"#.to_string(); - assert_eq!(parse_metadata(raw).unwrap(), - ModelMetadata { - name_en: "Bogus Dummy Model".to_string(), - description_en: None, - vars: vec![], - }); -} - -#[test] -fn test_load_model_entry() { - load_model_entry(Path::new("./examples/classic_gravitation/")).unwrap(); -} - -#[test] -fn test_search_models() { - assert_eq!(search_models(Path::new("./examples/")).len() > 1, true); -} - -#[test] -fn test_lexical() { - assert_eq!(&format!("{:?}", modelica_parser::parse_integer("+123").unwrap()), - "123"); - assert_eq!(&format!("{:?}", modelica_parser::parse_integer("-9").unwrap()), - "-9"); - assert_eq!(&format!("{:?}", modelica_parser::parse_float("-1.0e0").unwrap()), - "-1"); - assert_eq!(&format!("{:?}", modelica_parser::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!("{:?}", modelica_parser::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!("{:?}", modelica_parser::parse_model(example2).unwrap()), example2); -} diff --git a/src/modelica_ast.rs b/src/modelica_ast.rs index d93d81d..6fd29f8 100644 --- a/src/modelica_ast.rs +++ b/src/modelica_ast.rs @@ -1,6 +1,6 @@ use std::fmt::{Debug, Formatter, Error}; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; #[derive(PartialEq)] pub struct ModelicaModel { @@ -129,37 +129,6 @@ impl Expr { } } -#[cfg(test)] -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 self::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())); -} - impl SimpleEquation { // Order is undefined @@ -168,25 +137,6 @@ impl SimpleEquation { } } -#[test] -fn test_eqn_identifiers() { - use self::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())); -} //// Debug Implementations diff --git a/tests/lib.rs b/tests/lib.rs new file mode 100644 index 0000000..baddf72 --- /dev/null +++ b/tests/lib.rs @@ -0,0 +1,69 @@ + +extern crate modelthing; + +use std::path::Path; + +use modelthing::*; + + +#[test] +fn test_parse_metadata() { + let raw = +r#" +[model] +name-en = "Bogus Dummy Model" +[variables] +"#.to_string(); + assert_eq!(parse_metadata(raw).unwrap(), + ModelMetadata { + name_en: "Bogus Dummy Model".to_string(), + description_en: None, + vars: vec![], + }); +} + +#[test] +fn test_load_model_entry() { + load_model_entry(Path::new("./examples/classic_gravitation/")).unwrap(); +} + +#[test] +fn test_search_models() { + assert_eq!(search_models(Path::new("./examples/")).len() > 1, true); +} + +#[test] +fn test_lexical() { + assert_eq!(&format!("{:?}", modelica_parser::parse_integer("+123").unwrap()), + "123"); + assert_eq!(&format!("{:?}", modelica_parser::parse_integer("-9").unwrap()), + "-9"); + assert_eq!(&format!("{:?}", modelica_parser::parse_float("-1.0e0").unwrap()), + "-1"); + assert_eq!(&format!("{:?}", modelica_parser::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!("{:?}", modelica_parser::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!("{:?}", modelica_parser::parse_model(example2).unwrap()), example2); +} diff --git a/tests/modelica_ast.rs b/tests/modelica_ast.rs new file mode 100644 index 0000000..b6233f3 --- /dev/null +++ b/tests/modelica_ast.rs @@ -0,0 +1,56 @@ + +extern crate modelthing; + +use std::collections::HashSet; +use std::iter::FromIterator; +use modelthing::modelica_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 modelthing::modelica_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 modelthing::modelica_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())); +} -- cgit v1.2.3