Repository: TheAlgorithms/Java
Pull request #5943 (2024-10-23 06:18:53)

refactor: Enhance docs, add more tests in `PhoneticAlphabetConverter`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5943?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 67.17%. Comparing base [(`2f9f75a`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/2f9f75a3a70a11f3321396fd4a65c7331b50574c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`d44e37f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/d44e37f86deeb653f549fa174dbe6b14e606f2a6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5943      +/-   ##
============================================
- Coverage     67.18%   67.17%   -0.01%     
  Complexity     4561     4561              
============================================
  Files           620      620              
  Lines         17142    17144       +2     
  Branches       3310     3311       +1     
============================================
  Hits          11517    11517              
- Misses         5167     5168       +1     
- Partials        458      459       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5943?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java b/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java
index bcea1862..730ce221 100644
--- a/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java
+++ b/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java
@@ -58,9 +58,25 @@ public final class PhoneticAlphabetConverter {
         PHONETIC_MAP.put('9', "Nine");
     }
 
+    /**
+     * Converts text to the NATO phonetic alphabet.
+     * Steps:
+     * 1. Convert the text to uppercase.
+     * 2. Iterate over each character in the text.
+     * 3. Get the phonetic equivalent of the character from the map.
+     * 4. Append the phonetic equivalent to the result.
+     * 5. Append a space to separate the phonetic equivalents.
+     * 6. Return the result.
+     *
+     * @param text the text to convert
+     * @return the NATO phonetic alphabet
+     */
     public static String textToPhonetic(String text) {
         StringBuilder phonetic = new StringBuilder();
         for (char c : text.toUpperCase().toCharArray()) {
+            if (Character.isWhitespace(c)) {
+                continue;
+            }
             phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
         }
         return phonetic.toString().trim();


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java b/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java
index 07847302..360af7fa 100644
--- a/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java
+++ b/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java
@@ -2,20 +2,26 @@ package com.thealgorithms.conversions;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 public class PhoneticAlphabetConverterTest {
 
-    @Test
-    public void testTextToPhonetic() {
-        assertEquals("Alpha Bravo", PhoneticAlphabetConverter.textToPhonetic("AB"));
-        assertEquals("Alpha Bravo Charlie", PhoneticAlphabetConverter.textToPhonetic("ABC"));
-        assertEquals("Alpha One Bravo Two Charlie Three", PhoneticAlphabetConverter.textToPhonetic("A1B2C3"));
-        assertEquals("Hotel Echo Lima Lima Oscar", PhoneticAlphabetConverter.textToPhonetic("Hello"));
-        assertEquals("One Two Three", PhoneticAlphabetConverter.textToPhonetic("123"));
-        assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine",
-            PhoneticAlphabetConverter.textToPhonetic("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
-        assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine",
-            PhoneticAlphabetConverter.textToPhonetic("abcdefghijklmnopqrstuvwxyz0123456789"));
+    @ParameterizedTest
+    @CsvSource({
+        "'AB', 'Alpha Bravo'", "'ABC', 'Alpha Bravo Charlie'", "'A1B2C3', 'Alpha One Bravo Two Charlie Three'", "'Hello', 'Hotel Echo Lima Lima Oscar'", "'123', 'One Two Three'",
+        "'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 'Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine'",
+        "'abcdefghijklmnopqrstuvwxyz0123456789', 'Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine'",
+        "'', ''", // Empty string case
+        "'A B C', 'Alpha Bravo Charlie'", // String with spaces
+        "'A@B#C', 'Alpha @ Bravo # Charlie'", // Special characters
+        "'A B C 123', 'Alpha Bravo Charlie One Two Three'", // Mixed letters, digits, and spaces
+        "'a b c', 'Alpha Bravo Charlie'", // Lowercase letters with spaces
+        "'123!@#', 'One Two Three ! @ #'", // Numbers with special characters
+        "'HELLO WORLD', 'Hotel Echo Lima Lima Oscar Whiskey Oscar Romeo Lima Delta'" // Words with space
+    })
+    public void
+    testTextToPhonetic(String input, String expectedOutput) {
+        assertEquals(expectedOutput, PhoneticAlphabetConverter.textToPhonetic(input));
     }
 }