Add some basic unit tests

This commit is contained in:
Tiffany Bennett 2016-08-22 19:20:49 -04:00
parent b4f3d02539
commit b2a87e8dc6
2 changed files with 46 additions and 0 deletions

View file

@ -522,3 +522,26 @@ pub fn parse_expr(mut iter: &mut Iter) -> Expr {
_ => left
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn add_assoc() {
let res = parse_expr(&mut TokenIterator::new("a + b - c + d - e").peekable());
assert_eq!(res.to_string(), "(((a + b) - c) + d) - e");
}
#[test]
fn mul_assoc() {
let res = parse_expr(&mut TokenIterator::new("a b * c / d / e f g").peekable());
assert_eq!(res.to_string(), "((a b c / d) / e) f g");
}
#[test]
fn suffix_prec() {
let res = parse_expr(&mut TokenIterator::new("a b °C + x y °F").peekable());
assert_eq!(res.to_string(), "a b °C + x y °F");
}
}

23
tests/query.rs Normal file
View file

@ -0,0 +1,23 @@
extern crate rink;
use rink::*;
thread_local! {
static CONTEXT: Context = load().unwrap();
}
fn test(input: &str, output: &str) {
let mut iter = text_query::TokenIterator::new(input.trim()).peekable();
let expr = text_query::parse_expr(&mut iter);
CONTEXT.with(|ctx| {
let res = ctx.eval_outer(&expr);
assert_eq!(res.as_ref().map(|x| x.as_ref()), Ok(output));
});
}
#[test]
fn test_queries() {
test("watt", "Definition: watt = J / s = kg m^2 / s^3 (power)");
test("5 inch", "0.127 m (length)");
test("5 inch -> cm", "12.7 cm (length)");
}