Skip to content

Commit

Permalink
Tests and fixes
Browse files Browse the repository at this point in the history
* renamed test executable due to name reservation conflict in CMake
* other fixes in CmakeLists.txt. Now again builds with openSUSE 15.1
* more tests implemented
* added missing support for basic bright colors (90-97 and 100-107)
* found few bugs: see #3, #5
  • Loading branch information
pozemka committed Jan 8, 2020
1 parent 961058f commit 327c199
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 23 deletions.
18 changes: 10 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ add_library(ANSIEsc2HTML_static STATIC
"${LIBRARY_SRCS}"
)

set_target_properties(ANSIEsc2HTML ANSIEsc2HTML_static PROPERTIES
add_executable(tests
tests/catch.hpp
tests/tests.cpp
)

target_link_libraries(tests ANSIEsc2HTML_static)

add_dependencies(tests ANSIEsc2HTML_static)

set_target_properties(ANSIEsc2HTML ANSIEsc2HTML_static tests PROPERTIES
PUBLIC_HEADER src/ansi_esc2html.h
# VERSION ${PROJECT_VERSION}
# SOVERSION 0
CXX_STANDARD 17
CXX_EXTENSIONS OFF
)

add_executable(test
tests/catch.hpp
tests/tests.cpp
src/ansi_esc2html.h
"${LIBRARY_SRCS}"
)
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ Library to convert limited part of SGR (Select Graphic Rendition) sequence of AN
* 3 — Italic, 23 — Not italic
* 4 — Underline, 24 — Underline off
* 9 — Crossed-out, 29 — Not crossed out
* 30–39 — foreground color
* 40–49 — background color
* 30–39, 90–97 — foreground color
* 40–49, 100–107 — background color

## Limitations:
See more info on [[wiki|Limitations]]

* All other SGR parameters are unsupported and produce no HTML code.
* Can only disable SGR parameters in reverse order they where enabled. Terminal emulators can disable them in any order.

Expand All @@ -37,7 +38,9 @@ This will produce, release versions of static and dynamic libraries and test exe

mkdir build
cd build/
`cmake -DCMAKE_BUILD_TYPE=Release ..` or `cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..` for Windows
cmake -DCMAKE_BUILD_TYPE=Release ..
# or for Windows
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
make


13 changes: 10 additions & 3 deletions src/ansi_esc2html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,25 @@ std::string ANSI_SGR2HTML::impl::processSGR(SGRParts& sgr_parts/*non const!*/)
break;

default: // SGR code ranges
if (30 <= sgr_code && 37 >= sgr_code) { // foreground color from table
if (
(30 <= sgr_code && 37 >= sgr_code) ||
(90 <= sgr_code && 97 >= sgr_code)
) { // foreground color from table
// For now using <font color> instead of <span style>. It is little shorter and should not brake in most of cases.
out.append(R"(<font color=")"); // Not very beautilful string construction. Can use {fmt} or wait for С++20 with eel.is/c++draft/format.
out.append(decodeColorBasic(sgr_code));
out.append(R"(">)");
stack_fg_color_.push("</font>");
} else if (40 <= sgr_code && 47 >= sgr_code) { // background color from table
} else if (
(40 <= sgr_code && 47 >= sgr_code) ||
(100 <= sgr_code && 107 >= sgr_code)
) { // background color from table
out.append(R"(<span style="background-color:)");
out.append(decodeColorBasic(sgr_code));
out.append(R"(">)");
stack_bg_color_.push("</span>");
} else {
std::cerr << "Warning: unsupported SGR: " << sgr_code << std::endl;
std::cerr << "Warning: unsupported SGR: " << static_cast<unsigned int>(sgr_code) << std::endl;
}
}

Expand Down Expand Up @@ -329,6 +335,7 @@ void ANSI_SGR2HTML::impl::resetAll(std::string& out)
const char* ANSI_SGR2HTML::impl::decodeColor256(unsigned char color_code)
{
static constexpr std::array<const char*, 256> colors_256 = {
//standard colros based on Ubuntu color theme. Change to X-term colors?
"#000000", // Black
"#de382b", // Red
"#39b54a", // Green
Expand Down
119 changes: 110 additions & 9 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,127 @@

static std::string body_start=R"(<body style="background-color:#111111;font-family:'Consolas','Droid Sans Mono',monospace; color:#eeeeee; white-space:pre">)";
static std::string body_end=R"(</body>)";
TEST_CASE( "valid input for simpleParse", "[simple]" ) {
TEST_CASE( "empty strings", "[empty]" ) {
ANSI_SGR2HTML a2h;
SECTION( "empty string" ) {
std::string res = a2h.simpleParse("");
REQUIRE(body_start+body_end == res);
CHECK(body_start+body_end == res);
}
SECTION( "null string" ) {
std::string res = a2h.simpleParse(std::string());
REQUIRE(body_start+body_end == res);
CHECK(body_start+body_end == res);
}
SECTION( "48 5 141 blue-pink backgorund" ) {
std::string res = a2h.simpleParse(std::string("\x1b[48;5;141m background color \x1b[49m"));
REQUIRE(body_start+R"(<span style="background-color:#af87ff"> background color </span>)"+body_end == res);
SECTION( "unsupported params" ) {
// ideogram attributes are not supported
std::string res = a2h.simpleParse("\x1b[62m\x1b[65m");
CHECK(body_start+body_end == res);
}
}

TEST_CASE( "bad input for simpleParse", "[bad_simple]") {
TEST_CASE( "colors", "[colors]" ) {
ANSI_SGR2HTML a2h;
SECTION( "basic-color: 43 yellow backgorund" ) {
std::string res = a2h.simpleParse("\x1b[43m yellow background \x1b[49m");
CHECK(body_start+R"(<span style="background-color:#ffc706"> yellow background </span>)"+body_end == res);
}
SECTION( "basic-color: 103 bright-yellow backgorund" ) {
std::string res = a2h.simpleParse("\x1b[103m bright-yellow background \x1b[49m");
CHECK(body_start+R"(<span style="background-color:#ffff00"> bright-yellow background </span>)"+body_end == res);
}
SECTION( "256-color 48 5 141 blue-pink backgorund" ) {
std::string res = a2h.simpleParse("\x1b[48;5;141m background color \x1b[49m");
CHECK(body_start+R"(<span style="background-color:#af87ff"> background color </span>)"+body_end == res);
}
SECTION( "24-bit-color: 48 245 222 179 wheat backgorund" ) {
std::string res = a2h.simpleParse("\x1b[48;2;245;222;179m wheat background \x1b[49m");
CHECK(body_start+R"(<span style="background-color:#f5deb3"> wheat background </span>)"+body_end == res);
}

SECTION( "basic-color: 34 blue foreground" ) {
std::string res = a2h.simpleParse("\x1b[34m blue foreground \x1b[39m");
CHECK(body_start+R"(<font color="#006fb8"> blue foreground </font>)"+body_end == res);
}
SECTION( "basic-color: 94 bright-blue foreground" ) {
std::string res = a2h.simpleParse("\x1b[94m bright-blue foreground \x1b[39m");
CHECK(body_start+R"(<font color="#0000ff"> bright-blue foreground </font>)"+body_end == res);
}
SECTION( "256-color 38;5;208 orange foreground foreground" ) {
std::string res = a2h.simpleParse("\x1b[38;5;208m orange (256 color) foreground \x1b[39m");
CHECK(body_start+R"(<font color="#ff8700"> orange (256 color) foreground </font>)"+body_end == res);
}
SECTION( "24-bit-color: 38 47 79 79 DarkSlateGray foreground" ) {
std::string res = a2h.simpleParse("\x1b[38;2;47;79;79m DarkSlateGray foreground \x1b[39m");
CHECK(body_start+R"(<font color="#2f4f4f"> DarkSlateGray foreground </font>)"+body_end == res);
}
}

TEST_CASE( "formatting", "[format]" ) {
ANSI_SGR2HTML a2h;
SECTION( "bold" ) {
std::string res = a2h.simpleParse("\x1b[1m bold TEXT \x1b[22m");
CHECK(body_start+R"(<b> bold TEXT </b>)"+body_end == res);
}
SECTION( "italic" ) {
std::string res = a2h.simpleParse("\x1b[3m italic \x1b[23m");
CHECK(body_start+R"(<i> italic </i>)"+body_end == res);
}
SECTION( "underline" ) {
std::string res = a2h.simpleParse("\x1b[4m underline \x1b[24m");
CHECK(body_start+R"(<u> underline </u>)"+body_end == res);
}
SECTION( "crossed-out" ) {
std::string res = a2h.simpleParse("\x1b[9m crossed-out \x1b[29m");
CHECK(body_start+R"(<s> crossed-out </s>)"+body_end == res);
}
}

TEST_CASE( "mixed", "[mixed]" ) {
ANSI_SGR2HTML a2h;
SECTION( "bg+fg colors" ) {
std::string res = a2h.simpleParse("\x1b[42;36m green bg cyan fg \x1b[39;49m");
CHECK(body_start+R"(<span style="background-color:#39b54a"><font color="#2cb5e9"> green bg cyan fg </font></span>)"+body_end == res);
}
SECTION( "bg+fg+bold" ) {
std::string res = a2h.simpleParse("\x1b[38;5;208;48;5;141;1m EvErYtHiNg \x1b[0m");
CHECK(body_start+R"(<font color="#ff8700"><span style="background-color:#af87ff"><b> EvErYtHiNg </b></span></font>)"+body_end == res);
}
}

TEST_CASE( "complex", "[complex]") {
ANSI_SGR2HTML a2h;

}

TEST_CASE( "basic bad inputs for simpleParse", "[basic_bad_simple]") {
ANSI_SGR2HTML a2h;
SECTION( "Not closed parameter" ) {
std::string res = a2h.simpleParse(std::string("\x1b[48;5;141m background color "));
REQUIRE(body_start+R"(<span style="background-color:#af87ff"> background color </span>)"+body_end == res);
std::string res = a2h.simpleParse("\x1b[48;5;141m background color ");
CHECK(body_start+R"(<span style="background-color:#af87ff"> background color </span>)"+body_end == res);
}
SECTION( "Wrong close parameter" ) {
std::string res = a2h.simpleParse("\x1b[42m open bg close fg \x1b[39m");
CHECK(body_start+R"(<span style="background-color:#39b54a"> open bg close fg </span>)"+body_end == res);
}
SECTION( "bg+fg colors wrong close orded" ) {
std::string res = a2h.simpleParse("\x1b[42;36m green bg cyan fg \x1b[49;39m");
CHECK(body_start+R"(<span style="background-color:#39b54a"><font color="#2cb5e9"> green bg cyan fg </span></font>)"+body_end == res);
}

SECTION( "bg+fg+bold then fg+bold" ) {
std::string res = a2h.simpleParse("\x1b[38;5;208;48;5;141;1m EvErYtHiNg \x1b[49m EvErYtHiNg but background");
// Wrong close order. As expected
CHECK(body_start+R"(<font color="#ff8700"><span style="background-color:#af87ff"><b> EvErYtHiNg </span> EvErYtHiNg but background</b></font>)"+body_end == res);
}
SECTION( "broken sgrs" ) {
std::string res = a2h.simpleParse("\x1b[62\x1b[65aaa");
CHECK(body_start+R"(aaa)"+body_end == res);
}
SECTION( "broken sgr then correct one" ) {
std::string res = a2h.simpleParse("\x1b[31\x1b[32m aaa");
CHECK(body_start+R"(<font color="#39b54" aaa</font>)"+body_end == res);
}
}

TEST_CASE( "complex bad inputs for simpleParse", "[complex_bad_simple]") {
ANSI_SGR2HTML a2h;
}

0 comments on commit 327c199

Please sign in to comment.