From b852af0a6000efa6cf444a48b27eda7cd67b6761 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 15 Mar 2015 11:26:32 -0700 Subject: [PATCH] Improve test coverage --- src/lib.rs | 4 +-- src/parser.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5c5fa91..4b87b69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"); + } }