162 lines
5.9 KiB
Rust
162 lines
5.9 KiB
Rust
|
pub mod lexer;
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod lexer_tests {
|
||
|
use super::lexer::{self, Token, Class};
|
||
|
|
||
|
#[test]
|
||
|
fn single_key() {
|
||
|
let tree = lexer::tokenize("key = \"value\"");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
||
|
let manual_tree = vec![id, op, strn];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn single_key_escaped_double_quote_in_string() {
|
||
|
let tree = lexer::tokenize("key = \"\\\"value\"");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("\\\"value".to_string()) };
|
||
|
let manual_tree = vec![id, op, strn];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn integers() {
|
||
|
let tree = lexer::tokenize("key = 123567\r\nkey2 = -400");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::INTEGER, value: Some("123567".to_string()) };
|
||
|
let nl = Token { class: Class::NEWLINE, value: None };
|
||
|
let id2 = Token { class: Class::IDENTIFIER, value: Some("key2".to_string()) };
|
||
|
let op2 = Token { class: Class::EQUALS, value: None };
|
||
|
let strn2 = Token { class: Class::INTEGER, value: Some("-400".to_string()) };
|
||
|
let manual_tree = vec![id, op, strn, nl, id2, op2, strn2];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn booleans() {
|
||
|
let tree = lexer::tokenize("key = true\r\nkey2 = FALSE");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::TRUE, value: None };
|
||
|
let nl = Token { class: Class::NEWLINE, value: None };
|
||
|
let id2 = Token { class: Class::IDENTIFIER, value: Some("key2".to_string()) };
|
||
|
let op2 = Token { class: Class::EQUALS, value: None };
|
||
|
let strn2 = Token { class: Class::FALSE, value: None };
|
||
|
let manual_tree = vec![id, op, strn, nl, id2, op2, strn2];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn single_key_space_in_string() {
|
||
|
let tree = lexer::tokenize("key = \"a b c\"");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("a b c".to_string()) };
|
||
|
let manual_tree = vec![id, op, strn];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn single_key_double_space_in_string() {
|
||
|
let tree = lexer::tokenize("key = \"a b c d\"");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("a b c d".to_string()) };
|
||
|
let manual_tree = vec![id, op, strn];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn triple_key() {
|
||
|
let tree = lexer::tokenize("key = \"value\"\r\nkey2 = \"value2\"\r\nkey3 = \"value3\"");
|
||
|
|
||
|
let nl = Token { class: Class::NEWLINE, value: None };
|
||
|
let nl2 = Token { class: Class::NEWLINE, value: None };
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
||
|
let id2 = Token { class: Class::IDENTIFIER, value: Some("key2".to_string()) };
|
||
|
let op2 = Token { class: Class::EQUALS, value: None };
|
||
|
let strn2 = Token { class: Class::STRING, value: Some("value2".to_string()) };
|
||
|
let id3 = Token { class: Class::IDENTIFIER, value: Some("key3".to_string()) };
|
||
|
let op3 = Token { class: Class::EQUALS, value: None };
|
||
|
let strn3 = Token { class: Class::STRING, value: Some("value3".to_string()) };
|
||
|
|
||
|
let manual_tree = vec![id, op, strn, nl, id2, op2, strn2, nl2, id3, op3, strn3];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn comment() {
|
||
|
let tree = lexer::tokenize("# This is a comment\r\nkey = \"value\"");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
||
|
|
||
|
let manual_tree = vec![id, op, strn];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn inline_comment() {
|
||
|
let tree = lexer::tokenize("# This is a comment\r\nkey = \"value\" # I'm inline!");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
||
|
|
||
|
let manual_tree = vec![id, op, strn];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn inline_comment_no_spaces() {
|
||
|
let tree = lexer::tokenize("#This is a comment\r\nkey = \"value\" #I'm inline!");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
||
|
|
||
|
let manual_tree = vec![id, op, strn];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn quoted_identifier() {
|
||
|
let tree = lexer::tokenize("\"key\" = \"value\"");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let strn = Token { class: Class::STRING, value: Some("value".to_string()) };
|
||
|
|
||
|
let manual_tree = vec![id, op, strn];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn double_equals() {
|
||
|
let tree = lexer::tokenize("\"key\" = =");
|
||
|
|
||
|
let id = Token { class: Class::IDENTIFIER, value: Some("key".to_string()) };
|
||
|
let op = Token { class: Class::EQUALS, value: None };
|
||
|
let op2 = Token { class: Class::EQUALS, value: None };
|
||
|
|
||
|
let manual_tree = vec![id, op, op2];
|
||
|
assert_eq!(tree.unwrap(), manual_tree);
|
||
|
}
|
||
|
}
|
||
|
|