diff --git a/README.md b/README.md index f3e28eb..19f6584 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ auto main() -> int when run, will print ```console:example/minimal_pass.log -test `truthy`...[CONSTEXPR PASS] -all tests passed (1 test) +test `truthy`...[CONSTEXPR PASS] +all tests passed (1 test) ``` @@ -46,10 +46,10 @@ auto main(int argc, char*[]) -> int will print ```console:example/minimal_fail.log -test `falsy`...[FAIL] example/minimal_fail.cpp:9 -(0 == 1) +test `falsy`...[FAIL] example/minimal_fail.cpp:9 +(0 == 1) -0 tests passed | 1 test failed +0 tests passed | 1 test failed ``` diff --git a/example/BUILD.bazel b/example/BUILD.bazel index 1ef4086..e2e1a83 100644 --- a/example/BUILD.bazel +++ b/example/BUILD.bazel @@ -1,10 +1,17 @@ -load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test") +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") load("//rules:binary_log.bzl", "synchronized_binary_log") +cc_library( + name = "skytest_wrapper", + hdrs = ["skytest.hpp"], + include_prefix = "skytest", + deps = ["//:skytest"], +) + cc_binary( name = "minimal_pass", srcs = ["minimal_pass.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) synchronized_binary_log( @@ -15,7 +22,7 @@ synchronized_binary_log( cc_binary( name = "minimal_fail", srcs = ["minimal_fail.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) synchronized_binary_log( @@ -26,19 +33,19 @@ synchronized_binary_log( cc_test( name = "binary_comparisons", srcs = ["binary_comparisons.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) cc_test( name = "logical_operations", srcs = ["logical_operations.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) cc_binary( name = "additional_output", srcs = ["additional_output.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) synchronized_binary_log( @@ -49,7 +56,7 @@ synchronized_binary_log( cc_binary( name = "user_defined_predicates", srcs = ["user_defined_predicates.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) synchronized_binary_log( @@ -61,7 +68,7 @@ cc_binary( name = "described_predicates_20", srcs = ["described_predicates_20.cpp"], copts = ["-std=c++20"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) synchronized_binary_log( @@ -72,7 +79,7 @@ synchronized_binary_log( cc_binary( name = "described_predicates", srcs = ["described_predicates.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) synchronized_binary_log( @@ -83,24 +90,60 @@ synchronized_binary_log( cc_test( name = "type_parameterized", srcs = ["type_parameterized.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) cc_test( name = "value_parameterized", srcs = ["value_parameterized.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) cc_test( name = "param_ref_parameterized", srcs = ["param_ref_parameterized.cpp"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], ) cc_test( name = "param_parameterized", srcs = ["param_parameterized.cpp"], copts = ["-std=c++20"], - deps = ["//:skytest"], + deps = [":skytest_wrapper"], +) + +update_targets = [ + ":minimal_pass_log.update.sh", + ":minimal_fail_log.update.sh", + ":additional_output_log.update.sh", + ":user_defined_predicates_log.update.sh", + ":described_predicates_log.update.sh", + ":described_predicates_20_log.update.sh", +] + +genrule( + name = "update_all_sh", + srcs = update_targets, + outs = ["update_all.sh"], + cmd = """ +set -euo pipefail +echo "set -euo pipefail" > $@ +echo "" >> $@ +echo "for cmd in {update_targets}; do" >> $@ +echo " \"\\$$cmd\"" >> $@ +echo "done" >> $@ +""".format( + update_targets = " ".join([ + "$(rootpath {})".format(x) + for x in update_targets + ]), + ), + tags = ["manual"], + visibility = ["//visibility:private"], +) + +sh_binary( + name = "update_all", + srcs = ["update_all.sh"], + data = update_targets, ) diff --git a/example/additional_output.log b/example/additional_output.log index 46458f1..53731c7 100644 --- a/example/additional_output.log +++ b/example/additional_output.log @@ -1,8 +1,8 @@ -test `string-view-ish`...[CONSTEXPR FAIL] example/additional_output.cpp:11 -(1 == 2) +test `string-view-ish`...[CONSTEXPR FAIL] example/additional_output.cpp:11 +(1 == 2) a message -test `ostream invocable closure`...[CONSTEXPR FAIL] example/additional_output.cpp:16 -(std::pair{...} == std::pair{...}) +test `ostream invocable closure`...[CONSTEXPR FAIL] example/additional_output.cpp:16 +(std::pair{...} == std::pair{...}) 1, 2 -0 tests passed | 2 tests failed +0 tests passed | 2 tests failed diff --git a/example/described_predicates.log b/example/described_predicates.log index 8fb578a..f6129e6 100644 --- a/example/described_predicates.log +++ b/example/described_predicates.log @@ -1,4 +1,4 @@ -test `empty array`...[CONSTEXPR FAIL] example/described_predicates.cpp:24 -empty([1, 2, 3]) +test `empty array`...[CONSTEXPR FAIL] example/described_predicates.cpp:24 +empty([1, 2, 3]) -0 tests passed | 1 test failed +0 tests passed | 1 test failed diff --git a/example/described_predicates_20.log b/example/described_predicates_20.log index 8b4b7d2..99ae19b 100644 Binary files a/example/described_predicates_20.log and b/example/described_predicates_20.log differ diff --git a/example/minimal_fail.log b/example/minimal_fail.log index 950d6b8..b4f4ea8 100644 --- a/example/minimal_fail.log +++ b/example/minimal_fail.log @@ -1,4 +1,4 @@ -test `falsy`...[FAIL] example/minimal_fail.cpp:9 -(0 == 1) +test `falsy`...[FAIL] example/minimal_fail.cpp:9 +(0 == 1) -0 tests passed | 1 test failed +0 tests passed | 1 test failed diff --git a/example/minimal_pass.log b/example/minimal_pass.log index d6375f6..f1933f6 100644 --- a/example/minimal_pass.log +++ b/example/minimal_pass.log @@ -1,2 +1,2 @@ -test `truthy`...[CONSTEXPR PASS] -all tests passed (1 test) +test `truthy`...[CONSTEXPR PASS] +all tests passed (1 test) diff --git a/example/skytest.hpp b/example/skytest.hpp new file mode 100644 index 0000000..1f1e2e9 --- /dev/null +++ b/example/skytest.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "_virtual_includes/skytest/skytest/skytest.hpp" + +#include + +template <> +const auto ::skytest::cfg<::skytest::override> = + ::skytest::runner<>{std::cout, ::skytest::colors{{}, {}, {}, {}}}; diff --git a/example/user_defined_predicates.log b/example/user_defined_predicates.log index f063a66..fa4a8a9 100644 --- a/example/user_defined_predicates.log +++ b/example/user_defined_predicates.log @@ -1,4 +1,4 @@ -test `empty array`...[CONSTEXPR FAIL] example/user_defined_predicates.cpp:16 -(lambda at example/user_defined_predicates.cpp:12:38){}([1, 2, 3]) +test `empty array`...[CONSTEXPR FAIL] example/user_defined_predicates.cpp:16 +(lambda at example/user_defined_predicates.cpp:12:38){}([1, 2, 3]) -0 tests passed | 1 test failed +0 tests passed | 1 test failed diff --git a/rules/binary_log.bzl b/rules/binary_log.bzl index ac92657..173cc2d 100644 --- a/rules/binary_log.bzl +++ b/rules/binary_log.bzl @@ -3,6 +3,7 @@ Rule for generating and testing log files for a binary's output """ load("@bazel_skylib//rules:diff_test.bzl", "diff_test") +load("@local_config_info//:defs.bzl", "BAZEL_WORKSPACE_ROOT") def binary_log( name, @@ -72,21 +73,17 @@ def synchronized_binary_log( set -euo pipefail echo "set -euo pipefail" > $@ echo "" >> $@ -echo "cd \\$${{BUILD_WORKSPACE_DIRECTORY}}" >> $@ +echo "cd {workspace_dir}" >> $@ echo "$(execpath {src}) > $(rootpath {log}) || true" >> $@ """.format( src = src, log = log, + workspace_dir = BAZEL_WORKSPACE_ROOT, ), tags = ["manual"], visibility = ["//visibility:private"], ) - native.sh_binary( - name = name + ".update", - srcs = [name + ".update.sh"], - ) - diff_test( name = name, file1 = log, @@ -95,3 +92,8 @@ echo "$(execpath {src}) > $(rootpath {log}) || true" >> $@ str(label).replace("@//", "//"), ), ) + + native.sh_binary( + name = name + ".update", + srcs = [name + ".update.sh"], + ) diff --git a/src/cfg.hpp b/src/cfg.hpp index 9a7ca3e..1dfe624 100644 --- a/src/cfg.hpp +++ b/src/cfg.hpp @@ -1,6 +1,5 @@ #pragma once -#include "src/default_printer.hpp" #include "src/runner.hpp" #include @@ -12,6 +11,6 @@ struct override template // NOLINTNEXTLINE(cppcoreguidelines-interfaces-global-init) -const auto cfg = runner{std::cout}; +const auto cfg = runner<>{std::cout}; } // namespace skytest diff --git a/src/default_printer.hpp b/src/default_printer.hpp index 75668c0..027ea04 100644 --- a/src/default_printer.hpp +++ b/src/default_printer.hpp @@ -12,20 +12,21 @@ namespace skytest { +struct colors +{ + std::string_view none = "\033[0m"; + std::string_view dim = "\033[2m"; + std::string_view pass = "\033[32m"; + std::string_view fail = "\033[31m"; +}; + class default_printer { template using priority = detail::priority; std::ostream& os_; - - struct colors - { - static constexpr auto none = std::string_view{"\033[0m"}; - static constexpr auto dim = std::string_view{"\033[2m"}; - static constexpr auto pass = std::string_view{"\033[32m"}; - static constexpr auto fail = std::string_view{"\033[31m"}; - }; + colors color_{}; struct tests { @@ -42,8 +43,8 @@ class default_printer template auto& print(std::true_type, const Relation& r) & { - const auto os = [&os = this->os_, &r]() -> auto& { - os << (r ? colors::pass : colors::fail) << colors::dim; + const auto os = [&os = this->os_, &color = this->color_, &r]() -> auto& { + os << (r ? color.pass : color.fail) << color.dim; return os; }; @@ -53,15 +54,14 @@ class default_printer os() << " " << Relation::predicate_type::symbol << " "; (*this) << detail::arg_fmt(std::get<1>(r.args)); - os() << ")" << colors::none; + os() << ")" << color_.none; return *this; } template auto& print(std::false_type, const Relation& r) & { - os_ << (r ? colors::pass : colors::fail) << colors::dim << r - << colors::none; + os_ << (r ? color_.pass : color_.fail) << color_.dim << r << color_.none; return *this; } @@ -80,10 +80,10 @@ class default_printer auto stream_impl(priority<4>, const summary& s) -> void { if (s.fail != 0) { - os_ << tests{s.pass} << " passed | " << colors::fail << tests{s.fail} - << " failed" << colors::none; + os_ << tests{s.pass} << " passed | " << color_.fail << tests{s.fail} + << " failed" << color_.none; } else { - os_ << colors::pass << "all tests passed" << colors::none << " (" + os_ << color_.pass << "all tests passed" << color_.none << " (" << tests{s.pass} << ")"; } @@ -94,35 +94,35 @@ class default_printer { os_ << "test `" << r.name << "`..."; - [&os = os_, runtime = r, compile_time = r.compile_time] { + [&os = os_, &color = color_, runtime = r, compile_time = r.compile_time] { if (runtime and compile_time == true) { - os << colors::pass << "[CONSTEXPR PASS]"; + os << color.pass << "[CONSTEXPR PASS]"; return; } if (runtime and compile_time == std::nullopt) { - os << colors::pass << "[PASS]"; + os << color.pass << "[PASS]"; return; } if (runtime and compile_time == false) { - os << colors::pass << "[PASS]" << colors::fail << colors::dim + os << color.pass << "[PASS]" << color.fail << color.dim << "(CONSTEXPR FAIL)"; return; } if (not runtime and compile_time == true) { - os << colors::fail << "[FAIL]" << colors::pass << colors::dim + os << color.fail << "[FAIL]" << color.pass << color.dim << "(CONSTEXPR PASS)"; return; } if (not runtime and compile_time == std::nullopt) { - os << colors::fail << "[FAIL]"; + os << color.fail << "[FAIL]"; return; } if (not runtime and compile_time == false) { - os << colors::fail << "[CONSTEXPR FAIL]"; + os << color.fail << "[CONSTEXPR FAIL]"; return; } }(); - os_ << colors::none; + os_ << color_.none; if (not r) { os_ << " " << r.source.file_name() << ":" << r.source.line() << "\n"; @@ -154,7 +154,8 @@ class default_printer } public: - default_printer(std::ostream& os) : os_{os} {} + explicit default_printer(std::ostream& os, colors c = {}) : os_{os}, color_{c} + {} template friend auto& operator<<(default_printer& p, const T& t) diff --git a/src/runner.hpp b/src/runner.hpp index 31f743b..f65bdad 100644 --- a/src/runner.hpp +++ b/src/runner.hpp @@ -1,5 +1,6 @@ #pragma once +#include "src/default_printer.hpp" #include "src/result.hpp" #include "src/utility.hpp" @@ -11,7 +12,7 @@ namespace detail { struct aborts_fn; } -template +template class runner { mutable Printer printer_; @@ -31,6 +32,12 @@ class runner explicit runner(Printer p) : printer_{p} {} + template < + class... Args, + class = std::enable_if_t>> + runner(Args&&... args) : printer_{std::forward(args)...} + {} + ~runner() { if (silent_) {