Skip to content

refactor: Enhance docs, add more tests in PhoneticAlphabetConverter #5943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,25 @@ private 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

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));
}
}