package kata.wordwrap.a20110716; 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) 2011, Peter Kofler, licensed under BSD License. */ @RunWith(Parameterized.class) public class WordWrapTest { @Parameters public static Collection data() { return Arrays.asList(new Object[][] { { WordWrapRecursive.class }, // { WordWrapRegex.class }, // { WordWrapTailRecursive.class }, // { WordWrapLoop.class }, // { WordWrapLoopOpt.class }, // { WordWrapLoopBuffer.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); } 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()); } } @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); } }