Skip to content

fix: small bug in whitespace_tokens, added a few tests for such bugs. #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions crates/pg_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn whitespace_tokens(input: &str) -> VecDeque<Token> {
} else if let Some(tab) = cap.name("tab") {
tokens.push_back(Token {
token_type: TokenType::Whitespace,
kind: SyntaxKind::Newline,
kind: SyntaxKind::Tab,
text: tab.as_str().to_string(),
span: TextRange::new(
TextSize::from(u32::try_from(tab.start()).unwrap()),
Expand Down Expand Up @@ -177,9 +177,30 @@ pub fn lex(text: &str) -> Vec<Token> {
mod tests {
use super::*;

#[test]
fn test_tab_tokens() {
let input = "select\t1";
let tokens = lex(input);
assert_eq!(tokens[1].kind, SyntaxKind::Tab);
}

#[test]
fn test_newline_tokens() {
let input = "select\n1";
let tokens = lex(input);
assert_eq!(tokens[1].kind, SyntaxKind::Newline);
}

#[test]
fn test_whitespace_tokens() {
let input = "select 1";
let tokens = lex(input);
assert_eq!(tokens[1].kind, SyntaxKind::Whitespace);
}

#[test]
fn test_lexer() {
let input = "select 1; \n -- some comment \n select 2";
let input = "select 1; \n -- some comment \n select 2\t";

let tokens = lex(input);
let mut tokens_iter = tokens.iter();
Expand Down Expand Up @@ -227,5 +248,8 @@ mod tests {
let token = tokens_iter.next().unwrap();
assert_eq!(token.kind, SyntaxKind::Iconst);
assert_eq!(token.text, "2");

let token = tokens_iter.next().unwrap();
assert_eq!(token.kind, SyntaxKind::Tab);
}
}
Loading