Test number literals with underscores

This commit is contained in:
Richard Feldman 2019-09-14 17:54:28 -05:00
parent 9f39153a35
commit a282ecf07b

View file

@ -308,6 +308,17 @@ mod test_parser {
assert_parses_to(i64::MIN.to_string().as_str(), Int(i64::MIN)); assert_parses_to(i64::MIN.to_string().as_str(), Int(i64::MIN));
} }
#[test]
fn int_with_underscore() {
assert_parses_to("1_2_34_567", Int(1234567));
assert_parses_to("-1_2_34_567", Int(-1234567));
// The following cases are silly. They aren't supported on purpose,
// but there would be a performance cost to explicitly disallowing them,
// which doesn't seem like it would benefit anyone.
assert_parses_to("1_", Int(1));
assert_parses_to("1__23", Int(123));
}
#[quickcheck] #[quickcheck]
fn all_i64_values_parse(num: i64) { fn all_i64_values_parse(num: i64) {
assert_parses_to(num.to_string().as_str(), Int(num)); assert_parses_to(num.to_string().as_str(), Int(num));
@ -362,6 +373,12 @@ mod test_parser {
assert_parses_to(&format!("{}.0", f64::MIN), Float(f64::MIN)); assert_parses_to(&format!("{}.0", f64::MIN), Float(f64::MIN));
} }
#[test]
fn float_with_underscores() {
assert_parses_to("1_23_456.0_1_23_456", Float(123456.0123456));
assert_parses_to("-1_23_456.0_1_23_456", Float(-123456.0123456));
}
#[quickcheck] #[quickcheck]
fn all_f64_values_parse(num: f64) { fn all_f64_values_parse(num: f64) {
assert_parses_to(num.to_string().as_str(), Float(num)); assert_parses_to(num.to_string().as_str(), Float(num));
@ -382,55 +399,4 @@ mod test_parser {
MalformedFloat(Problem::OutsideSupportedRange), MalformedFloat(Problem::OutsideSupportedRange),
); );
} }
// fn expect_parsed_float<'a>(expected: f64, actual: &str) {
// assert_eq!(
// Ok((Float(expected), "".to_string())),
// parse_without_loc(actual)
// );
// }
// fn expect_parsed_int<'a>(expected: i64, actual: &str) {
// assert_eq!(
// Ok((Int(expected), "".to_string())),
// parse_without_loc(actual)
// );
// }
// #[test]
// fn positive_int() {
// expect_parsed_int(1234, "1234");
// }
// #[test]
// fn negative_int() {
// expect_parsed_int(-1234, "-1234");
// }
// #[test]
// fn positive_float() {
// expect_parsed_float(123.45, "123.45");
// expect_parsed_float(42.00, "42.00");
// }
// #[test]
// fn negative_float() {
// expect_parsed_float(-1234.567, "-1234.567");
// expect_parsed_float(-192.0, "-192.0");
// }
// #[test]
// fn ints_with_underscores() {
// expect_parsed_int(987654321, "987_6_5_432_1");
// expect_parsed_int(-1234567890, "-1_234_567_890");
// }
// #[test]
// fn fracs_with_spaces() {
// expect_parsed_float(-1234.567, "-1_23_4.567");
// expect_parsed_float(-192.0, "-19_2.0");
// expect_parsed_float(123.45, "1_2_3.45");
// expect_parsed_float(42.00, "4_2.00");
// }
} }