Skip to content

Commit

Permalink
improved accuracy and reduced line numbers for some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Todor committed Aug 9, 2024
1 parent 8082e43 commit 34b6060
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 95 deletions.
42 changes: 21 additions & 21 deletions src/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,19 @@ TEST_CASE("create_sequence")
{
context.args.shell = "bash";
const std::string result = create_sequence("32", context);
CHECK(result.find("\001\033[32m\002") != std::string::npos);
CHECK(result == "\001\033[32m\002");
}
SUBCASE("fish")
{
context.args.shell = "fish";
const std::string result = create_sequence("32", context);
CHECK(result.find("\001\033[32m\002") != std::string::npos);
CHECK(result == "\001\033[32m\002");
}
SUBCASE("zsh")
{
context.args.shell = "zsh";
const std::string result = create_sequence("32", context);
CHECK(result.find("%{\033[32m%}") != std::string::npos);
CHECK(result == "%{\033[32m%}");
}
}

Expand All @@ -189,47 +189,47 @@ TEST_CASE("to_color_code")
SUBCASE("foreground named")
{
const std::string result = to_color_code("green", 0, context);
CHECK(result.find("\001\033[32m\002") != std::string::npos);
CHECK(result == "\001\033[32m\002");
}
SUBCASE("background named")
{
const std::string result = to_color_code("green", 10, context);
CHECK(result.find("\001\033[42m\002") != std::string::npos);
CHECK(result == "\001\033[42m\002");
}
SUBCASE("foreground color id")
{
const std::string result = to_color_code("128", 0, context);
CHECK(result.find("\001\033[38;5;128m\002") != std::string::npos);
CHECK(result == "\001\033[38;5;128m\002");
}
SUBCASE("background color id")
{
const std::string result = to_color_code("128", 10, context);
CHECK(result.find("\001\033[48;5;128m\002") != std::string::npos);
CHECK(result == "\001\033[48;5;128m\002");
}
SUBCASE("foreground rgb")
{
const std::string result = to_color_code("30;40;50", 0, context);
CHECK(result.find("\001\033[38;2;30;40;50m\002") != std::string::npos);
CHECK(result == "\001\033[38;2;30;40;50m\002");
}
SUBCASE("background rgb")
{
const std::string result = to_color_code("30;40;50", 10, context);
CHECK(result.find("\001\033[48;2;30;40;50m\002") != std::string::npos);
CHECK(result == "\001\033[48;2;30;40;50m\002");
}
SUBCASE("rgb with comma and space")
{
const std::string result = to_color_code("30, 40, 50", 0, context);
CHECK(result.find("\001\033[38;2;30;40;50m\002") != std::string::npos);
CHECK(result == "\001\033[38;2;30;40;50m\002");
}
SUBCASE("foreground hex")
{
const std::string result = to_color_code("#3f7fb7", 0, context);
CHECK(result.find("\001\033[38;2;63;127;183m\002") != std::string::npos);
CHECK(result == "\001\033[38;2;63;127;183m\002");
}
SUBCASE("background hex")
{
const std::string result = to_color_code("#3f7fb7", 10, context);
CHECK(result.find("\001\033[48;2;63;127;183m\002") != std::string::npos);
CHECK(result == "\001\033[48;2;63;127;183m\002");
}
}

Expand All @@ -238,31 +238,31 @@ TEST_CASE("set_text_mode")
Context context;
context.args.shell = "bash";
const std::string result = set_text_mode(1, context);
CHECK(result.find("\001\033[1m\002") != std::string::npos);
CHECK(result == "\001\033[1m\002");
}

TEST_CASE("set_foreground")
{
Context context;
context.args.shell = "bash";
const std::string result = set_foreground("green", context);
CHECK(result.find("\001\033[32m\002") != std::string::npos);
CHECK(result == "\001\033[32m\002");
}

TEST_CASE("set_background")
{
Context context;
context.args.shell = "bash";
const std::string result = set_background("green", context);
CHECK(result.find("\001\033[42m\002") != std::string::npos);
CHECK(result == "\001\033[42m\002");
}

TEST_CASE("reset_all")
{
Context context;
context.args.shell = "bash";
const std::string result = reset_all(context);
CHECK(result.find("\001\033[0m\002") != std::string::npos);
CHECK(result == "\001\033[0m\002");
}

TEST_CASE("reset_text_mode")
Expand All @@ -272,17 +272,17 @@ TEST_CASE("reset_text_mode")
SUBCASE("bold")
{
const std::string result = reset_text_mode(1, context);
CHECK(result.find("\001\033[22m\002") != std::string::npos);
CHECK(result == "\001\033[22m\002");
}
SUBCASE("dim")
{
const std::string result = reset_text_mode(2, context);
CHECK(result.find("\001\033[22m\002") != std::string::npos);
CHECK(result == "\001\033[22m\002");
}
SUBCASE("italic")
{
const std::string result = reset_text_mode(3, context);
CHECK(result.find("\001\033[23m\002") != std::string::npos);
CHECK(result == "\001\033[23m\002");
}
}

Expand All @@ -291,15 +291,15 @@ TEST_CASE("reset_foreground")
Context context;
context.args.shell = "bash";
const std::string result = reset_foreground(context);
CHECK(result.find("\001\033[39m\002") != std::string::npos);
CHECK(result == "\001\033[39m\002");
}

TEST_CASE("reset_background")
{
Context context;
context.args.shell = "bash";
const std::string result = reset_background(context);
CHECK(result.find("\001\033[49m\002") != std::string::npos);
CHECK(result == "\001\033[49m\002");
}

TEST_CASE("move_cursor_up")
Expand Down
3 changes: 1 addition & 2 deletions src/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ TEST_CASE("call_module")
{
Context context;
context.args.exit_status = 255;
const std::string result = call_module("exit_status", context);
CHECK(result.find("255") != std::string::npos);
CHECK(call_module("exit_status", context) == "255");
}

#endif
6 changes: 2 additions & 4 deletions src/modules/date.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ TEST_CASE("date")
SUBCASE("correct date %F format")
{
context.config.date.format = "%F";
const std::string result = date(context);
CHECK(result == "2024-08-03");
CHECK(date(context) == "2024-08-03");
}
SUBCASE("correct date %T format")
{
context.config.date.format = "%T";
const std::string result = date(context);
CHECK(result == "20:04:34");
CHECK(date(context) == "20:04:34");
}
}

Expand Down
21 changes: 7 additions & 14 deletions src/modules/directory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,48 @@ TEST_CASE("directory")
SUBCASE("replace home with ~")
{
context.HOME = "/a";
const std::string result = directory(context);
CHECK(result == "~/b/c");
CHECK(directory(context) == "~/b/c");
}
SUBCASE("PWD when not in home directory")
{
context.HOME = "/d";
const std::string result = directory(context);
CHECK(result == "/a/b/c");
CHECK(directory(context) == "/a/b/c");
}
SUBCASE("from repository")
{
context.PWD = get_env("PWD");
context.config.directory.from_repository = true;
context.git_repository_detected = true;
const std::string result = directory(context);
CHECK(result == ".../blaze");
CHECK(directory(context) == ".../blaze");
}
SUBCASE("from repository inside subdirectory")
{
context.HOME = get_env("HOME");
context.PWD = get_env("PWD") + "/a/b/c";
context.config.directory.from_repository = true;
context.git_repository_detected = true;
const std::string result = directory(context);
CHECK(result == ".../blaze/a/b/c");
CHECK(directory(context) == ".../blaze/a/b/c");
}
SUBCASE("from repository directory limit")
{
context.HOME = get_env("HOME");
context.PWD = get_env("PWD") + "/a/b/c/d/e/f";
context.config.directory.from_repository = true;
context.git_repository_detected = true;
const std::string result = directory(context);
CHECK(result == ".../c/d/e/f");
CHECK(directory(context) == ".../c/d/e/f");
}
SUBCASE("directory limit reached")
{
context.HOME = "/a";
context.PWD = "/a/b/c/d/e";
const std::string result = directory(context);
CHECK(result == ".../b/c/d/e");
CHECK(directory(context) == ".../b/c/d/e");
}
SUBCASE("directory limit not reached")
{
context.HOME = "/a";
context.PWD = "/a/b/c/d/e";
context.config.directory.limit = 5;
const std::string result = directory(context);
CHECK(result == "~/b/c/d/e");
CHECK(directory(context) == "~/b/c/d/e");
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/modules/dummy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ std::string dummy(const Context &context)
TEST_CASE("dummy")
{
Context context;
const std::string result = dummy(context);
CHECK(result == "");
CHECK(dummy(context) == "");
}

#endif
Expand Down
24 changes: 8 additions & 16 deletions src/modules/duration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,63 +76,55 @@ TEST_CASE("duration")
{
context.args.start_time = 0;
context.args.finish_time = 10;
const std::string result = duration(context);
CHECK(result == "");
CHECK(duration(context) == "");
}
SUBCASE("empty if finish time is zero")
{
context.args.start_time = 10;
context.args.finish_time = 0;
const std::string result = duration(context);
CHECK(result == "");
CHECK(duration(context) == "");
}
SUBCASE("empty if less than display_from")
{
context.config.duration.display_from = 10;
context.args.start_time = 1;
context.args.finish_time = 10;
const std::string result = duration(context);
CHECK(result == "");
CHECK(duration(context) == "");
}
SUBCASE("correct fractional")
{
context.config.duration.precision = 4;
context.config.duration.display_from = 1;
context.args.start_time = 3.5;
context.args.finish_time = 5.512345;
const std::string result = duration(context);
CHECK(result == "2.0123s");
CHECK(duration(context) == "2.0123s");
}
SUBCASE("skip fractional above display_fractional_until")
{
context.config.duration.precision = 4;
context.config.duration.display_fractional_until = 5;
context.args.start_time = 1;
context.args.finish_time = 6.412345;
const std::string result = duration(context);
CHECK(result == "5s");
CHECK(duration(context) == "5s");
}
SUBCASE("correct seconds")
{
context.config.duration.precision = 1;
context.args.start_time = 3;
context.args.finish_time = 30.4;
const std::string result = duration(context);
CHECK(result == "27s");
CHECK(duration(context) == "27s");
}
SUBCASE("correct minutes")
{
context.args.start_time = 3;
context.args.finish_time = 300.4;
const std::string result = duration(context);
CHECK(result == "4m 57s");
CHECK(duration(context) == "4m 57s");
}
SUBCASE("correct hours")
{
context.args.start_time = 3;
context.args.finish_time = 5900.4;
const std::string result = duration(context);
CHECK(result == "1h 38m 17s");
CHECK(duration(context) == "1h 38m 17s");
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/modules/exit_status.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,22 @@ TEST_CASE("exit_status")
context.args.exit_status = 0;
SUBCASE("empty if exit_status is zero and non_zero_only is true")
{
const std::string result = exit_status(context);
CHECK(result == "");
CHECK(exit_status(context) == "");
}
SUBCASE("correct exit_status 0")
{
context.config.exit_status.non_zero_only = false;
const std::string result = exit_status(context);
CHECK(result == "0");
CHECK(exit_status(context) == "0");
}
SUBCASE("correct positive exit_status")
{
context.args.exit_status = 255;
const std::string result = exit_status(context);
CHECK(result == "255");
CHECK(exit_status(context) == "255");
}
SUBCASE("correct negative exit_status")
{
context.args.exit_status = -255;
const std::string result = exit_status(context);
CHECK(result == "-255");
CHECK(exit_status(context) == "-255");
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/modules/git_branch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ TEST_CASE("git_branch")
SUBCASE("not a repository")
{
context.git_repository_detected = false;
const std::string result = git_branch(context);
CHECK(result == "");
CHECK(git_branch(context) == "");
}
SUBCASE("branch")
{
context.HOME = get_env("HOME");
context.PWD = get_env("PWD");
context.git_repository_detected = true;
const std::string result = git_branch(context);
CHECK(result == "master");
CHECK(git_branch(context) == "master");
}
}

Expand Down
Loading

0 comments on commit 34b6060

Please sign in to comment.