Test too big/small int literals

This commit is contained in:
Richard Feldman 2019-09-14 16:03:15 -05:00
parent dfd1d4bbb4
commit 5e740da48c
3 changed files with 22 additions and 5 deletions

View file

@ -111,12 +111,12 @@ where
match f64_buf.parse::<f64>() { match f64_buf.parse::<f64>() {
Ok(float) => Expr::Float(float), Ok(float) => Expr::Float(float),
Err(_) => Expr::MalformedNumber(Problem::TooLarge), Err(_) => Expr::MalformedNumber(Problem::OutsideSupportedRange),
} }
} else { } else {
match before_decimal.parse::<i64>() { match before_decimal.parse::<i64>() {
Ok(int_val) => Expr::Int(int_val), Ok(int_val) => Expr::Int(int_val),
Err(_) => Expr::MalformedNumber(Problem::TooLarge), Err(_) => Expr::MalformedNumber(Problem::OutsideSupportedRange),
} }
}; };

View file

@ -21,5 +21,5 @@ pub enum Problem {
UnsupportedEscapedChar, UnsupportedEscapedChar,
// NUMBER LITERAL // NUMBER LITERAL
TooLarge, OutsideSupportedRange,
} }

View file

@ -18,6 +18,7 @@ mod test_parser {
use roc::parse::parser::{Fail, FailReason, Parser, State}; use roc::parse::parser::{Fail, FailReason, Parser, State};
use roc::parse::problems::Problem; use roc::parse::problems::Problem;
use roc::region::{Located, Region}; use roc::region::{Located, Region};
use std::i64;
fn assert_parses_to<'a>(input: &'a str, expected_expr: Expr<'a>) { fn assert_parses_to<'a>(input: &'a str, expected_expr: Expr<'a>) {
let state = State::new(&input, Attempting::Module); let state = State::new(&input, Attempting::Module);
@ -287,14 +288,30 @@ mod test_parser {
fn positive_int() { fn positive_int() {
assert_parses_to("1", Int(1)); assert_parses_to("1", Int(1));
assert_parses_to("42", Int(42)); assert_parses_to("42", Int(42));
assert_parses_to(&std::i64::MAX.to_string(), Int(std::i64::MAX)); assert_parses_to(i64::MAX.to_string().as_str(), Int(std::i64::MAX));
} }
#[test] #[test]
fn negative_int() { fn negative_int() {
assert_parses_to("-1", Int(-1)); assert_parses_to("-1", Int(-1));
assert_parses_to("-42", Int(-42)); assert_parses_to("-42", Int(-42));
assert_parses_to(&std::i64::MIN.to_string(), Int(std::i64::MIN)); assert_parses_to(i64::MIN.to_string().as_str(), Int(std::i64::MIN));
}
#[test]
fn int_too_large() {
assert_parses_to(
(i64::MAX as i128 + 1).to_string().as_str(),
MalformedNumber(Problem::OutsideSupportedRange),
);
}
#[test]
fn int_too_small() {
assert_parses_to(
(i64::MIN as i128 - 1).to_string().as_str(),
MalformedNumber(Problem::OutsideSupportedRange),
);
} }
// fn expect_parsed_float<'a>(expected: f64, actual: &str) { // fn expect_parsed_float<'a>(expected: f64, actual: &str) {