/* * $Id$ * * Copyright (C) 2003-2015 JNode.org * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; If not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package org.jnode.test.shell.help; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; import org.jnode.shell.help.def.DefaultHelpFactory; import org.junit.Assert; import org.junit.Test; /** * Unit tests for the DefaultHelp implementation of the Help * * @author crawley@jnode.org */ public class DefaultHelpTest { static class MyDefaultHelp extends DefaultHelpFactory { static class MyCell extends Cell { MyCell(int margin, int width) { super(margin, width); } public String fit(String text) { return super.fit(text); } public String stamp(String text) { return super.stamp(text); } } public void format(PrintWriter out, MyCell[] cells, String[] texts) { super.format(out, cells, texts); } } @Test public void testConstructor() { new DefaultHelpFactory(); } @Test public void testCellFit() { String msg = "The quick brown fox jumped over the lazy dog."; for (int i = 1; i < msg.length() + 5; i++) { String m = new MyDefaultHelp.MyCell(5, i).fit(msg); Assert.assertTrue("fit length", m.length() <= i); Assert.assertTrue("text starts with fit ", msg.startsWith(m)); } Assert.assertEquals(" Hello ", new MyDefaultHelp.MyCell(5, 10).fit(" Hello ")); } @Test public void testCellStamp() { String msg = "Hello Mum"; String m = new MyDefaultHelp.MyCell(5, 10).stamp(msg); Assert.assertEquals(m, " Hello Mum "); } @Test public void testFormat() { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(bos); new MyDefaultHelp().format(pw, new MyDefaultHelp.MyCell[] {new MyDefaultHelp.MyCell(3, 5), new MyDefaultHelp.MyCell(3, 20)}, new String[] { "12345678901234567890", "The quick brown fox jumped over the lazy dog." + "The quick brown fox jumped over the lazy dog." + "The quick brown fox jumped over the lazy dog."}); pw.flush(); Assert.assertEquals(" 12345 The quick brown fox \n" + " 67890 jumped over the lazy\n" + " 12345 dog.The quick brown \n" + " 67890 fox jumped over \n" + " the lazy dog.The \n" + " quick brown fox \n" + " jumped over the \n" + " lazy dog. \n", bos.toString()); } }