package kata.wordwrap.a20160813; import static org.junit.Assert.fail; import static org.junit.Assert.assertEquals; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * Word Wrap Kata. * Copyright (c) 2016, Peter Kofler, licensed under BSD License. */ @RunWith(Parameterized.class) public class WordWrapTest { @Parameters(name = "{0}") public static Collection data() { return Arrays.asList(new Object[][] { // { WordWrapReduce.class }, // }); } private final String name; private final Method wrapMethod; public WordWrapTest(Class testee) throws NoSuchMethodException { name = testee.getSimpleName(); wrapMethod = testee.getMethod("wrap", String.class, int.class); } @Test public void testWrapEmptyString() { assertWrap("", "", 1); } @Test public void testWrapWordShorterThanLimit() { assertWrap("word", "word", 10); } @Test public void testWrapWordOnce() { assertWrap("wo\nrd", "word", 2); } @Test public void testWrapWordTwice() { assertWrap("wor\ndwo\nrd", "wordword", 3); } @Test public void testWrapTwoWords() { assertWrap("word\nword", "word word", 5); } @Test public void testWrapTwoWordsThreeTimes() { assertWrap("wor\nd\nwor\nd", "word word", 3); } @Test public void testWrapTwoWordsInMiddle() { assertWrap("word\nword", "word word", 4); } @Test public void testWrapThreeWords() { assertWrap("word\nword\nword", "word word word", 6); } @Test public void testWrapThreeWordsAtSecondBreak() { assertWrap("word word\nword", "word word word", 11); } // additional tests by Chuck Krutsinger @Test public void shouldNotWrapIfMaxLengthIsGreaterLine() { assertWrap("word word", "word word", 80); } // more test cases for wrapping on every blank @Test public void testWrapBlank() { assertWrap(" ", " ", 80); } @Test public void testWrapBlanksAtBoundaries() { assertWrap(" word", " word", 80); assertWrap("word ", "word ", 80); assertWrap(" word ", " word ", 80); } private void assertWrap(String expected, String given, int withLen) { try { assertEquals(name + "wrap(" + withLen + ")", expected, wrapMethod.invoke(null, given, withLen)); } catch (IllegalAccessException e) { fail(e.toString()); } catch (InvocationTargetException e) { fail(e.toString()); } } }