|
| 1 | +package com.baeldung.algorithms.moneywords; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import com.baeldung.algorithms.numberwordconverter.NumberWordConverter; |
| 8 | + |
| 9 | +public class NumberWordConverterTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + public void whenMoneyNegative_thenReturnInvalidInput() { |
| 13 | + assertEquals(NumberWordConverter.INVALID_INPUT_GIVEN, NumberWordConverter.getMoneyIntoWords(-13)); |
| 14 | + } |
| 15 | + |
| 16 | + @Test |
| 17 | + public void whenZeroDollarsGiven_thenReturnEmptyString() { |
| 18 | + assertEquals("", NumberWordConverter.getMoneyIntoWords(0)); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void whenOnlyDollarsGiven_thenReturnWords() { |
| 23 | + assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1)); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void whenOnlyCentsGiven_thenReturnWords() { |
| 28 | + assertEquals("sixty cents", NumberWordConverter.getMoneyIntoWords(0.6)); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void whenAlmostAMillioDollarsGiven_thenReturnWords() { |
| 33 | + String expectedResult = "nine hundred ninety nine thousand nine hundred ninety nine dollars"; |
| 34 | + assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(999_999)); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void whenThirtyMillionDollarsGiven_thenReturnWords() { |
| 39 | + String expectedResult = "thirty three million three hundred forty eight thousand nine hundred seventy eight dollars"; |
| 40 | + assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(33_348_978)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void whenTwoBillionDollarsGiven_thenReturnWords() { |
| 45 | + String expectedResult = "two billion one hundred thirty three million two hundred forty seven thousand eight hundred ten dollars"; |
| 46 | + assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(2_133_247_810)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenGivenDollarsAndCents_thenReturnWords() { |
| 51 | + String expectedResult = "nine hundred twenty four dollars and sixty cents"; |
| 52 | + assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(924.6)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void whenOneDollarAndNoCents_thenReturnDollarSingular() { |
| 57 | + assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void whenNoDollarsAndOneCent_thenReturnCentSingular() { |
| 62 | + assertEquals("one cent", NumberWordConverter.getMoneyIntoWords(0.01)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void whenNoDollarsAndTwoCents_thenReturnCentsPlural() { |
| 67 | + assertEquals("two cents", NumberWordConverter.getMoneyIntoWords(0.02)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenNoDollarsAndNinetyNineCents_thenReturnWords() { |
| 72 | + assertEquals("ninety nine cents", NumberWordConverter.getMoneyIntoWords(0.99)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void whenNoDollarsAndNineFiveNineCents_thenCorrectRounding() { |
| 77 | + assertEquals("ninety six cents", NumberWordConverter.getMoneyIntoWords(0.959)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void whenGivenDollarsAndCents_thenReturnWordsVersionTwo() { |
| 82 | + assertEquals("three hundred ten £ 00/100", NumberWordConverter.getMoneyIntoWords("310")); |
| 83 | + } |
| 84 | +} |
0 commit comments