|
| 1 | +package com.baeldung.algorithms.printtriangles; |
| 2 | + |
| 3 | +import junitparams.JUnitParamsRunner; |
| 4 | +import junitparams.Parameters; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | + |
| 10 | +@RunWith(JUnitParamsRunner.class) |
| 11 | +public class PrintTriangleExamplesUnitTest { |
| 12 | + |
| 13 | + private static Object[][] rightAngledTriangles() { |
| 14 | + String expected0 = ""; |
| 15 | + |
| 16 | + String expected2 = "*" + System.lineSeparator() |
| 17 | + + "**" + System.lineSeparator(); |
| 18 | + |
| 19 | + String expected5 = "*" + System.lineSeparator() |
| 20 | + + "**" + System.lineSeparator() |
| 21 | + + "***" + System.lineSeparator() |
| 22 | + + "****" + System.lineSeparator() |
| 23 | + + "*****" + System.lineSeparator(); |
| 24 | + |
| 25 | + String expected7 = "*" + System.lineSeparator() |
| 26 | + + "**" + System.lineSeparator() |
| 27 | + + "***" + System.lineSeparator() |
| 28 | + + "****" + System.lineSeparator() |
| 29 | + + "*****" + System.lineSeparator() |
| 30 | + + "******" + System.lineSeparator() |
| 31 | + + "*******" + System.lineSeparator(); |
| 32 | + |
| 33 | + return new Object[][] { |
| 34 | + { 0, expected0 }, |
| 35 | + { 2, expected2 }, |
| 36 | + { 5, expected5 }, |
| 37 | + { 7, expected7 } |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + @Parameters(method = "rightAngledTriangles") |
| 43 | + public void whenPrintARightAngledTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) { |
| 44 | + String actual = PrintTriangleExamples.printARightAngledTriangle(nrOfRows); |
| 45 | + |
| 46 | + assertEquals(expected, actual); |
| 47 | + } |
| 48 | + |
| 49 | + private static Object[][] isoscelesTriangles() { |
| 50 | + String expected0 = ""; |
| 51 | + |
| 52 | + String expected2 = " *" + System.lineSeparator() |
| 53 | + + "***" + System.lineSeparator(); |
| 54 | + |
| 55 | + String expected5 = " *" + System.lineSeparator() |
| 56 | + + " ***" + System.lineSeparator() |
| 57 | + + " *****" + System.lineSeparator() |
| 58 | + + " *******" + System.lineSeparator() |
| 59 | + + "*********" + System.lineSeparator(); |
| 60 | + |
| 61 | + String expected7 = " *" + System.lineSeparator() |
| 62 | + + " ***" + System.lineSeparator() |
| 63 | + + " *****" + System.lineSeparator() |
| 64 | + + " *******" + System.lineSeparator() |
| 65 | + + " *********" + System.lineSeparator() |
| 66 | + + " ***********" + System.lineSeparator() |
| 67 | + + "*************" + System.lineSeparator(); |
| 68 | + |
| 69 | + return new Object[][] { |
| 70 | + { 0, expected0 }, |
| 71 | + { 2, expected2 }, |
| 72 | + { 5, expected5 }, |
| 73 | + { 7, expected7 } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + @Parameters(method = "isoscelesTriangles") |
| 79 | + public void whenPrintAnIsoscelesTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) { |
| 80 | + String actual = PrintTriangleExamples.printAnIsoscelesTriangle(nrOfRows); |
| 81 | + |
| 82 | + assertEquals(expected, actual); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @Parameters(method = "isoscelesTriangles") |
| 87 | + public void whenPrintAnIsoscelesTriangleUsingSubstringIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) { |
| 88 | + String actual = PrintTriangleExamples.printAnIsoscelesTriangleUsingSubstring(nrOfRows); |
| 89 | + |
| 90 | + assertEquals(expected, actual); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments