Skip to content

Commit

Permalink
fix: don't allow empty string in range, test coverage improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloso committed Oct 27, 2023
1 parent acda169 commit afc2c29
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pomsky-lib/tests/testcases/codepoints/e_surrogate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! expect=error
U+D801
-----
ERROR: This code point is outside the allowed range
SPAN: 0..6
5 changes: 5 additions & 0 deletions pomsky-lib/tests/testcases/codepoints/e_too_large.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! expect=error
U+EEEEEE
-----
ERROR: This code point is outside the allowed range
SPAN: 0..8
5 changes: 5 additions & 0 deletions pomsky-lib/tests/testcases/codepoints/e_too_long.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! expect=error
U+12345678
-----
ERROR: This code point is outside the allowed range
SPAN: 0..10
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! expect=error
[''-'z']
-----
ERROR: Strings used in ranges can't be empty
SPAN: 1..3
6 changes: 6 additions & 0 deletions pomsky-lib/tests/testcases/errors/negated_shorthand_twice.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! expect=error
[!!d]
-----
ERROR: A shorthand character class can't be negated more than once
HELP: The number of exclamation marks is even, so you can remove all of them
SPAN: 1..3
6 changes: 6 additions & 0 deletions pomsky-lib/tests/testcases/errors/set_range_descending.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! expect=error
['z'-'g']
-----
ERROR: Character range must be in increasing order, but it is U+007A - U+0067
HELP: Switch the characters: 'g'-'z'
SPAN: 1..8
5 changes: 5 additions & 0 deletions pomsky-lib/tests/testcases/ranges/e_empty_string.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! expect=error
range ''-'0'
-----
ERROR: cannot parse integer from empty string
SPAN: 6..8
9 changes: 6 additions & 3 deletions pomsky-syntax/src/parse/parser_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,7 @@ impl<'i> Parser<'i> {
}

let hex = trimmed_u.trim_start_matches(|c: char| c == '+' || c.is_whitespace());
if hex.len() > 6 {
return Err(PEK::InvalidCodePoint.at(span));
}

u32::from_str_radix(hex, 16)
.ok()
.and_then(|n| char::try_from(n).ok())
Expand Down Expand Up @@ -751,6 +749,11 @@ impl<'i> Parser<'i> {
let end = helper::parse_number(helper::strip_first_last(second), radix)
.map_err(|k| PEK::from(k).at(span_2))?;

if start.is_empty() || end.is_empty() {
let span = if start.is_empty() { span_1 } else { span_2 };
return Err(PEK::Number(NumberError::Empty).at(span));
}

if start.len() > end.len() || (start.len() == end.len() && start > end) {
return Err(PEK::RangeIsNotIncreasing.at(span_1.join(span_2)));
}
Expand Down

0 comments on commit afc2c29

Please sign in to comment.