/*
* 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 Daniel Doubrovkine
*/
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("[0m[K"));
assertEquals("hello world", colorize("[0mhello world"));
}
@Test
public void testBold() throws IOException {
assertEquals("hello world", colorize("[1mhello world"));
}
@Test
public void testGreen() throws IOException {
assertEquals("hello world",
colorize("[32mhello world"));
}
@Test
public void testGreenOnWhite() throws IOException {
assertEquals("hello world",
colorize("[47;32mhello world"));
}
@Test
public void testEscapeHtml() throws IOException {
assertEquals(""", colorize("\""));
assertEquals("&", colorize("&"));
assertEquals("<", colorize("<"));
assertEquals(">", colorize(">"));
assertEquals(""&<>", colorize("\"&<>"));
}
@Test
public void testResetOnOpen() throws IOException {
assertEquals("red",
colorize("[0;31;49mred[0m"));
}
@Test
public void testUTF8Character() throws IOException {
assertEquals("\u3053\u3093\u306b\u3061\u306f",
colorize("[1m\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);
}
}