more tests plus bug fix relating to arrays in the lexer

This commit is contained in:
abbie 2024-01-19 00:26:05 +00:00
parent ed882263a8
commit 8e191ee11a
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
6 changed files with 51 additions and 7 deletions

2
Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 3
[[package]]
name = "frostwalker"
version = "0.0.6"
version = "0.0.7"

View file

@ -1,6 +1,6 @@
[package]
name = "frostwalker"
version = "0.0.6"
version = "0.0.7"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -4,7 +4,7 @@ use std::collections::HashMap;
pub fn format(tree: Vec<Token>) -> HashMap<String, String> {
let mut output = HashMap::new();
let mut current_key = "".to_string();
let mut current_index = 0;
let mut current_index;
let mut i = 0;
while i < tree.len() {
if tree[i].class == Class::IDENTIFIER {
@ -33,6 +33,7 @@ pub fn format(tree: Vec<Token>) -> HashMap<String, String> {
break;
}
}
output.insert(current_key.clone(), (current_index).to_string());
}
i = i + 1;

View file

@ -53,7 +53,7 @@ fn array() {
let manual_tree = vec![id, op, t1, strn, t2, strn2, t3];
let tree = formatter::format(manual_tree);
if !(tree.get("key[0]").unwrap() == "value") || !(tree.get("key[1]").unwrap() == "6") {
if !(tree.get("key[0]").unwrap() == "value") || !(tree.get("key[1]").unwrap() == "6") || !(tree.get("key").unwrap() == "2") {
println!("{:?}", tree);
panic!();
}

View file

@ -16,7 +16,7 @@ pub fn tokenize(source: &str) -> Vec<Token> {
let mut i = 0;
while i < words.len() {
if words[i].ends_with(",") && words[i-1] == "[" {
if words[i].ends_with(",") && (words[i-1] == "[" || words[i-1] == ",") {
words[i] = words[i].chars().next_back().map(|_| &words[i][..words[i].len()-1]).unwrap_or("");
words.insert(i+1, ",");
}

View file

@ -1,5 +1,6 @@
use frostwalker::lexer;
use frostwalker::validator;
use frostwalker::formatter;
#[test]
fn lv_single_key() {
@ -16,15 +17,57 @@ fn lv_triple_key() {
}
#[test]
#[should_panic]
fn lv_triple_key_missing_newline() {
let tree = lexer::tokenize("key = \"value\"key1 = 128\nkey2 = [ 6, 7 ]");
let result = validator::validate(&tree);
assert_eq!(result.is_some(), true);
assert_eq!(result.is_none(), true);
}
#[test]
#[should_panic]
fn lv_double_equals() {
let tree = lexer::tokenize("key = = \"value\"\r\nkey1 = 128\nkey2 = [ 6, 7 ]");
let result = validator::validate(&tree);
assert_eq!(result.is_some(), true);
assert_eq!(result.is_none(), true);
}
#[test]
fn full_stack_single_key() {
let tree = lexer::tokenize("key = \"value\"");
let result = validator::validate(&tree);
if result.is_some() {
panic!("{}", result.unwrap());
}
let hashmap = formatter::format(tree);
if !(hashmap.get("key").unwrap() == "value") {
panic!("Formatter error.");
}
}
#[test]
fn full_stack_varied_array() {
let tree = lexer::tokenize("key = [ \"value\", 150, -30, \"\\\"value\" ]");
let result = validator::validate(&tree);
if result.is_some() {
panic!("{}", result.unwrap());
}
let hashmap = formatter::format(tree);
if !(hashmap.get("key").unwrap() == "4") || !(hashmap.get("key[0]").unwrap() == "value") || !(hashmap.get("key[1]").unwrap() == "150") || !(hashmap.get("key[2]").unwrap() == "-30") || !(hashmap.get("key[3]").unwrap() == "\"value") {
panic!("Formatter error.");
}
}
#[test]
#[should_panic]
fn full_stack_single_key_double_equals() {
let tree = lexer::tokenize("key = = \"value\"");
let result = validator::validate(&tree);
if result.is_some() {
panic!("{}", result.unwrap());
}
let hashmap = formatter::format(tree);
if !(hashmap.get("key").unwrap() == "value") {
panic!("Formatter error.");
}
}