handle multiple scans

main
Alpha Chen 2 years ago
parent 277d64c85c
commit cba14f648a
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -271,41 +271,53 @@ mod tests {
#[test]
fn test_scan() {
assert_scan_token("(", TokenKind::LeftParen);
assert_source_scan("(", TokenKind::LeftParen);
assert_scan_token("!", TokenKind::Bang);
assert_scan_token("!=", TokenKind::BangEqual);
assert_scan_token("!a", TokenKind::Bang);
assert_source_scan("!", TokenKind::Bang);
assert_source_scan("!=", TokenKind::BangEqual);
assert_source_scan("!a", TokenKind::Bang);
assert_scan_token("a", TokenKind::Identifier);
assert_scan_token("_a", TokenKind::Identifier);
assert_scan_token("a1", TokenKind::Identifier);
assert_source_scan("a", TokenKind::Identifier);
assert_source_scan("_a", TokenKind::Identifier);
assert_source_scan("a1", TokenKind::Identifier);
assert_scan_token("\"foo\"", TokenKind::String);
assert_source_scan("\"foo\"", TokenKind::String);
assert_scan_token("1a", TokenKind::Number);
assert_scan_token("1.1", TokenKind::Number);
assert_source_scan("1a", TokenKind::Number);
assert_source_scan("1.1", TokenKind::Number);
assert_scan_token("a", TokenKind::Identifier);
assert_scan_token("an", TokenKind::Identifier);
assert_scan_token("and", TokenKind::And);
assert_scan_token("andy", TokenKind::Identifier);
assert_scan_token("false", TokenKind::False);
assert_scan_token("fa", TokenKind::Identifier);
assert_source_scan("a", TokenKind::Identifier);
assert_source_scan("an", TokenKind::Identifier);
assert_source_scan("and", TokenKind::And);
assert_source_scan("andy", TokenKind::Identifier);
assert_source_scan("false", TokenKind::False);
assert_source_scan("fa", TokenKind::Identifier);
assert_scan_token("@", TokenKind::Error);
assert_scan_token("", TokenKind::Eof);
assert_source_scan("@", TokenKind::Error);
assert_source_scan("", TokenKind::Eof);
}
#[test]
fn test_multi_scan() {
let mut scanner = Scanner::new("()");
assert_scan(&mut scanner, TokenKind::LeftParen);
assert_scan(&mut scanner, TokenKind::RightParen);
assert_scan(&mut scanner, TokenKind::Eof);
}
#[test]
fn test_whitespace() {
assert_scan_token(" foo", TokenKind::Identifier);
assert_scan_token("\tfoo", TokenKind::Identifier);
assert_scan_token("// \n", TokenKind::Eof);
assert_source_scan(" foo", TokenKind::Identifier);
assert_source_scan("\tfoo", TokenKind::Identifier);
assert_source_scan("// \n", TokenKind::Eof);
}
fn assert_scan_token(source: &str, kind: TokenKind) {
fn assert_source_scan(source: &str, kind: TokenKind) {
let mut scanner = Scanner::new(source);
assert_scan(&mut scanner, kind);
}
fn assert_scan(scanner: &mut Scanner, kind: TokenKind) {
let token = scanner.scan().unwrap();
assert_eq!(token.kind, kind);
}

Loading…
Cancel
Save