From d77f53bf3a225c9cae1005acf8900f27a9e00fd3 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Tue, 21 May 2024 13:21:00 +1200 Subject: [PATCH] Fix unit tests --- .github/workflows/release.yml | 3 ++- .github/workflows/test.yml | 4 ++++ toolproof/src/definitions/mod.rs | 26 ++++++++++++++++---------- toolproof/src/parser.rs | 14 +++++++------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b888a29..da8cc07 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -358,7 +358,8 @@ jobs: - name: Test CLI if: matrix.run_tests == true working-directory: ./toolproof - run: cargo run -- --placeholders toolproof_path="../target/${{ matrix.target }}/release/toolproof" + # toolproof tests itself when run + run: cargo run -- --placeholders toolproof_path="$(pwd)/../target/debug/toolproof" # TODO: build and package pagebrowse - name: Create Release Assets diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6433ffe..cb5bba1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -56,6 +56,10 @@ jobs: run: cargo build - name: Test Lib + working-directory: ./toolproof + run: cargo test + + - name: Test CLI working-directory: ./toolproof # toolproof tests itself when run run: cargo run -- --placeholders toolproof_path="$(pwd)/../target/debug/toolproof" diff --git a/toolproof/src/definitions/mod.rs b/toolproof/src/definitions/mod.rs index 208b80d..943508d 100644 --- a/toolproof/src/definitions/mod.rs +++ b/toolproof/src/definitions/mod.rs @@ -110,7 +110,7 @@ mod test { #[async_trait] impl ToolproofInstruction for TestInstruction { fn segments(&self) -> &'static str { - "I am an instruction asking for {argument}" + "__test__ I am an instruction asking for {argument}" } async fn run( @@ -122,8 +122,9 @@ mod test { } } - let users_instruction = parse_segments("I am an instruction asking for \"this argument\"") - .expect("Valid instruction"); + let users_instruction = + parse_segments("__test__ I am an instruction asking for \"this argument\"") + .expect("Valid instruction"); let all_instructions = register_instructions(); let matching_instruction = all_instructions @@ -132,7 +133,7 @@ mod test { assert_eq!( matching_instruction.segments(), - "I am an instruction asking for {argument}" + "__test__ I am an instruction asking for {argument}" ); } @@ -147,7 +148,7 @@ mod test { #[async_trait] impl ToolproofRetriever for TestRetriever { fn segments(&self) -> &'static str { - "the file {filename}" + "__test__ the file {filename}" } async fn run( @@ -159,14 +160,18 @@ mod test { } } - let users_segments = parse_segments("the file \"index.html\"").expect("Valid instruction"); + let users_segments = + parse_segments("__test__ the file \"index.html\"").expect("Valid instruction"); let all_segments = register_retrievers(); let matching_retriever = all_segments .get(&users_segments) .expect("should be able to retrieve segments"); - assert_eq!(matching_retriever.segments(), "the file {filename}"); + assert_eq!( + matching_retriever.segments(), + "__test__ the file {filename}" + ); } #[test] @@ -180,7 +185,7 @@ mod test { #[async_trait] impl ToolproofAssertion for TestAssertion { fn segments(&self) -> &'static str { - "be exactly {value}" + "__test__ be exactly {value}" } async fn run( @@ -193,13 +198,14 @@ mod test { } } - let users_segments = parse_segments("be exactly {my_json}").expect("Valid instruction"); + let users_segments = + parse_segments("__test__ be exactly {my_json}").expect("Valid instruction"); let all_segments = register_assertions(); let matching_assertion = all_segments .get(&users_segments) .expect("should be able to retrieve segments"); - assert_eq!(matching_assertion.segments(), "be exactly {value}"); + assert_eq!(matching_assertion.segments(), "__test__ be exactly {value}"); } } diff --git a/toolproof/src/parser.rs b/toolproof/src/parser.rs index 300b6fe..c1e10af 100644 --- a/toolproof/src/parser.rs +++ b/toolproof/src/parser.rs @@ -199,7 +199,7 @@ pub fn parse_segments(s: &str) -> Result match mode { InstMode::None(start) => { if start < s.len() { - segments.push(Literal(s[start..].to_string())); + segments.push(Literal(s[start..].to_lowercase())); } } InstMode::InQuote(_, q) => return Err(ToolproofInputError::UnclosedValue { expected: q }), @@ -226,14 +226,14 @@ mod test { // look inside Value or Variable segments. assert_eq!( segments.segments, - vec![Literal("I run my program".to_string())] + vec![Literal("i run my program".to_string())] ); let segments = parse_segments("I have a \"public/cat/'index'.html\" file with the body '

Happy post about \"cats

'").expect("Valid segments"); assert_eq!( segments.segments, vec![ - Literal("I have a ".to_string()), + Literal("i have a ".to_string()), Value(st("public/cat/'index'.html")), Literal(" file with the body ".to_string()), Value(st("

Happy post about \"cats

")) @@ -245,9 +245,9 @@ mod test { assert_eq!( segments.segments, vec![ - Literal("In my browser, ".to_string()), + Literal("in my browser, ".to_string()), Value(st("")), - Literal("I eval ".to_string()), + Literal("i eval ".to_string()), Variable("j\"s".to_string()), Literal(" and ".to_string()), Value(st("x")), @@ -266,7 +266,7 @@ mod test { ToolproofTestStep::Instruction { step: ToolproofSegments { segments: vec![ - Literal("I have a ".to_string()), + Literal("i have a ".to_string()), Variable("js".to_string()), Literal(" file".to_string()) ] @@ -289,7 +289,7 @@ mod test { ToolproofTestStep::Assertion { retrieval: ToolproofSegments { segments: vec![ - Literal("The file ".to_string()), + Literal("the file ".to_string()), Variable("name".to_string()) ] },