From af5cdb4bda43e3b65d441857d9af6b02d6775789 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 24 May 2024 02:27:07 -0400 Subject: [PATCH 1/3] tests: show issue 27 Signed-off-by: Henry Schreiner --- rust/src/build_system.rs | 19 ++++++++++++++++ rust/src/helpers/table.rs | 21 +++++++++++++++++ rust/src/main.rs | 23 +++++++++++++++++++ tests/test_main.py | 47 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/rust/src/build_system.rs b/rust/src/build_system.rs index b7b49bb..84e4926 100644 --- a/rust/src/build_system.rs +++ b/rust/src/build_system.rs @@ -107,6 +107,25 @@ mod tests { [[build-system.a]] # empty table within the array [[build-system.a]] name = "Nail" + "#}, + false + )] + #[case::reorder( + indoc ! {r#" + [build-system] + requires = [ + "hatchling", + ] + build-backend = "hatchling.build" + + "#}, + indoc ! {r#" + [build-system] + build-backend = "hatchling.build" + requires = [ + "hatchling", + ] + "#}, false )] diff --git a/rust/src/helpers/table.rs b/rust/src/helpers/table.rs index 2addc76..d83cd53 100644 --- a/rust/src/helpers/table.rs +++ b/rust/src/helpers/table.rs @@ -316,3 +316,24 @@ pub fn collapse_sub_tables(tables: &mut Tables, name: &str) { sub.clear(); } } + +#[cfg(test)] +mod tests { + use super::*; + + use taplo::parser::parse; + + #[test] + fn test_reorder() { + let root_ast = parse("[A]\nb = 1\na = 1\n\n[B]\nb = 2") + .into_syntax() + .clone_for_update(); + let mut tables = Tables::from_ast(&root_ast); + { + let table = &mut tables.get("A").unwrap().first().unwrap().borrow_mut(); + reorder_table_keys(table, &["", "a", "b"]); + } + tables.reorder(&root_ast, &["B", "A"]); + assert_eq!(root_ast.to_string(), "[B]\nb = 2\n\n[A]\na = 1\n\nb = 1\n\n"); + } +} diff --git a/rust/src/main.rs b/rust/src/main.rs index 5abda5f..01232d6 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -272,6 +272,29 @@ mod tests { true, (3, 8) )] + #[case::space_issue_27( + indoc ! {r#" + [build-system] + requires = [ + "hatchling", + ] + build-backend = "hatchling.build" + + [project] + "#}, + indoc ! {r#" + [build-system] + build-backend = "hatchling.build" + requires = [ + "hatchling", + ] + + [project] + "#}, + 2, + true, + (3, 8) + )] fn test_format_toml( #[case] start: &str, #[case] expected: &str, diff --git a/tests/test_main.py b/tests/test_main.py index 8216701..3453136 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -66,6 +66,53 @@ """, id="collapsed", ), + pytest.param( + """ + [build-system] + requires = [ + "hatchling", + ] + build-backend = "hatchling.build" + + [project] + keywords = [ + "A", + ] + classifiers = [ + "Programming Language :: Python :: 3 :: Only", + ] + dynamic = [ + "B", + ] + dependencies = [ + "requests>=2.0", + ] + """, + """\ + [build-system] + build-backend = "hatchling.build" + requires = [ + "hatchling", + ] + + [project] + keywords = [ + "A", + ] + classifiers = [ + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + ] + dynamic = [ + "B", + ] + dependencies = [ + "requests>=2.0", + ] + """, + id="multi", + ), ], ) def test_format_toml(start: str, expected: str) -> None: From c3228f73846cfb7072ad8ef35de6edb657427886 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 18 Sep 2024 00:14:56 -0400 Subject: [PATCH 2/3] chore: fix one lint in test Signed-off-by: Henry Schreiner --- rust/src/helpers/table.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/src/helpers/table.rs b/rust/src/helpers/table.rs index d83cd53..96b0fd9 100644 --- a/rust/src/helpers/table.rs +++ b/rust/src/helpers/table.rs @@ -328,7 +328,7 @@ mod tests { let root_ast = parse("[A]\nb = 1\na = 1\n\n[B]\nb = 2") .into_syntax() .clone_for_update(); - let mut tables = Tables::from_ast(&root_ast); + let tables = Tables::from_ast(&root_ast); { let table = &mut tables.get("A").unwrap().first().unwrap().borrow_mut(); reorder_table_keys(table, &["", "a", "b"]); From b48069e6ec7db2ffe2bf05838aae5eaa06fc4f8e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 18 Sep 2024 14:11:26 -0400 Subject: [PATCH 3/3] tests: add a table test Signed-off-by: Henry Schreiner --- rust/src/helpers/table.rs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/rust/src/helpers/table.rs b/rust/src/helpers/table.rs index 96b0fd9..eb18610 100644 --- a/rust/src/helpers/table.rs +++ b/rust/src/helpers/table.rs @@ -321,19 +321,46 @@ pub fn collapse_sub_tables(tables: &mut Tables, name: &str) { mod tests { use super::*; + use indoc::indoc; use taplo::parser::parse; #[test] fn test_reorder() { - let root_ast = parse("[A]\nb = 1\na = 1\n\n[B]\nb = 2") - .into_syntax() - .clone_for_update(); + let root_ast = parse(indoc! {r#" + [A] + b = 1 + a = 1 + + [B] + b = 2 + [C] + b = 3 + # Notes on A + a = 3"#}) + .into_syntax() + .clone_for_update(); let tables = Tables::from_ast(&root_ast); { let table = &mut tables.get("A").unwrap().first().unwrap().borrow_mut(); reorder_table_keys(table, &["", "a", "b"]); } - tables.reorder(&root_ast, &["B", "A"]); - assert_eq!(root_ast.to_string(), "[B]\nb = 2\n\n[A]\na = 1\n\nb = 1\n\n"); + tables.reorder(&root_ast, &["C", "B", "A"]); + assert_eq!( + root_ast.to_string(), + indoc! {r#" + [C] + # Notes on A + a = 3 + b = 3 + + [B] + b = 2 + + [A] + a = 1 + b = 1 + + "#} + ); } }