From 52f0b14c71837750cb119c3ef792f9db44e397d5 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Fri, 3 Jan 2025 00:49:59 -0800 Subject: [PATCH] Moving table_header_adder tests over to root adder module --- src/adder.rs | 179 +++++++++++++++++++++++++- src/adder/table_header_adder.rs | 218 -------------------------------- 2 files changed, 172 insertions(+), 225 deletions(-) diff --git a/src/adder.rs b/src/adder.rs index a6f6880..0bb4f04 100644 --- a/src/adder.rs +++ b/src/adder.rs @@ -154,6 +154,7 @@ fn add_in_generic_value(generic_value: &mut Value, last_field: &str, toml: Item) } #[cfg(test)] +#[macro_use] mod adder_tests { use super::*; @@ -176,22 +177,25 @@ test = "yo" none = "all""#; macro_rules! meta_add_test { - ($name:ident, $table_header_path:expr, $field:expr, $value:expr, $contents:expr, $expected:expr) => { + ($name:ident, $table_header_path:expr, $field:expr, $value:expr, $contents:expr, $expected:expr, $result:ident, $($assertion:stmt)*) => { #[test] fn $name() { let mut doc = $contents.parse::().unwrap(); let expected = $expected; - let field = $field; + let table_header_path = ($table_header_path as Option<&str>).map(|s| s.to_string()); + let field = ($field as Option<&str>).map(|s| s.to_owned()); let value = Some($value.to_string()); let op = AddOp { path: field, - table_header_path: $table_header_path, + table_header_path: table_header_path, dotted_path: None, value: value, }; - let result = handle_add(&mut doc, op); - assert!(result.is_ok(), "error: {:?}", result); + let $result = handle_add(&mut doc, op); + $( + $assertion + )* assert_eq!(doc.to_string().trim(), expected.trim()); } }; @@ -202,10 +206,46 @@ test = "yo" meta_add_test!( $name, None, - Some($field.to_owned()), + Some($field), + $value, + $contents, + $expected, + result, + { assert!(result.is_ok(), "error: {:?}", result) } + ); + }; + } + + #[macro_export] + macro_rules! add_table_header_test { + ($name:ident, $table_header_path:expr, $field:expr, $value:expr, $contents:expr, $expected:expr) => { + meta_add_test!( + $name, + $table_header_path, + $field, + $value, + $contents, + $expected, + result, + { assert!(result.is_ok(), "error: {:?}", result) } + ); + }; + } + + #[macro_export] + macro_rules! add_table_header_error_test { + ($name:ident, $table_header_path:expr, $field:expr, $value:expr, $contents:expr, $expected:expr) => { + meta_add_test!( + $name, + $table_header_path, + $field, $value, $contents, - $expected + $expected, + result, + { + assert!(result.is_err(), "expected an error, got : {:?}", result); + } ); }; } @@ -510,3 +550,128 @@ POETRY_CACHE_DIR = "${HOME}/${REPL_SLUG}/.cache/pypoetry" "# ); } + +#[cfg(test)] +mod table_header_adder_tests { + use super::*; + + add_table_header_test!( + test_one_element_table_header, + Some("moduleConfig"), + Some("interpreters/ruby/enable"), + "true", + "", + r#" +[moduleConfig] +interpreters.ruby.enable = true + "# + ); + + add_table_header_test!( + test_two_element_table_header, + Some("moduleConfig/interpreters"), + Some("ruby/enable"), + "true", + "", + r#" +[moduleConfig.interpreters] +ruby.enable = true + "# + ); + + add_table_header_test!( + test_preserve_existing, + Some("moduleConfig"), + Some("interpreters/ruby/enable"), + "true", + r#" +[moduleConfig] +bundles.go.enable = true + "#, + r#" +[moduleConfig] +bundles.go.enable = true +interpreters.ruby.enable = true +"# + ); + + add_table_header_test!( + test_preserve_existing_inner_tables, + Some("moduleConfig"), + Some("interpreters/ruby/version"), + "\"3.2.3\"", + r#" +[moduleConfig] +interpreter.ruby.enable = true + "#, + r#" +[moduleConfig] +interpreter.ruby.enable = true +interpreters.ruby.version = "3.2.3" + "# + ); + + add_table_header_error_test!( + test_error_when_adding_key_to_non_table, + Some("moduleConfig"), + Some("interpreters/ruby/version"), + "\"3.2.3\"", + r#" +[moduleConfig] +interpreters.ruby = "my dear" + "#, + r#" +[moduleConfig] +interpreters.ruby = "my dear" + "# + ); + + add_table_header_test!( + test_add_arrays_of_tables, + Some("tool/uv/index/[[]]"), + None, + r#" + {"key": "value"} + "#, + "", + r#" +[[tool.uv.index]] +key = "value" +"# + ); + + add_table_header_test!( + test_append_arrays_of_tables, + Some("tool/uv/index/[[]]"), + None, + r#" + {"key": "second"} + "#, + r#" +[[tool.uv.index]] +key = "first" + "#, + r#" +[[tool.uv.index]] +key = "first" + +[[tool.uv.index]] +key = "second" +"# + ); + + add_table_header_test!( + test_add_table_literal, + Some("tool/uv/sources"), + None, + r#" + {"torchvision": [{ "index": "pytorch-cpu", "marker": "platform_system == 'Linux'" }]} + "#, + r#" + "#, + r#" +[tool.uv.sources] +torchvision = [{ index = "pytorch-cpu", marker = "platform_system == 'Linux'" }] + "# + ); +} diff --git a/src/adder/table_header_adder.rs b/src/adder/table_header_adder.rs index 30a9636..e029843 100644 --- a/src/adder/table_header_adder.rs +++ b/src/adder/table_header_adder.rs @@ -146,221 +146,3 @@ fn add_value_with_dotted_path( }, } } - -#[cfg(test)] -mod table_header_adder_tests { - use super::*; - use toml_edit::{value, Array, DocumentMut, Formatted, InlineTable}; - - macro_rules! meta_add_test { - ($name:ident, $table_header:expr, $path:expr, $value:expr, $contents:expr, $expected:expr, $result:ident, $($assertion:stmt)*) => { - #[test] - fn $name() { - let mut doc = $contents.to_string().parse::().expect("invalid doc"); - - let mut table_header_path = $table_header - .iter() - .map(|s| s.to_string()) - .collect::>(); - // We need to be explicit to guide None towards a compatible type: - let dotted_path = ($path as Option>).map(|path| - path - .iter() - .map(|s| s.to_string()) - .collect::>()); - - let array_of_tables = if table_header_path.last().is_some_and(|k| k == "[[]]") { - table_header_path.pop(); - true - } else { - false - }; - let $result = add_value_with_table_header_and_dotted_path( - &mut doc, - &table_header_path, - dotted_path, - $value, - array_of_tables, - ); - $( - $assertion - )* - assert_eq!( - doc.to_string().trim(), - $expected.trim(), - ); - } - }; - } - - macro_rules! add_test { - ($name:ident, $table_header:expr, $path:expr, $value:expr, $contents:expr, $expected:expr) => { - meta_add_test!( - $name, - $table_header, - $path, - $value, - $contents, - $expected, - result, - { - assert!(result.is_ok(), "error: {:?}", result); - } - ); - }; - } - - macro_rules! add_error_test { - ($name:ident, $table_header:expr, $path:expr, $value:expr, $contents:expr, $expected:expr) => { - meta_add_test!( - $name, - $table_header, - $path, - $value, - $contents, - $expected, - result, - { - assert!(result.is_err(), "expected an error, got : {:?}", result); - } - ); - }; - } - - add_test!( - test_one_element_table_header, - vec!["moduleConfig"], - Some(vec!["interpreters", "ruby", "enable"]), - value(true), - "", - r#" -[moduleConfig] -interpreters.ruby.enable = true - "# - ); - - add_test!( - test_two_element_table_header, - vec!["moduleConfig", "interpreters"], - Some(vec!["ruby", "enable"]), - value(true), - "", - r#" -[moduleConfig.interpreters] -ruby.enable = true - "# - ); - - add_test!( - test_preserve_existing, - vec!["moduleConfig"], - Some(vec!["interpreters", "ruby", "enable"]), - value(true), - r#" -[moduleConfig] -bundles.go.enable = true - "#, - r#" -[moduleConfig] -bundles.go.enable = true -interpreters.ruby.enable = true -"# - ); - - add_test!( - test_preserve_existing_inner_tables, - vec!["moduleConfig"], - Some(vec!["interpreters", "ruby", "version"]), - value("3.2.3"), - r#" -[moduleConfig] -interpreter.ruby.enable = true - "#, - r#" -[moduleConfig] -interpreter.ruby.enable = true -interpreters.ruby.version = "3.2.3" - "# - ); - - add_error_test!( - test_error_when_adding_key_to_non_table, - vec!["moduleConfig"], - Some(vec!["interpreters", "ruby", "version"]), - value("3.2.3"), - r#" -[moduleConfig] -interpreters.ruby = "my dear" - "#, - r#" -[moduleConfig] -interpreters.ruby = "my dear" - "# - ); - - add_test!( - test_add_arrays_of_tables, - vec!["tool", "uv", "index", "[[]]"], - None, - { - let mut it = InlineTable::default(); - it.insert("key", Value::String(Formatted::new("value".to_owned()))); - Item::Value(Value::InlineTable(it)) - }, - "", - r#" -[[tool.uv.index]] -key = "value" -"# - ); - - add_test!( - test_append_arrays_of_tables, - vec!["tool", "uv", "index", "[[]]"], - None, - { - let mut it = InlineTable::default(); - it.insert("key", Value::String(Formatted::new("second".to_owned()))); - Item::Value(Value::InlineTable(it)) - }, - r#" -[[tool.uv.index]] -key = "first" - "#, - r#" -[[tool.uv.index]] -key = "first" - -[[tool.uv.index]] -key = "second" -"# - ); - - add_test!( - test_add_table_literal, - vec!["tool", "uv", "sources"], - None, - { - let mut pytorch_cpu = InlineTable::default(); - pytorch_cpu.insert( - "index", - Value::String(Formatted::new("pytorch-cpu".to_owned())), - ); - pytorch_cpu.insert( - "marker", - Value::String(Formatted::new("platform_system == 'Linux'".to_owned())), - ); - let mut tv = Array::new(); - tv.push(pytorch_cpu); - let mut it = InlineTable::default(); - it.insert("torchvision", Value::Array(tv)); - Item::Value(Value::InlineTable(it)) - }, - r#" - "#, - r#" -[tool.uv.sources] -torchvision = [{ index = "pytorch-cpu", marker = "platform_system == 'Linux'" }] - "# - ); -}