From ff215ad928a402c7d9646f46dd7c066836bf9f94 Mon Sep 17 00:00:00 2001 From: Abdelhak Zaaim Date: Mon, 11 Nov 2024 15:29:02 +0100 Subject: [PATCH 1/2] Add unit tests for Prompter class methods --- .../archetype/engine/v1/PrompterTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 archetype/engine-v1/src/test/java/io/helidon/build/archetype/engine/v1/PrompterTest.java diff --git a/archetype/engine-v1/src/test/java/io/helidon/build/archetype/engine/v1/PrompterTest.java b/archetype/engine-v1/src/test/java/io/helidon/build/archetype/engine/v1/PrompterTest.java new file mode 100644 index 000000000..be67d9fda --- /dev/null +++ b/archetype/engine-v1/src/test/java/io/helidon/build/archetype/engine/v1/PrompterTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.build.archetype.engine.v1; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.util.List; +import java.util.Optional; +import java.util.function.Predicate; + +import static org.junit.jupiter.api.Assertions.*; + +class PrompterTest { + + @Test + void testPromptWithDefaultAnswer() { + String input = "\n"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + String result = Prompter.prompt("Enter your name", "defaultName"); + assertEquals("defaultName", result); + } + + @Test + void testPromptWithValidator() { + String input = "validInput\n"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + Predicate validator = s -> s.equals("validInput"); + String result = Prompter.prompt("Enter valid input", null, validator); + assertEquals("validInput", result); + } + + @Test + void testPromptWithOptionalDefault() { + String input = "\n"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + String result = Prompter.prompt("Enter your name", Optional.of("defaultName")); + assertEquals("defaultName", result); + } + + @Test + void testPromptSelection() { + String input = "2\n"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + List options = List.of("Option 1", "Option 2", "Option 3"); + int result = Prompter.prompt("Choose an option", options, 0); + assertEquals(1, result); + } + + @Test + void testPromptYesNo() { + String input = "y\n"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + boolean result = Prompter.promptYesNo("Do you agree?", false); + assertTrue(result); + } + +} \ No newline at end of file From ae2f8e30e90218e9efbe8b1961f6937ae3c141c2 Mon Sep 17 00:00:00 2001 From: Abdelhak Zaaim Date: Wed, 13 Nov 2024 14:22:48 +0100 Subject: [PATCH 2/2] update to use assertThat function --- .../build/archetype/engine/v1/PrompterTest.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/archetype/engine-v1/src/test/java/io/helidon/build/archetype/engine/v1/PrompterTest.java b/archetype/engine-v1/src/test/java/io/helidon/build/archetype/engine/v1/PrompterTest.java index be67d9fda..c1110442d 100644 --- a/archetype/engine-v1/src/test/java/io/helidon/build/archetype/engine/v1/PrompterTest.java +++ b/archetype/engine-v1/src/test/java/io/helidon/build/archetype/engine/v1/PrompterTest.java @@ -22,7 +22,8 @@ import java.util.Optional; import java.util.function.Predicate; -import static org.junit.jupiter.api.Assertions.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.is; class PrompterTest { @@ -31,7 +32,7 @@ void testPromptWithDefaultAnswer() { String input = "\n"; System.setIn(new ByteArrayInputStream(input.getBytes())); String result = Prompter.prompt("Enter your name", "defaultName"); - assertEquals("defaultName", result); + assertThat(result, is("defaultName")); } @Test @@ -40,7 +41,7 @@ void testPromptWithValidator() { System.setIn(new ByteArrayInputStream(input.getBytes())); Predicate validator = s -> s.equals("validInput"); String result = Prompter.prompt("Enter valid input", null, validator); - assertEquals("validInput", result); + assertThat(result, is("validInput")); } @Test @@ -48,7 +49,7 @@ void testPromptWithOptionalDefault() { String input = "\n"; System.setIn(new ByteArrayInputStream(input.getBytes())); String result = Prompter.prompt("Enter your name", Optional.of("defaultName")); - assertEquals("defaultName", result); + assertThat(result, is("defaultName")); } @Test @@ -57,7 +58,7 @@ void testPromptSelection() { System.setIn(new ByteArrayInputStream(input.getBytes())); List options = List.of("Option 1", "Option 2", "Option 3"); int result = Prompter.prompt("Choose an option", options, 0); - assertEquals(1, result); + assertThat(result, is(1)); } @Test @@ -65,7 +66,7 @@ void testPromptYesNo() { String input = "y\n"; System.setIn(new ByteArrayInputStream(input.getBytes())); boolean result = Prompter.promptYesNo("Do you agree?", false); - assertTrue(result); + assertThat(result, is(true)); } } \ No newline at end of file