#define LIBREXY_ENABLE_DEBUG_LEVEL 2 #include "rexy/format.hpp" #include "rexy/string.hpp" #include "rexy/debug_print.hpp" #include "rexy/cx/string.hpp" #include using namespace rexy::str_literals; [[noreturn]] void test_fail(rexy::string_view failed, rexy::string_view expected){ rexy::debug::print_error("expected \"%s\", got \"%s\"\n", expected.c_str(), failed.c_str()); exit(-1); } [[noreturn]] void test_fail(void){ exit(-1); } #define PERFORM_TEST(desired, formt, ...) \ do{ \ rexy::string str = rexy::format( (formt), __VA_ARGS__); \ if(str != (desired) ){ \ test_fail( (str), (desired) ); \ } \ }while(0) struct X { int i; constexpr operator int(void)const{return i;} }; template class rexy::formatter : public rexy::formatter { public: template auto format(X&, FormatContext& ctx) -> decltype(ctx.out()){ *(ctx.out()++) = 'X'; return std::move(ctx.out()); //return rexy::formatter::format(t.i, ctx); } }; void do_brace_escapes(void){ PERFORM_TEST("this is } a test string"_sv, "this is }} a test string", nullptr); PERFORM_TEST("this is a test {string"_sv, "this is a test {{string", nullptr); PERFORM_TEST("this }is a test {string"_sv, "this }}is a test {{string", nullptr); PERFORM_TEST("this {is a tes}t string"_sv, "this {{is a tes}}t string", nullptr); } void do_empty_format(void){ PERFORM_TEST("0"_sv, "{}", 0); PERFORM_TEST("0"_sv, "{}", 0ul); PERFORM_TEST("0"_sv, "{}", short{0}); PERFORM_TEST("0"_sv, "{}", 0.0f); PERFORM_TEST("0x0"_sv, "{}", nullptr); PERFORM_TEST("true"_sv, "{}", true); PERFORM_TEST("cstring"_sv, "{}", "cstring"); PERFORM_TEST("string_view"_sv, "{}", "string_view"_sv); PERFORM_TEST("string"_sv, "{}", rexy::string{"string"}); PERFORM_TEST("c"_sv, "{}", 'c'); } void do_index_specifiers(void){ PERFORM_TEST("2"_sv, "{2}", 0, 1, 2); PERFORM_TEST("0"_sv, "{0}", 0, 1, 2); PERFORM_TEST("1"_sv, "{1}", "string", 1, 2.8f); } void do_dynamic_index_specifiers(void){ PERFORM_TEST("| 5|"_sv, "|{:{}}|", 5, 10); PERFORM_TEST("| 5.80|"_sv, "|{:{}.{}f}|", 5.8f, 10, 2); PERFORM_TEST("| 5|"_sv, "|{1:{0}}|", 10, 5); PERFORM_TEST("| 5.80|"_sv, "|{0:{4}.{1}f}|", 5.8f, 2, 7, 8, 10); } void do_fill_and_align(void){ PERFORM_TEST("*****6"_sv, "{:*>6}", 6); PERFORM_TEST("6*****"_sv, "{:*<6}", 6); PERFORM_TEST("**6***"_sv, "{:*^6}", 6); PERFORM_TEST("*8.20*"_sv, "{:*^6.2f}", 8.2f); PERFORM_TEST("-2****"_sv, "{:*<6}", -2); PERFORM_TEST("***str"_sv, "{:*>6}", "str"); } //TODO more test coverage int main(){ do_brace_escapes(); do_empty_format(); do_index_specifiers(); do_dynamic_index_specifiers(); do_fill_and_align(); }