From 9f05e2a7e7e6fbfbc0ffa587a3d17bf1c348c6e6 Mon Sep 17 00:00:00 2001 From: Caleb Jones Date: Wed, 14 Dec 2016 02:24:04 -0500 Subject: [PATCH] Add more tests --- src/lib.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 461a2e9..09508d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ pub enum Sexp { } pub fn parse(input: &str) -> Result { - match sexp(input) { + match do_parse!(input, exp: sexp >> opt!(multispace) >> eof!() >> (exp)) { IResult::Done(_, res) => Ok(res), _ => Err(()), } @@ -69,7 +69,6 @@ named!(string<&str, Atom>, tag_s!("\"") >> contents: take_until_s!("\"") >> tag_s!("\"") >> - opt!(multispace) >> (Atom::Str(contents.into())) ) ); @@ -146,6 +145,17 @@ fn test_parse_ident() { assert!(symbol("").is_incomplete()); } +#[cfg(test)] +#[test] +fn test_parse_string() { + assert_eq!(string(r#""hello""#), IResult::Done("", Atom::Str("hello".into()))); + assert_eq!(string(r#" "this is a nice string +with 0123 things in it""#), + IResult::Done("", Atom::Str("this is a nice string\nwith 0123 things in it".into()))); + + assert!(string(r#""hi"#).is_err()); +} + #[cfg(test)] #[test] fn test_parse_char() { @@ -156,3 +166,64 @@ fn test_parse_char() { assert!(character("#").is_incomplete()); assert!(character("a").is_err()); } + +#[cfg(test)] +#[test] +fn test_parse_list() { + assert_eq!(list("()"), IResult::Done("", vec![])); + assert_eq!(list("(1)"), IResult::Done("", vec![Sexp::Atom { atom: Atom::Int(1) }])); + assert_eq!(list(" ( 1 2 3 a )"), IResult::Done("", vec![ + Sexp::Atom { atom: Atom::Int(1) }, + Sexp::Atom { atom: Atom::Int(2) }, + Sexp::Atom { atom: Atom::Int(3) }, + Sexp::Atom { atom: Atom::Sym("a".into()) }, + ])); +} + +#[cfg(test)] +#[test] +fn test_cant_parse() { + assert!(parse("1 2").is_err()); +} + +#[cfg(test)] +#[test] +fn test_parse_expression() { + assert_eq!(parse(r#" +(def (main) + (print (str "say " #\" "Hello, World" #\" " today!"))) +"#), + Ok(Sexp::List { + list: vec![ + Sexp::Atom { atom: Atom::Sym("def".into()) }, + Sexp::List { + list: vec![ + Sexp::Atom { atom: Atom::Sym("main".into()) } + ] + }, + Sexp::List { + list: vec![ + Sexp::Atom { atom: Atom::Sym("print".into()) }, + Sexp::List { + list: vec![ + Sexp::Atom { + atom: Atom::Sym("str".into()) + }, + Sexp::Atom { + atom: Atom::Str("say ".into()) + }, + Sexp::Atom { atom: Atom::Char('"') }, + Sexp::Atom { + atom: Atom::Str("Hello, World".into()) + }, + Sexp::Atom { atom: Atom::Char('"') }, + Sexp::Atom { + atom: Atom::Str(" today!".into()) + } + ] + } + ] + } + ] + })); +} -- 2.43.2