From a282ecf07bd7002ba74358bccd462b2093035996 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sat, 14 Sep 2019 17:54:28 -0500 Subject: [PATCH] Test number literals with underscores --- tests/test_parse.rs | 68 ++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 51 deletions(-) diff --git a/tests/test_parse.rs b/tests/test_parse.rs index 448a043812..b14dd5e560 100644 --- a/tests/test_parse.rs +++ b/tests/test_parse.rs @@ -308,6 +308,17 @@ mod test_parser { 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] fn all_i64_values_parse(num: i64) { 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)); } + #[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] fn all_f64_values_parse(num: f64) { assert_parses_to(num.to_string().as_str(), Float(num)); @@ -382,55 +399,4 @@ mod test_parser { 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"); - // } - }