From 279677fb7cdcde7d6eb98dd029bab2e9f38753fc Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Sun, 10 Apr 2022 15:42:19 +0200 Subject: [PATCH] master > master: code-rust - String-type macro --- code/rust/src/rules/convert.rs | 20 +++++++++++ code/rust/src/rules/mod.rs | 1 + code/rust/tests/test_graphs/test_graph.rs | 14 +++----- code/rust/tests/test_rules/mod.rs | 1 + code/rust/tests/test_rules/test_convert.rs | 42 ++++++++++++++++++++++ 5 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 code/rust/src/rules/convert.rs create mode 100644 code/rust/tests/test_rules/test_convert.rs diff --git a/code/rust/src/rules/convert.rs b/code/rust/src/rules/convert.rs new file mode 100644 index 0000000..996d2df --- /dev/null +++ b/code/rust/src/rules/convert.rs @@ -0,0 +1,20 @@ +// ---------------------------------------------------------------- +// IMPORTS +// ---------------------------------------------------------------- + +// + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// Conversion macros - arrays +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +/// Allows simpler inline creation of vectors of Strings +#[macro_export] +macro_rules! vec_of_strings{ + () => { + Vec::::new() + }; + ($element:expr $(, $elements:expr )* $(,)?)=>{ + vec![ $element.to_string() $(, $elements.to_string() )* ] + }; +} diff --git a/code/rust/src/rules/mod.rs b/code/rust/src/rules/mod.rs index 0897e2a..48e5fe6 100644 --- a/code/rust/src/rules/mod.rs +++ b/code/rust/src/rules/mod.rs @@ -1 +1,2 @@ pub mod assert; +pub mod convert; diff --git a/code/rust/tests/test_graphs/test_graph.rs b/code/rust/tests/test_graphs/test_graph.rs index 0db3611..d45e79b 100644 --- a/code/rust/tests/test_graphs/test_graph.rs +++ b/code/rust/tests/test_graphs/test_graph.rs @@ -4,6 +4,7 @@ use rstest::fixture; use rstest::rstest; +use std::panic::catch_unwind; use ads2::graphs::graph; use ads2::*; @@ -25,28 +26,21 @@ fn fixture_graph() -> graph::Graph { // ---------------------------------------------------------------- #[test] -fn test_graph_create_noerror() { - let result = std::panic::catch_unwind(|| { +fn test_graph_creation() { + assert!(catch_unwind(|| { let gph = graph::Graph::new( vec![5, 7, 8], vec![(5,7), (7, 8)] ); assert_length!(gph, 3); - }); - assert!(result.is_ok()); - let result = std::panic::catch_unwind(|| { let gph = graph::Graph::new( vec!["5", "7", "8", "10"], vec![("5", "7"), ("7", "8")] ); assert_length!(gph, 4); - }); - assert!(result.is_ok()); - let result = std::panic::catch_unwind(|| { let gph = graph::Graph::::new(Vec::new(), Vec::new()); assert_length!(gph, 0); - }); - assert!(result.is_ok()); + }).is_ok()); } #[rstest] diff --git a/code/rust/tests/test_rules/mod.rs b/code/rust/tests/test_rules/mod.rs index 29e2dfe..9c5530b 100644 --- a/code/rust/tests/test_rules/mod.rs +++ b/code/rust/tests/test_rules/mod.rs @@ -1 +1,2 @@ pub mod test_assert; +pub mod test_convert; diff --git a/code/rust/tests/test_rules/test_convert.rs b/code/rust/tests/test_rules/test_convert.rs new file mode 100644 index 0000000..d6a571f --- /dev/null +++ b/code/rust/tests/test_rules/test_convert.rs @@ -0,0 +1,42 @@ +// ---------------------------------------------------------------- +// IMPORTS +// ---------------------------------------------------------------- + +use rstest::rstest; +use std::panic::catch_unwind; + +use ads2::assert_length; +use ads2::vec_of_strings; + +// ---------------------------------------------------------------- +// Fixtures +// ---------------------------------------------------------------- + +// ---------------------------------------------------------------- +// Test length +// ---------------------------------------------------------------- + +#[test] +fn test_vec_of_strings_creation() { + assert!(catch_unwind(|| { + let _: Vec = vec_of_strings![]; + let _: Vec = vec_of_strings!["apple", "bear", "coal"]; + let _: Vec = vec_of_strings!["apple", "bear", "coal",]; + }).is_ok()); +} + +#[rstest] +#[case(vec_of_strings![], Vec::<&str>::new(), 0)] +#[case(vec_of_strings!["apple"], vec!["apple"], 1)] +#[case(vec_of_strings!["apple", "bear"], vec!["apple", "bear"], 2)] +#[case(vec_of_strings!["apple", -75.2], vec!["apple", "-75.2"], 2)] +#[case(vec_of_strings!["apple", "bear", "coal"], vec!["apple", "bear", "coal"], 3)] +#[case(vec_of_strings!["apple", "bear", "coal",], vec!["apple", "bear", "coal"], 3)] +fn test_vec_of_strings_cases( + #[case] list1: Vec, + #[case] list2: Vec<&str>, + #[case] n: usize) +{ + assert_length!(list1, n); + assert_eq!(list1, list2); +}