From 8e191ee11a7258dbfdb868cad93ec5d58ef567dc Mon Sep 17 00:00:00 2001 From: threeoh6000 Date: Fri, 19 Jan 2024 00:26:05 +0000 Subject: [PATCH] more tests plus bug fix relating to arrays in the lexer --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/formatter.rs | 3 ++- src/formatter_tests.rs | 2 +- src/lexer.rs | 2 +- tests/integration.rs | 47 ++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 51 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 195def2..eebac4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "frostwalker" -version = "0.0.6" +version = "0.0.7" diff --git a/Cargo.toml b/Cargo.toml index 4e06241..b8faa49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/formatter.rs b/src/formatter.rs index 027d178..f6c7a36 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; pub fn format(tree: Vec) -> HashMap { 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) -> HashMap { break; } } + output.insert(current_key.clone(), (current_index).to_string()); } i = i + 1; diff --git a/src/formatter_tests.rs b/src/formatter_tests.rs index f15d530..ee17122 100644 --- a/src/formatter_tests.rs +++ b/src/formatter_tests.rs @@ -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!(); } diff --git a/src/lexer.rs b/src/lexer.rs index f5691d2..3f4a149 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -16,7 +16,7 @@ pub fn tokenize(source: &str) -> Vec { 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, ","); } diff --git a/tests/integration.rs b/tests/integration.rs index 912a594..c35b247 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -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."); + } }