diff options
author | Alex Crichton <alex@alexcrichton.com> | 2015-03-15 11:26:32 -0700 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-15 11:26:32 -0700 |
commit | b852af0a6000efa6cf444a48b27eda7cd67b6761 (patch) | |
tree | 65cbfc4473ddcb10ae1e201480a6d06fe2c4abe3 | |
parent | ff3bb7d25505a565f8bdfd73b1105ae15a1e6f4b (diff) | |
download | milf-rs-b852af0a6000efa6cf444a48b27eda7cd67b6761.tar.gz milf-rs-b852af0a6000efa6cf444a48b27eda7cd67b6761.zip |
Improve test coverage
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/parser.rs | 72 |
2 files changed, 74 insertions, 2 deletions
@@ -240,8 +240,8 @@ mod tests { let foo1 = value.lookup("values.1.foo").unwrap(); assert_eq!(foo1.as_str().unwrap(), "qux"); - let no_bar = value.lookup("test.bar"); - assert!(no_bar.is_none()); + assert!(value.lookup("test.bar").is_none()); + assert!(value.lookup("test.foo.bar").is_none()); } #[test] diff --git a/src/parser.rs b/src/parser.rs index abb4c5c..377da24 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -893,6 +893,15 @@ mod tests { use Value::Table; use Parser; + macro_rules! bad { + ($s:expr, $msg:expr) => ({ + let mut p = Parser::new($s); + assert!(p.parse().is_none()); + assert!(p.errors.iter().any(|e| e.desc.contains($msg)), + "errors: {:?}", p.errors); + }) + } + #[test] fn crlf() { let mut p = Parser::new("\ @@ -1262,4 +1271,67 @@ trimmed in raw strings. assert!(Parser::new("foo = __0").parse().is_none()); assert!(Parser::new("foo = 1_0_").parse().is_none()); } + + #[test] + fn bad_unicode_codepoint() { + bad!("foo = \"\\uD800\"", "not a valid unicode codepoint"); + } + + #[test] + fn bad_strings() { + bad!("foo = \"\\uxx\"", "expected 4 hex digits"); + bad!("foo = \"\\u\"", "expected 4 hex digits"); + bad!("foo = \"\\", "unterminated"); + bad!("foo = '", "unterminated"); + } + + #[test] + fn empty_string() { + let mut p = Parser::new("foo = \"\""); + let table = Table(p.parse().unwrap()); + assert_eq!(table.lookup("foo").unwrap().as_str(), Some("")); + } + + #[test] + fn booleans() { + let mut p = Parser::new("foo = true"); + let table = Table(p.parse().unwrap()); + assert_eq!(table.lookup("foo").unwrap().as_bool(), Some(true)); + + let mut p = Parser::new("foo = false"); + let table = Table(p.parse().unwrap()); + assert_eq!(table.lookup("foo").unwrap().as_bool(), Some(false)); + + assert!(Parser::new("foo = true2").parse().is_none()); + assert!(Parser::new("foo = false2").parse().is_none()); + assert!(Parser::new("foo = t1").parse().is_none()); + assert!(Parser::new("foo = f2").parse().is_none()); + } + + #[test] + fn bad_nesting() { + bad!(" + a = [2] + [[a]] + b = 5 + ", "expected type `integer`, found type `table`"); + bad!(" + a = 1 + [a.b] + ", "key `a` was not previously a table"); + bad!(" + a = [] + [a.b] + ", "array `a` does not contain tables"); + bad!(" + a = [] + [[a.b]] + ", "array `a` does not contain tables"); + bad!(" + [a] + b = { c = 2, d = {} } + [a.b] + c = 2 + ", "duplicate key `c` in table"); + } } |