Console/test/com/dorkbox/console/HtmlAnsiOutputStreamTest.java

93 lines
2.6 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (C) 2009 the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dorkbox.console;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import org.junit.Test;
import dorkbox.console.output.HtmlAnsiOutputStream;
/**
* @author <a href="http://code.dblock.org">Daniel Doubrovkine</a>
*/
public class HtmlAnsiOutputStreamTest {
private static final Charset charset = Charset.forName("UTF-8");
@Test
public void testNoMarkup() throws IOException {
assertEquals("line", colorize("line"));
}
@Test
public void testClear() throws IOException {
assertEquals("", colorize(""));
assertEquals("hello world", colorize("hello world"));
}
@Test
public void testBold() throws IOException {
assertEquals("<b>hello world</b>", colorize("hello world"));
}
@Test
public void testGreen() throws IOException {
assertEquals("<span style=\"color: green;\">hello world</span>",
colorize("hello world"));
}
@Test
public void testGreenOnWhite() throws IOException {
assertEquals("<span style=\"background-color: white;\"><span style=\"color: green;\">hello world</span></span>",
colorize("hello world"));
}
@Test
public void testEscapeHtml() throws IOException {
assertEquals("&quot;", colorize("\""));
assertEquals("&amp;", colorize("&"));
assertEquals("&lt;", colorize("<"));
assertEquals("&gt;", colorize(">"));
assertEquals("&quot;&amp;&lt;&gt;", colorize("\"&<>"));
}
@Test
public void testResetOnOpen() throws IOException {
assertEquals("<span style=\"color: red;\">red</span>",
colorize("red"));
}
@Test
public void testUTF8Character() throws IOException {
assertEquals("<b>\u3053\u3093\u306b\u3061\u306f</b>",
colorize("\u3053\u3093\u306b\u3061\u306f"));
}
private String colorize(String text) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
HtmlAnsiOutputStream hos = new HtmlAnsiOutputStream(os);
hos.write(text.getBytes(charset));
hos.close();
return new String(os.toByteArray(), charset);
}
}