From c805437c0c5e057f3ad60c397fe6550eec7be453 Mon Sep 17 00:00:00 2001 From: Ricardo Ramos <36955909+ricardo-ramos-moura@users.noreply.github.com> Date: Wed, 12 Oct 2022 15:42:24 -0300 Subject: [PATCH] Add tests for CountChar (#3334) --- .../com/thealgorithms/others/CountChar.java | 15 ++------------- .../com/thealgorithms/others/CountCharTest.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/CountCharTest.java diff --git a/src/main/java/com/thealgorithms/others/CountChar.java b/src/main/java/com/thealgorithms/others/CountChar.java index 11876e40..5a78c0c1 100644 --- a/src/main/java/com/thealgorithms/others/CountChar.java +++ b/src/main/java/com/thealgorithms/others/CountChar.java @@ -1,26 +1,15 @@ package com.thealgorithms.others; -import java.util.Scanner; - public class CountChar { - public static void main(String[] args) { - Scanner input = new Scanner(System.in); - System.out.print("Enter your text: "); - String str = input.nextLine(); - input.close(); - System.out.println( - "There are " + CountCharacters(str) + " characters." - ); - } - /** * Count non space character in string * * @param str String to count the characters * @return number of character in the specified string */ - private static int CountCharacters(String str) { + + public static int CountCharacters(String str) { return str.replaceAll("\\s", "").length(); } } diff --git a/src/test/java/com/thealgorithms/others/CountCharTest.java b/src/test/java/com/thealgorithms/others/CountCharTest.java new file mode 100644 index 00000000..9e9972c6 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/CountCharTest.java @@ -0,0 +1,17 @@ +package com.thealgorithms.others; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CountCharTest { + + @Test + void testCountCharacters(){ + String input = "12345"; + int expectedValue = 5; + + assertEquals(expectedValue, CountChar.CountCharacters(input)); + } + +} \ No newline at end of file