Skip to content

Commit

Permalink
test(parser): Move parser tests next to there definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 22, 2022
1 parent 2839693 commit 3082067
Show file tree
Hide file tree
Showing 11 changed files with 565 additions and 518 deletions.
55 changes: 55 additions & 0 deletions crates/toml_edit/src/parser/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,58 @@ parse!(array_value() -> Value, {
Ok(v)
})
});

#[cfg(test)]
mod test {
use super::*;

use combine::stream::position::Stream;

#[test]
fn arrays() {
let inputs = [
r#"[]"#,
r#"[ ]"#,
r#"[
1, 2, 3
]"#,
r#"[
1,
2, # this is ok
]"#,
r#"[# comment
# comment2
]"#,
r#"[# comment
# comment2
1
#sd
,
# comment3
]"#,
r#"[1]"#,
r#"[1,]"#,
r#"[ "all", 'strings', """are the same""", '''type''']"#,
r#"[ 100, -2,]"#,
r#"[1, 2, 3]"#,
r#"[1.1, 2.1, 3.1]"#,
r#"["a", "b", "c"]"#,
r#"[ [ 1, 2 ], [3, 4, 5] ]"#,
r#"[ [ 1, 2 ], ["a", "b", "c"] ]"#,
r#"[ { x = 1, a = "2" }, {a = "a",b = "b", c = "c"} ]"#,
];
for input in inputs {
parsed_value_eq!(input);
}

let invalid_inputs = [r#"["#, r#"[,]"#, r#"[,2]"#, r#"[1e165,,]"#];
for input in invalid_inputs {
let parsed = array().easy_parse(Stream::new(input.as_bytes()));
assert!(parsed.is_err());
}
}
}
45 changes: 45 additions & 0 deletions crates/toml_edit/src/parser/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,48 @@ parse!(unsigned_digits(count: usize) -> u32, {
s.parse::<u32>()
})
});

#[cfg(test)]
mod test {
#[test]
fn offset_date_time() {
let inputs = [
"1979-05-27T07:32:00Z",
"1979-05-27T00:32:00-07:00",
"1979-05-27T00:32:00.999999-07:00",
];
for input in inputs {
parsed_date_time_eq!(input, is_datetime);
}
}

#[test]
fn local_date_time() {
let inputs = ["1979-05-27T07:32:00", "1979-05-27T00:32:00.999999"];
for input in inputs {
parsed_date_time_eq!(input, is_datetime);
}
}

#[test]
fn local_date() {
let inputs = ["1979-05-27", "2017-07-20"];
for input in inputs {
parsed_date_time_eq!(input, is_datetime);
}
}

#[test]
fn local_time() {
let inputs = ["07:32:00", "00:32:00.999999"];
for input in inputs {
parsed_date_time_eq!(input, is_datetime);
}
}

#[test]
fn time_fraction_truncated() {
let input = "1987-07-05T17:45:00.123456789012345Z";
parsed_date_time_eq!(input, is_datetime);
}
}
111 changes: 111 additions & 0 deletions crates/toml_edit/src/parser/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,114 @@ parser! {
})
}
}

#[cfg(test)]
mod test {
use super::*;

use snapbox::assert_eq;

#[test]
fn documents() {
let documents = [
r#"
# This is a TOML document.
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
[servers]
# Indentation (tabs and/or spaces) is allowed but not required
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
[clients]
data = [ ["gamma", "delta"], [1, 2] ]
# Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]
'some.wierd .stuff' = """
like
that
# """ # this broke my sintax highlighting
" also. like " = '''
that
'''
double = 2e39 # this number looks familiar
# trailing comment"#,
r#""#,
r#" "#,
r#" hello = 'darkness' # my old friend
"#,
r#"[parent . child]
key = "value"
"#,
r#"hello.world = "a"
"#,
r#"foo = 1979-05-27 # Comment
"#,
];
for input in documents {
let doc = document(input.as_bytes());
let doc = match doc {
Ok(doc) => doc,
Err(err) => {
panic!(
"Parse error: {}\nFailed to parse:\n```\n{}\n```",
err, input
)
}
};

dbg!(doc.to_string());
dbg!(input);
assert_eq(input, doc.to_string());
}

let parse_only = ["\u{FEFF}
[package]
name = \"foo\"
version = \"0.0.1\"
authors = []
"];
for input in parse_only {
let doc = document(input.as_bytes());
match doc {
Ok(_) => (),
Err(err) => {
panic!(
"Parse error: {}\nFailed to parse:\n```\n{}\n```",
err, input
)
}
}
}

let invalid_inputs = [r#" hello = 'darkness' # my old friend
$"#];
for input in invalid_inputs {
let doc = document(input.as_bytes());

assert!(doc.is_err());
}
}
}
26 changes: 26 additions & 0 deletions crates/toml_edit/src/parser/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,29 @@ parse!(keyval() -> (Vec<Key>, TableKeyValue), {
)
})
});

#[cfg(test)]
mod test {
use super::*;

use combine::stream::position::Stream;

#[test]
fn inline_tables() {
let inputs = [
r#"{}"#,
r#"{ }"#,
r#"{a = 1e165}"#,
r#"{ hello = "world", a = 1}"#,
r#"{ hello.world = "a" }"#,
];
for input in inputs {
parsed_value_eq!(input);
}
let invalid_inputs = [r#"{a = 1e165"#, r#"{ hello = "world", a = 2, hello = 1}"#];
for input in invalid_inputs {
let parsed = inline_table().easy_parse(Stream::new(input.as_bytes()));
assert!(parsed.is_err());
}
}
}
25 changes: 25 additions & 0 deletions crates/toml_edit/src/parser/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,28 @@ pub(crate) fn is_unquoted_char(c: u8) -> bool {

// dot-sep = ws %x2E ws ; . Period
const DOT_SEP: u8 = b'.';

#[cfg(test)]
mod test {
use super::*;

use combine::stream::position::Stream;
use snapbox::assert_eq;

#[test]
fn keys() {
let cases = [
("a", "a"),
(r#""hello\n ""#, "hello\n "),
(r#"'hello\n '"#, "hello\\n "),
];

for (input, expected) in cases {
let parsed = simple_key().easy_parse(Stream::new(input.as_bytes()));
assert!(parsed.is_ok());
let ((.., k), rest) = parsed.unwrap();
assert_eq(k.as_str(), expected);
assert_eq!(rest.input.len(), 0);
}
}
}
65 changes: 65 additions & 0 deletions crates/toml_edit/src/parser/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,68 @@ macro_rules! toml_parser (
}
);
);

#[cfg(test)]
macro_rules! parsed_eq {
($parsed:ident, $expected:expr) => {{
assert!($parsed.is_ok(), "{:?}", $parsed.err().unwrap());
let (v, rest) = $parsed.unwrap();
assert_eq!(v, $expected);
assert!(rest.input.is_empty());
}};
}

#[cfg(test)]
macro_rules! parsed_float_eq {
($input:ident, $expected:expr) => {{
let parsed = crate::parser::numbers::float().easy_parse(Stream::new($input.as_bytes()));
let (v, rest) = match parsed {
Ok(parsed) => parsed,
Err(err) => {
panic!("Unexpected error for {:?}: {:?}", $input, err);
}
};
if $expected.is_nan() {
assert!(v.is_nan());
} else if $expected.is_infinite() {
assert!(v.is_infinite());
assert_eq!($expected.is_sign_positive(), v.is_sign_positive());
} else {
dbg!($expected);
dbg!(v);
assert!(($expected - v).abs() < std::f64::EPSILON);
}
assert!(rest.input.is_empty());
}};
}

#[cfg(test)]
macro_rules! parsed_value_eq {
($input:expr) => {
use combine::EasyParser;
let parsed = crate::parser::value::value()
.easy_parse(combine::stream::position::Stream::new($input.as_bytes()));
let (v, rest) = match parsed {
Ok(parsed) => parsed,
Err(err) => {
panic!("Unexpected error for {:?}: {:?}", $input, err);
}
};
snapbox::assert_eq(v.to_string(), $input);
assert!(rest.input.is_empty());
};
}

#[cfg(test)]
macro_rules! parsed_date_time_eq {
($input:expr, $is:ident) => {{
use combine::EasyParser;
let parsed = crate::parser::value::value()
.easy_parse(combine::stream::position::Stream::new($input.as_bytes()));
assert!(parsed.is_ok());
let (v, rest) = parsed.unwrap();
snapbox::assert_eq(v.to_string(), $input);
assert!(rest.input.is_empty());
assert!(v.$is());
}};
}
Loading

0 comments on commit 3082067

Please sign in to comment.