| 1 | /** |
| 2 | * \file t/tap.h |
| 3 | * |
| 4 | * \brief |
| 5 | * Helpers for following the Test Anything Protocol. |
| 6 | * |
| 7 | * Its interface mimics Test::More from Perl: |
| 8 | * https://perldoc.perl.org/Test/More.html |
| 9 | * |
| 10 | * Unlike Test::More, a test failure raises an exception and aborts the whole subtest. |
| 11 | */ |
| 12 | |
| 13 | #pragma once |
| 14 | |
| 15 | #include <exception> |
| 16 | #include <iostream> |
| 17 | |
| 18 | inline namespace tapcxx . . $ tap/ { |
| 19 | |
| 20 | struct failurecxx . . $ tap/failure# : stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error# { |
| 21 | failurecxx . . $ tap/failure#failure(9c546c94e5fcce4e).(const stdcxx . . $ std/::stringcxx . . $ std/string#& what) : stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#(what) {} |
| 22 | }; |
| 23 | |
| 24 | template <typename F> |
| 25 | static void runcxx . . $ tap/run(7ee90fd1703d5d92).(F test, const char *name) |
| 26 | { |
| 27 | bool ok = false; |
| 28 | try { |
| 29 | test(); |
| 30 | ok = true; |
| 31 | } catch (failurecxx . . $ tap/failure#& e) { |
| 32 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "# fail: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). e.whatcxx . . $ std/runtime_error#what(ba32b354807b2b81).() <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n"; |
| 33 | } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status# &rc) { |
| 34 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "# unexpected error: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). rc.messagecxx . . $ ot/status#message. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n"; |
| 35 | } |
| 36 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). (ok ? "ok" : "not ok") <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " - " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). name <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n"; |
| 37 | } |
| 38 | |
| 39 | void plancxx . . $ tap/plan(d4f767463ce0a6b3).(int tests) |
| 40 | { |
| 41 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "1.." <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). tests <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n"; |
| 42 | } |
| 43 | |
| 44 | template <typename T, typename U> |
| 45 | void iscxx . . $ tap/is(e56f9d162936dca9).(const T& got, const U& expected, const char* name) |
| 46 | { |
| 47 | if (got != expected) { |
| 48 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "# got: " << got << "\n" |
| 49 | "# expected: " << expected << "\n"; |
| 50 | throw failurecxx . . $ tap/failure#(namecxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | template <typename T, typename U> |
| 55 | void opaque_iscxx . . $ tap/opaque_is(e56f9d162936dca9).(const T& got, const U& expected, const char* name) |
| 56 | { |
| 57 | if (got != expected) |
| 58 | throw failurecxx . . $ tap/failure#(namecxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 59 | } |
| 60 | |
| 61 | template <> |
| 62 | void iscxx . . $ tap/is(96b83e82715b0c36).(const otcxx . . $ ot/::statuscxx . . $ ot/status#& got, const otcxx . . $ ot/::stcxx . . $ ot/st#& expected, const char* name) |
| 63 | { |
| 64 | if (got.codecxx . . $ ot/status#code. != expected) { |
| 65 | if (got.codecxx . . $ ot/status#code. == otcxx . . $ ot/::stcxx . . $ ot/st#::okcxx . . $ ot/st#ok.) |
| 66 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "# unexpected success\n"; |
| 67 | else |
| 68 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "# unexpected error: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). got.messagecxx . . $ ot/status#message. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n"; |
| 69 | throw failurecxx . . $ tap/failure#(namecxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | } |