Fully expand error messages in tests

This commit is contained in:
Andres Suarez 2019-07-28 16:56:22 +00:00
parent 0a3fd8ae62
commit 8820659431
6 changed files with 414 additions and 200 deletions

View file

@ -3,13 +3,22 @@ extern crate toml;
use serde::de::Deserialize; use serde::de::Deserialize;
macro_rules! bad {
($toml:expr, $msg:expr) => {
match $toml.parse::<toml::Value>() {
Ok(s) => panic!("parsed to: {:#?}", s),
Err(e) => assert_eq!(e.to_string(), $msg),
}
};
}
#[test] #[test]
fn newlines_after_tables() { fn newlines_after_tables() {
let s = " let s = "
[a] foo = 1 [a] foo = 1
[[b]] foo = 1 [[b]] foo = 1
"; ";
assert!(s.parse::<toml::Value>().is_err()); bad!(s, "expected newline, found an identifier at line 2");
let mut d = toml::de::Deserializer::new(s); let mut d = toml::de::Deserializer::new(s);
d.set_require_newline_after_table(false); d.set_require_newline_after_table(false);
@ -30,7 +39,10 @@ fn allow_duplicate_after_longer() {
[dependencies] [dependencies]
bitflags = 1 bitflags = 1
"; ";
assert!(s.parse::<toml::Value>().is_err()); bad!(
s,
"redefinition of table `dependencies` for key `dependencies` at line 8"
);
let mut d = toml::de::Deserializer::new(s); let mut d = toml::de::Deserializer::new(s);
d.set_allow_duplicate_after_longer_table(true); d.set_allow_duplicate_after_longer_table(true);

View file

@ -2,13 +2,20 @@ extern crate toml;
use std::str::FromStr; use std::str::FromStr;
use toml::Value; macro_rules! bad {
($toml:expr, $msg:expr) => {
match $toml.parse::<toml::Value>() {
Ok(s) => panic!("parsed to: {:#?}", s),
Err(e) => assert_eq!(e.to_string(), $msg),
}
};
}
#[test] #[test]
fn times() { fn times() {
fn dogood(s: &str, serialized: &str) { fn dogood(s: &str, serialized: &str) {
let to_parse = format!("foo = {}", s); let to_parse = format!("foo = {}", s);
let value = Value::from_str(&to_parse).unwrap(); let value = toml::Value::from_str(&to_parse).unwrap();
assert_eq!(value["foo"].as_datetime().unwrap().to_string(), serialized); assert_eq!(value["foo"].as_datetime().unwrap().to_string(), serialized);
} }
fn good(s: &str) { fn good(s: &str) {
@ -35,32 +42,66 @@ fn times() {
#[test] #[test]
fn bad_times() { fn bad_times() {
fn bad(s: &str) { bad!("foo = 199-09-09", "failed to parse datetime for key `foo`");
let to_parse = format!("foo = {}", s); bad!("foo = 199709-09", "failed to parse datetime for key `foo`");
assert!(Value::from_str(&to_parse).is_err()); bad!("foo = 1997-9-09", "failed to parse datetime for key `foo`");
} bad!("foo = 1997-09-9", "failed to parse datetime for key `foo`");
bad!(
"foo = 1997-09-0909:09:09",
"failed to parse datetime for key `foo`"
);
bad!("foo = 1997-09-09T09:09:09.", "invalid date at line 1");
bad!("foo = T", "failed to parse datetime for key `foo`");
bad!("foo = T.", "expected newline, found a period at line 1");
bad!("foo = TZ", "failed to parse datetime for key `foo`");
bad!("foo = 1997-09-09T09:09:09.09+", "invalid date at line 1");
bad!(
"foo = 1997-09-09T09:09:09.09+09",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 1997-09-09T09:09:09.09+09:9",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 1997-09-09T09:09:09.09+0909",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 1997-09-09T09:09:09.09-",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 1997-09-09T09:09:09.09-09",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 1997-09-09T09:09:09.09-09:9",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 1997-09-09T09:09:09.09-0909",
"failed to parse datetime for key `foo`"
);
bad("199-09-09"); bad!(
bad("199709-09"); "foo = 1997-00-09T09:09:09.09Z",
bad("1997-9-09"); "failed to parse datetime for key `foo`"
bad("1997-09-9"); );
bad("1997-09-0909:09:09"); bad!(
bad("1997-09-09T09:09:09."); "foo = 1997-09-00T09:09:09.09Z",
bad("T"); "failed to parse datetime for key `foo`"
bad("T."); );
bad("TZ"); bad!(
bad("1997-09-09T09:09:09.09+"); "foo = 1997-09-09T30:09:09.09Z",
bad("1997-09-09T09:09:09.09+09"); "failed to parse datetime for key `foo`"
bad("1997-09-09T09:09:09.09+09:9"); );
bad("1997-09-09T09:09:09.09+0909"); bad!(
bad("1997-09-09T09:09:09.09-"); "foo = 1997-09-09T12:69:09.09Z",
bad("1997-09-09T09:09:09.09-09"); "failed to parse datetime for key `foo`"
bad("1997-09-09T09:09:09.09-09:9"); );
bad("1997-09-09T09:09:09.09-0909"); bad!(
"foo = 1997-09-09T12:09:69.09Z",
bad("1997-00-09T09:09:09.09Z"); "failed to parse datetime for key `foo`"
bad("1997-09-00T09:09:09.09Z"); );
bad("1997-09-09T30:09:09.09Z");
bad("1997-09-09T12:69:09.09Z");
bad("1997-09-09T12:09:69.09Z");
} }

View file

@ -1,28 +1,48 @@
extern crate toml; extern crate toml;
macro_rules! bad {
($toml:expr, $msg:expr) => {
match $toml.parse::<toml::Value>() {
Ok(s) => panic!("parsed to: {:#?}", s),
Err(e) => assert_eq!(e.to_string(), $msg),
}
};
}
#[test] #[test]
fn bad() { fn bad() {
fn bad(s: &str) { bad!("a = 01", "invalid number at line 1");
assert!(s.parse::<toml::Value>().is_err()); bad!("a = 1__1", "invalid number at line 1");
} bad!("a = 1_", "invalid number at line 1");
bad!("''", "empty table key found at line 1");
bad!("a = 9e99999", "invalid number at line 1");
bad("a = 01"); bad!(
bad("a = 1__1"); "a = \"\u{7f}\"",
bad("a = 1_"); "invalid character in string: `\\u{7f}` at line 1"
bad("''"); );
bad("a = 9e99999"); bad!(
"a = '\u{7f}'",
"invalid character in string: `\\u{7f}` at line 1"
);
bad("a = \"\u{7f}\""); bad!("a = -0x1", "invalid number at line 1");
bad("a = '\u{7f}'"); bad!("a = 0x-1", "failed to parse datetime for key `a`");
bad("a = -0x1");
bad("a = 0x-1");
// Dotted keys. // Dotted keys.
bad("a.b.c = 1 bad!(
"a.b.c = 1
a.b = 2 a.b = 2
"); ",
bad("a = 1 "duplicate key: `b` for key `a`"
a.b = 2"); );
bad("a = {k1 = 1, k1.name = \"joe\"}") bad!(
"a = 1
a.b = 2",
"dotted key attempted to extend non-table type at line 1"
);
bad!(
"a = {k1 = 1, k1.name = \"joe\"}",
"dotted key attempted to extend non-table type at line 1"
);
} }

View file

@ -1,167 +1,241 @@
extern crate toml; extern crate toml;
fn run(toml: &str) { macro_rules! bad {
println!("test if invalid:\n{}", toml); ($toml:expr, $msg:expr) => {
if let Ok(e) = toml.parse::<toml::Value>() { match $toml.parse::<toml::Value>() {
panic!("parsed to: {:#?}", e); Ok(s) => panic!("parsed to: {:#?}", s),
Err(e) => assert_eq!(e.to_string(), $msg),
} }
};
} }
macro_rules! test( ($name:ident, $toml:expr) => ( macro_rules! test( ($name:ident, $s:expr, $msg:expr) => (
#[test] #[test]
fn $name() { run($toml); } fn $name() { bad!($s, $msg); }
) ); ) );
test!( test!(
array_mixed_types_arrays_and_ints, array_mixed_types_arrays_and_ints,
include_str!("invalid/array-mixed-types-arrays-and-ints.toml") include_str!("invalid/array-mixed-types-arrays-and-ints.toml"),
"mixed types in an array at line 1"
); );
test!( test!(
array_mixed_types_ints_and_floats, array_mixed_types_ints_and_floats,
include_str!("invalid/array-mixed-types-ints-and-floats.toml") include_str!("invalid/array-mixed-types-ints-and-floats.toml"),
"mixed types in an array at line 1"
); );
test!( test!(
array_mixed_types_strings_and_ints, array_mixed_types_strings_and_ints,
include_str!("invalid/array-mixed-types-strings-and-ints.toml") include_str!("invalid/array-mixed-types-strings-and-ints.toml"),
"mixed types in an array at line 1"
); );
test!( test!(
datetime_malformed_no_leads, datetime_malformed_no_leads,
include_str!("invalid/datetime-malformed-no-leads.toml") include_str!("invalid/datetime-malformed-no-leads.toml"),
"failed to parse datetime for key `no-leads`"
); );
test!( test!(
datetime_malformed_no_secs, datetime_malformed_no_secs,
include_str!("invalid/datetime-malformed-no-secs.toml") include_str!("invalid/datetime-malformed-no-secs.toml"),
"expected a colon, found a newline at line 1"
); );
test!( test!(
datetime_malformed_no_t, datetime_malformed_no_t,
include_str!("invalid/datetime-malformed-no-t.toml") include_str!("invalid/datetime-malformed-no-t.toml"),
"failed to parse datetime for key `no-t`"
); );
test!( test!(
datetime_malformed_with_milli, datetime_malformed_with_milli,
include_str!("invalid/datetime-malformed-with-milli.toml") include_str!("invalid/datetime-malformed-with-milli.toml"),
"failed to parse datetime for key `with-milli`"
); );
test!( test!(
duplicate_key_table, duplicate_key_table,
include_str!("invalid/duplicate-key-table.toml") include_str!("invalid/duplicate-key-table.toml"),
"duplicate key: `type` for key `fruit`"
);
test!(
duplicate_keys,
include_str!("invalid/duplicate-keys.toml"),
"duplicate key: `dupe`"
); );
test!(duplicate_keys, include_str!("invalid/duplicate-keys.toml"));
test!( test!(
duplicate_table, duplicate_table,
include_str!("invalid/duplicate-table.toml") include_str!("invalid/duplicate-table.toml"),
"redefinition of table `dependencies` for key `dependencies` at line 7"
); );
test!( test!(
duplicate_tables, duplicate_tables,
include_str!("invalid/duplicate-tables.toml") include_str!("invalid/duplicate-tables.toml"),
"redefinition of table `a` for key `a` at line 2"
); );
test!( test!(
empty_implicit_table, empty_implicit_table,
include_str!("invalid/empty-implicit-table.toml") include_str!("invalid/empty-implicit-table.toml"),
"expected a table key, found a period at line 1"
);
test!(
empty_table,
include_str!("invalid/empty-table.toml"),
"expected a table key, found a right bracket at line 1"
); );
test!(empty_table, include_str!("invalid/empty-table.toml"));
test!( test!(
float_no_leading_zero, float_no_leading_zero,
include_str!("invalid/float-no-leading-zero.toml") include_str!("invalid/float-no-leading-zero.toml"),
"expected a value, found a period at line 1"
); );
test!( test!(
float_no_suffix, float_no_suffix,
include_str!("invalid/float-no-suffix.toml") include_str!("invalid/float-no-suffix.toml"),
"invalid number at line 1"
); );
test!( test!(
float_no_trailing_digits, float_no_trailing_digits,
include_str!("invalid/float-no-trailing-digits.toml") include_str!("invalid/float-no-trailing-digits.toml"),
"invalid number at line 1"
); );
test!( test!(
key_after_array, key_after_array,
include_str!("invalid/key-after-array.toml") include_str!("invalid/key-after-array.toml"),
"expected newline, found an identifier at line 1"
); );
test!( test!(
key_after_table, key_after_table,
include_str!("invalid/key-after-table.toml") include_str!("invalid/key-after-table.toml"),
"expected newline, found an identifier at line 1"
);
test!(
key_empty,
include_str!("invalid/key-empty.toml"),
"expected a table key, found an equals at line 1"
);
test!(
key_hash,
include_str!("invalid/key-hash.toml"),
"expected an equals, found a comment at line 1"
);
test!(
key_newline,
include_str!("invalid/key-newline.toml"),
"expected an equals, found a newline at line 1"
); );
test!(key_empty, include_str!("invalid/key-empty.toml"));
test!(key_hash, include_str!("invalid/key-hash.toml"));
test!(key_newline, include_str!("invalid/key-newline.toml"));
test!( test!(
key_open_bracket, key_open_bracket,
include_str!("invalid/key-open-bracket.toml") include_str!("invalid/key-open-bracket.toml"),
"expected a right bracket, found an equals at line 1"
); );
test!( test!(
key_single_open_bracket, key_single_open_bracket,
include_str!("invalid/key-single-open-bracket.toml") include_str!("invalid/key-single-open-bracket.toml"),
"expected a table key, found eof at line 1"
);
test!(
key_space,
include_str!("invalid/key-space.toml"),
"expected an equals, found an identifier at line 1"
); );
test!(key_space, include_str!("invalid/key-space.toml"));
test!( test!(
key_start_bracket, key_start_bracket,
include_str!("invalid/key-start-bracket.toml") include_str!("invalid/key-start-bracket.toml"),
"expected a right bracket, found an equals at line 2"
);
test!(
key_two_equals,
include_str!("invalid/key-two-equals.toml"),
"expected a value, found an equals at line 1"
); );
test!(key_two_equals, include_str!("invalid/key-two-equals.toml"));
test!( test!(
string_bad_byte_escape, string_bad_byte_escape,
include_str!("invalid/string-bad-byte-escape.toml") include_str!("invalid/string-bad-byte-escape.toml"),
"invalid escape character in string: `x` at line 1"
); );
test!( test!(
string_bad_escape, string_bad_escape,
include_str!("invalid/string-bad-escape.toml") include_str!("invalid/string-bad-escape.toml"),
"invalid escape character in string: `a` at line 1"
); );
test!( test!(
string_bad_line_ending_escape, string_bad_line_ending_escape,
include_str!("invalid/string-bad-line-ending-escape.toml") include_str!("invalid/string-bad-line-ending-escape.toml"),
"invalid escape character in string: ` ` at line 2"
); );
test!( test!(
string_byte_escapes, string_byte_escapes,
include_str!("invalid/string-byte-escapes.toml") include_str!("invalid/string-byte-escapes.toml"),
"invalid escape character in string: `x` at line 1"
); );
test!( test!(
string_no_close, string_no_close,
include_str!("invalid/string-no-close.toml") include_str!("invalid/string-no-close.toml"),
"newline in string found at line 1"
); );
test!( test!(
table_array_implicit, table_array_implicit,
include_str!("invalid/table-array-implicit.toml") include_str!("invalid/table-array-implicit.toml"),
"table redefined as array for key `albums` at line 13"
); );
test!( test!(
table_array_malformed_bracket, table_array_malformed_bracket,
include_str!("invalid/table-array-malformed-bracket.toml") include_str!("invalid/table-array-malformed-bracket.toml"),
"expected a right bracket, found a newline at line 1"
); );
test!( test!(
table_array_malformed_empty, table_array_malformed_empty,
include_str!("invalid/table-array-malformed-empty.toml") include_str!("invalid/table-array-malformed-empty.toml"),
"expected a table key, found a right bracket at line 1"
);
test!(
table_empty,
include_str!("invalid/table-empty.toml"),
"expected a table key, found a right bracket at line 1"
); );
test!(table_empty, include_str!("invalid/table-empty.toml"));
test!( test!(
table_nested_brackets_close, table_nested_brackets_close,
include_str!("invalid/table-nested-brackets-close.toml") include_str!("invalid/table-nested-brackets-close.toml"),
"expected newline, found an identifier at line 1"
); );
test!( test!(
table_nested_brackets_open, table_nested_brackets_open,
include_str!("invalid/table-nested-brackets-open.toml") include_str!("invalid/table-nested-brackets-open.toml"),
"expected a right bracket, found a left bracket at line 1"
); );
test!( test!(
table_whitespace, table_whitespace,
include_str!("invalid/table-whitespace.toml") include_str!("invalid/table-whitespace.toml"),
"expected a right bracket, found an identifier at line 1"
); );
test!( test!(
table_with_pound, table_with_pound,
include_str!("invalid/table-with-pound.toml") include_str!("invalid/table-with-pound.toml"),
"expected a right bracket, found a comment at line 1"
); );
test!( test!(
text_after_array_entries, text_after_array_entries,
include_str!("invalid/text-after-array-entries.toml") include_str!("invalid/text-after-array-entries.toml"),
"invalid number at line 2"
); );
test!( test!(
text_after_integer, text_after_integer,
include_str!("invalid/text-after-integer.toml") include_str!("invalid/text-after-integer.toml"),
"expected newline, found an identifier at line 1"
); );
test!( test!(
text_after_string, text_after_string,
include_str!("invalid/text-after-string.toml") include_str!("invalid/text-after-string.toml"),
"expected newline, found an identifier at line 1"
); );
test!( test!(
text_after_table, text_after_table,
include_str!("invalid/text-after-table.toml") include_str!("invalid/text-after-table.toml"),
"expected newline, found an identifier at line 1"
); );
test!( test!(
text_before_array_separator, text_before_array_separator,
include_str!("invalid/text-before-array-separator.toml") include_str!("invalid/text-before-array-separator.toml"),
"expected a right bracket, found an identifier at line 2"
);
test!(
text_in_array,
include_str!("invalid/text-in-array.toml"),
"invalid number at line 3"
); );
test!(text_in_array, include_str!("invalid/text-in-array.toml"));

View file

@ -3,15 +3,12 @@ extern crate toml;
use toml::Value; use toml::Value;
macro_rules! bad { macro_rules! bad {
($s:expr, $msg:expr) => {{ ($toml:expr, $msg:expr) => {
match $s.parse::<Value>() { match $toml.parse::<toml::Value>() {
Ok(s) => panic!("successfully parsed as {}", s), Ok(s) => panic!("parsed to: {:#?}", s),
Err(e) => { Err(e) => assert_eq!(e.to_string(), $msg),
let e = e.to_string();
assert!(e.contains($msg), "error: {}", e);
} }
} };
}};
} }
#[test] #[test]
@ -189,13 +186,22 @@ name = "plantain"
#[test] #[test]
fn stray_cr() { fn stray_cr() {
"\r".parse::<Value>().unwrap_err(); bad!("\r", "unexpected character found: `\\r` at line 1");
"a = [ \r ]".parse::<Value>().unwrap_err(); bad!("a = [ \r ]", "unexpected character found: `\\r` at line 1");
"a = \"\"\"\r\"\"\"".parse::<Value>().unwrap_err(); bad!(
"a = \"\"\"\\ \r \"\"\"".parse::<Value>().unwrap_err(); "a = \"\"\"\r\"\"\"",
"a = '''\r'''".parse::<Value>().unwrap_err(); "invalid character in string: `\\r` at line 1"
"a = '\r'".parse::<Value>().unwrap_err(); );
"a = \"\r\"".parse::<Value>().unwrap_err(); bad!(
"a = \"\"\"\\ \r \"\"\"",
"invalid escape character in string: ` ` at line 1"
);
bad!(
"a = '''\r'''",
"invalid character in string: `\\r` at line 1"
);
bad!("a = '\r'", "invalid character in string: `\\r` at line 1");
bad!("a = \"\r\"", "invalid character in string: `\\r` at line 1");
} }
#[test] #[test]
@ -224,32 +230,32 @@ fn literal_eats_crlf() {
#[test] #[test]
fn string_no_newline() { fn string_no_newline() {
"a = \"\n\"".parse::<Value>().unwrap_err(); bad!("a = \"\n\"", "newline in string found at line 1");
"a = '\n'".parse::<Value>().unwrap_err(); bad!("a = '\n'", "newline in string found at line 1");
} }
#[test] #[test]
fn bad_leading_zeros() { fn bad_leading_zeros() {
"a = 00".parse::<Value>().unwrap_err(); bad!("a = 00", "invalid number at line 1");
"a = -00".parse::<Value>().unwrap_err(); bad!("a = -00", "invalid number at line 1");
"a = +00".parse::<Value>().unwrap_err(); bad!("a = +00", "invalid number at line 1");
"a = 00.0".parse::<Value>().unwrap_err(); bad!("a = 00.0", "invalid number at line 1");
"a = -00.0".parse::<Value>().unwrap_err(); bad!("a = -00.0", "invalid number at line 1");
"a = +00.0".parse::<Value>().unwrap_err(); bad!("a = +00.0", "invalid number at line 1");
"a = 9223372036854775808".parse::<Value>().unwrap_err(); bad!("a = 9223372036854775808", "invalid number at line 1");
"a = -9223372036854775809".parse::<Value>().unwrap_err(); bad!("a = -9223372036854775809", "invalid number at line 1");
} }
#[test] #[test]
fn bad_floats() { fn bad_floats() {
"a = 0.".parse::<Value>().unwrap_err(); bad!("a = 0.", "invalid number at line 1");
"a = 0.e".parse::<Value>().unwrap_err(); bad!("a = 0.e", "invalid number at line 1");
"a = 0.E".parse::<Value>().unwrap_err(); bad!("a = 0.E", "invalid number at line 1");
"a = 0.0E".parse::<Value>().unwrap_err(); bad!("a = 0.0E", "invalid number at line 1");
"a = 0.0e".parse::<Value>().unwrap_err(); bad!("a = 0.0e", "invalid number at line 1");
"a = 0.0e-".parse::<Value>().unwrap_err(); bad!("a = 0.0e-", "invalid number at line 1");
"a = 0.0e+".parse::<Value>().unwrap_err(); bad!("a = 0.0e+", "invalid number at line 1");
"a = 0.0e+00".parse::<Value>().unwrap_err(); bad!("a = 0.0e+00", "invalid number at line 1");
} }
#[test] #[test]
@ -310,37 +316,67 @@ fn bare_key_names() {
#[test] #[test]
fn bad_keys() { fn bad_keys() {
"key\n=3".parse::<Value>().unwrap_err(); bad!("key\n=3", "expected an equals, found a newline at line 1");
"key=\n3".parse::<Value>().unwrap_err(); bad!("key=\n3", "expected a value, found a newline at line 1");
"key|=3".parse::<Value>().unwrap_err(); bad!("key|=3", "unexpected character found: `|` at line 1");
"\"\"=3".parse::<Value>().unwrap_err(); bad!("\"\"=3", "empty table key found at line 1");
"=3".parse::<Value>().unwrap_err(); bad!("=3", "expected a table key, found an equals at line 1");
"\"\"|=3".parse::<Value>().unwrap_err(); bad!("\"\"|=3", "empty table key found at line 1");
"\"\n\"|=3".parse::<Value>().unwrap_err(); bad!("\"\n\"|=3", "newline in string found at line 1");
"\"\r\"|=3".parse::<Value>().unwrap_err(); bad!("\"\r\"|=3", "invalid character in string: `\\r` at line 1");
"''''''=3".parse::<Value>().unwrap_err(); bad!(
"\"\"\"\"\"\"=3".parse::<Value>().unwrap_err(); "''''''=3",
"'''key'''=3".parse::<Value>().unwrap_err(); "multiline strings are not allowed for key at line 1"
"\"\"\"key\"\"\"=3".parse::<Value>().unwrap_err(); );
bad!(
"\"\"\"\"\"\"=3",
"multiline strings are not allowed for key at line 1"
);
bad!(
"'''key'''=3",
"multiline strings are not allowed for key at line 1"
);
bad!(
"\"\"\"key\"\"\"=3",
"multiline strings are not allowed for key at line 1"
);
} }
#[test] #[test]
fn bad_table_names() { fn bad_table_names() {
"[]".parse::<Value>().unwrap_err(); bad!(
"[.]".parse::<Value>().unwrap_err(); "[]",
"[\"\".\"\"]".parse::<Value>().unwrap_err(); "expected a table key, found a right bracket at line 1"
"[a.]".parse::<Value>().unwrap_err(); );
"[\"\"]".parse::<Value>().unwrap_err(); bad!("[.]", "expected a table key, found a period at line 1");
"[!]".parse::<Value>().unwrap_err(); bad!("[\"\".\"\"]", "empty table key found at line 1");
"[\"\n\"]".parse::<Value>().unwrap_err(); bad!(
"[a.b]\n[a.\"b\"]".parse::<Value>().unwrap_err(); "[a.]",
"[']".parse::<Value>().unwrap_err(); "expected a table key, found a right bracket at line 1"
"[''']".parse::<Value>().unwrap_err(); );
"['''''']".parse::<Value>().unwrap_err(); bad!("[\"\"]", "empty table key found at line 1");
"['''foo''']".parse::<Value>().unwrap_err(); bad!("[!]", "unexpected character found: `!` at line 1");
"[\"\"\"bar\"\"\"]".parse::<Value>().unwrap_err(); bad!("[\"\n\"]", "newline in string found at line 1");
"['\n']".parse::<Value>().unwrap_err(); bad!(
"['\r\n']".parse::<Value>().unwrap_err(); "[a.b]\n[a.\"b\"]",
"redefinition of table `a.b` for key `a.b` at line 2"
);
bad!("[']", "unterminated string at line 1");
bad!("[''']", "unterminated string at line 1");
bad!(
"['''''']",
"multiline strings are not allowed for key at line 1"
);
bad!(
"['''foo''']",
"multiline strings are not allowed for key at line 1"
);
bad!(
"[\"\"\"bar\"\"\"]",
"multiline strings are not allowed for key at line 1"
);
bad!("['\n']", "newline in string found at line 1");
bad!("['\r\n']", "newline in string found at line 1");
} }
#[test] #[test]
@ -365,7 +401,7 @@ fn table_names() {
#[test] #[test]
fn invalid_bare_numeral() { fn invalid_bare_numeral() {
"4".parse::<Value>().unwrap_err(); bad!("4", "expected an equals, found eof at line 1");
} }
#[test] #[test]
@ -375,11 +411,19 @@ fn inline_tables() {
"a = { b = 1 }".parse::<Value>().unwrap(); "a = { b = 1 }".parse::<Value>().unwrap();
"a = {a=1,b=2}".parse::<Value>().unwrap(); "a = {a=1,b=2}".parse::<Value>().unwrap();
"a = {a=1,b=2,c={}}".parse::<Value>().unwrap(); "a = {a=1,b=2,c={}}".parse::<Value>().unwrap();
"a = {a=1,}".parse::<Value>().unwrap_err();
"a = {,}".parse::<Value>().unwrap_err(); bad!(
"a = {a=1,a=1}".parse::<Value>().unwrap_err(); "a = {a=1,}",
"a = {\n}".parse::<Value>().unwrap_err(); "expected a table key, found a right brace at line 1"
"a = {".parse::<Value>().unwrap_err(); );
bad!("a = {,}", "expected a table key, found a comma at line 1");
bad!("a = {a=1,a=1}", "duplicate key: `a` for key `a`");
bad!(
"a = {\n}",
"expected a table key, found a newline at line 1"
);
bad!("a = {", "expected a table key, found eof at line 1");
"a = {a=[\n]}".parse::<Value>().unwrap(); "a = {a=[\n]}".parse::<Value>().unwrap();
"a = {\"a\"=[\n]}".parse::<Value>().unwrap(); "a = {\"a\"=[\n]}".parse::<Value>().unwrap();
"a = [\n{},\n{},\n]".parse::<Value>().unwrap(); "a = [\n{},\n{},\n]".parse::<Value>().unwrap();
@ -404,23 +448,32 @@ fn number_underscores() {
#[test] #[test]
fn bad_underscores() { fn bad_underscores() {
bad!("foo = 0_", "invalid number"); bad!("foo = 0_", "invalid number at line 1");
bad!("foo = 0__0", "invalid number"); bad!("foo = 0__0", "invalid number at line 1");
bad!("foo = __0", "invalid number"); bad!("foo = __0", "invalid number at line 1");
bad!("foo = 1_0_", "invalid number"); bad!("foo = 1_0_", "invalid number at line 1");
} }
#[test] #[test]
fn bad_unicode_codepoint() { fn bad_unicode_codepoint() {
bad!("foo = \"\\uD800\"", "invalid escape value"); bad!(
"foo = \"\\uD800\"",
"invalid escape value: `55296` at line 1"
);
} }
#[test] #[test]
fn bad_strings() { fn bad_strings() {
bad!("foo = \"\\uxx\"", "invalid hex escape"); bad!(
bad!("foo = \"\\u\"", "invalid hex escape"); "foo = \"\\uxx\"",
bad!("foo = \"\\", "unterminated"); "invalid hex escape character in string: `x` at line 1"
bad!("foo = '", "unterminated"); );
bad!(
"foo = \"\\u\"",
"invalid hex escape character in string: `\\\"` at line 1"
);
bad!("foo = \"\\", "unterminated string at line 1");
bad!("foo = '", "unterminated string at line 1");
} }
#[test] #[test]
@ -441,10 +494,10 @@ fn booleans() {
let table = "foo = false".parse::<Value>().unwrap(); let table = "foo = false".parse::<Value>().unwrap();
assert_eq!(table["foo"].as_bool(), Some(false)); assert_eq!(table["foo"].as_bool(), Some(false));
assert!("foo = true2".parse::<Value>().is_err()); bad!("foo = true2", "failed to parse datetime for key `foo`");
assert!("foo = false2".parse::<Value>().is_err()); bad!("foo = false2", "invalid number at line 1");
assert!("foo = t1".parse::<Value>().is_err()); bad!("foo = t1", "failed to parse datetime for key `foo`");
assert!("foo = f2".parse::<Value>().is_err()); bad!("foo = f2", "invalid number at line 1");
} }
#[test] #[test]
@ -485,7 +538,7 @@ fn bad_nesting() {
[a.b] [a.b]
c = 2 c = 2
", ",
"duplicate key: `b`" "duplicate key: `b` for key `a`"
); );
} }
@ -499,7 +552,7 @@ fn bad_table_redefine() {
foo=\"bar\" foo=\"bar\"
[a] [a]
", ",
"redefinition of table `a`" "redefinition of table `a` for key `a` at line 6"
); );
bad!( bad!(
" "
@ -508,7 +561,7 @@ fn bad_table_redefine() {
b = { foo = \"bar\" } b = { foo = \"bar\" }
[a] [a]
", ",
"redefinition of table `a`" "redefinition of table `a` for key `a` at line 5"
); );
bad!( bad!(
" "
@ -516,7 +569,7 @@ fn bad_table_redefine() {
b = {} b = {}
[a.b] [a.b]
", ",
"duplicate key: `b`" "duplicate key: `b` for key `a`"
); );
bad!( bad!(
@ -525,7 +578,7 @@ fn bad_table_redefine() {
b = {} b = {}
[a] [a]
", ",
"redefinition of table `a`" "redefinition of table `a` for key `a` at line 4"
); );
} }
@ -543,11 +596,26 @@ fn datetimes() {
t!("2016-09-09T09:09:09.1Z"); t!("2016-09-09T09:09:09.1Z");
t!("2016-09-09T09:09:09.2+10:00"); t!("2016-09-09T09:09:09.2+10:00");
t!("2016-09-09T09:09:09.123456789-02:00"); t!("2016-09-09T09:09:09.123456789-02:00");
bad!("foo = 2016-09-09T09:09:09.Z", "failed to parse date"); bad!(
bad!("foo = 2016-9-09T09:09:09Z", "failed to parse date"); "foo = 2016-09-09T09:09:09.Z",
bad!("foo = 2016-09-09T09:09:09+2:00", "failed to parse date"); "failed to parse datetime for key `foo`"
bad!("foo = 2016-09-09T09:09:09-2:00", "failed to parse date"); );
bad!("foo = 2016-09-09T09:09:09Z-2:00", "failed to parse date"); bad!(
"foo = 2016-9-09T09:09:09Z",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 2016-09-09T09:09:09+2:00",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 2016-09-09T09:09:09-2:00",
"failed to parse datetime for key `foo`"
);
bad!(
"foo = 2016-09-09T09:09:09Z-2:00",
"failed to parse datetime for key `foo`"
);
} }
#[test] #[test]
@ -557,7 +625,7 @@ fn require_newline_after_value() {
r#" r#"
0=""o=""m=""r=""00="0"q="""0"""e="""0""" 0=""o=""m=""r=""00="0"q="""0"""e="""0"""
"#, "#,
"expected newline" "expected newline, found an identifier at line 2"
); );
bad!( bad!(
r#" r#"
@ -566,13 +634,13 @@ fn require_newline_after_value() {
0="0"[[0000l0]] 0="0"[[0000l0]]
0="0"l="0" 0="0"l="0"
"#, "#,
"expected newline" "expected newline, found a left bracket at line 3"
); );
bad!( bad!(
r#" r#"
0=[0]00=[0,0,0]t=["0","0","0"]s=[1000-00-00T00:00:00Z,2000-00-00T00:00:00Z] 0=[0]00=[0,0,0]t=["0","0","0"]s=[1000-00-00T00:00:00Z,2000-00-00T00:00:00Z]
"#, "#,
"expected newline" "expected newline, found an identifier at line 2"
); );
bad!( bad!(
r#" r#"

View file

@ -44,21 +44,17 @@ macro_rules! equivalent {
} }
macro_rules! error { macro_rules! error {
($ty:ty, $toml:expr, $error:expr) => {{ ($ty:ty, $toml:expr, $msg_parse:expr, $msg_decode:expr) => {{
println!("attempting parsing"); println!("attempting parsing");
match toml::from_str::<$ty>(&$toml.to_string()) { match toml::from_str::<$ty>(&$toml.to_string()) {
Ok(_) => panic!("successful"), Ok(_) => panic!("successful"),
Err(e) => { Err(e) => assert_eq!(e.to_string(), $msg_parse),
assert!(e.to_string().contains($error), "bad error: {}", e);
}
} }
println!("attempting toml decoding"); println!("attempting toml decoding");
match $toml.try_into::<$ty>() { match $toml.try_into::<$ty>() {
Ok(_) => panic!("successful"), Ok(_) => panic!("successful"),
Err(e) => { Err(e) => assert_eq!(e.to_string(), $msg_decode),
assert!(e.to_string().contains($error), "bad error: {}", e);
}
} }
}}; }};
} }
@ -277,6 +273,7 @@ fn type_errors() {
Table(map! { Table(map! {
bar: Value::String("a".to_string()) bar: Value::String("a".to_string())
}), }),
"invalid type: string \"a\", expected isize for key `bar`",
"invalid type: string \"a\", expected isize for key `bar`" "invalid type: string \"a\", expected isize for key `bar`"
} }
@ -293,6 +290,7 @@ fn type_errors() {
bar: Value::String("a".to_string()) bar: Value::String("a".to_string())
}) })
}), }),
"invalid type: string \"a\", expected isize for key `foo.bar`",
"invalid type: string \"a\", expected isize for key `foo.bar`" "invalid type: string \"a\", expected isize for key `foo.bar`"
} }
} }
@ -307,6 +305,7 @@ fn missing_errors() {
error! { error! {
Foo, Foo,
Table(map! { }), Table(map! { }),
"missing field `bar`",
"missing field `bar`" "missing field `bar`"
} }
} }