Skip to content

Commit

Permalink
test: Add tests for gathering location information from multi-line qu…
Browse files Browse the repository at this point in the history
…eries
  • Loading branch information
notheotherben committed Aug 7, 2024
1 parent e8235a7 commit dfac53b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/filter/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a> Scanner<'a> {
if let Some((idx, c)) = self.chars.peek() {
if *c == '\n' {
self.line += 1;
self.line_start = *idx;
self.line_start = *idx + 1;

Check warning on line 26 in src/filter/lexer.rs

View check run for this annotation

Codecov / codecov/patch

src/filter/lexer.rs#L25-L26

Added lines #L25 - L26 were not covered by tests
}

if *c == next {
Expand All @@ -40,7 +40,7 @@ impl<'a> Scanner<'a> {
while let Some((idx, c)) = self.chars.peek() {
if *c == '\n' {
self.line += 1;
self.line_start = *idx;
self.line_start = *idx + 1;

Check warning on line 43 in src/filter/lexer.rs

View check run for this annotation

Codecov / codecov/patch

src/filter/lexer.rs#L42-L43

Added lines #L42 - L43 were not covered by tests
}

if !f(*idx, *c) {
Expand All @@ -60,7 +60,7 @@ impl<'a> Scanner<'a> {
match c {
'\n' => {
self.line += 1;
self.line_start = idx;
self.line_start = idx + 1;
}

Check warning on line 64 in src/filter/lexer.rs

View check run for this annotation

Codecov / codecov/patch

src/filter/lexer.rs#L61-L64

Added lines #L61 - L64 were not covered by tests
'"' => {
return Ok(Token::String(start_loc, &self.source[start + 1..idx]));
Expand Down Expand Up @@ -126,7 +126,7 @@ impl<'a> Iterator for Scanner<'a> {
' ' | '\t' => {}
'\n' => {
self.line += 1;
self.line_start = idx;
self.line_start = idx + 1;
}
'(' => {
return Some(Ok(Token::LeftParen(Loc::new(
Expand Down Expand Up @@ -236,7 +236,7 @@ mod tests {
$(
match scanner.next() {
Some(Ok($item)) => {},
Some(Ok(item)) => panic!("Expected '{}' but got '{}'", stringify!($item), item),
Some(Ok(item)) => panic!("Expected '{}' but got '{:?}'", stringify!($item), item),
Some(Err(e)) => panic!("Error: {}", e),
None => panic!("Expected '{}' but got the end of the parse sequence instead", stringify!($item)),
}
Expand Down Expand Up @@ -338,4 +338,14 @@ mod tests {
Token::Property(.., "artifact.source-code"),
);
}

#[test]
fn test_location() {
assert_sequence!(
"true !=\nfalse",
Token::True(Loc { line: 1, column: 1 }),
Token::NotEquals(Loc { line: 1, column: 6 }),
Token::False(Loc { line: 2, column: 1 })
);
}
}
4 changes: 2 additions & 2 deletions src/filter/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fmt::Display;

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Copy, Clone, Hash, Default)]
pub struct Loc {
line: usize,
column: usize,
pub line: usize,
pub column: usize,
}

impl Loc {
Expand Down

0 comments on commit dfac53b

Please sign in to comment.