<!-- description --> @brief Used to execute the actual checks. We put the checks in this function so that we can validate the tests both at compile-time and at run-time. If a bsl::ut_check fails, the tests will either fail fast at run-time, or will produce a compile-time error. <!-- inputs/outputs --> @return Always returns bsl::exit_success.
| 54 | /// @return Always returns bsl::exit_success. |
| 55 | /// |
| 56 | [[nodiscard]] constexpr auto |
| 57 | tests() noexcept -> bsl::exit_code |
| 58 | { |
| 59 | /// NOTE: |
| 60 | /// - The tests() function is executed both at compile-time and at |
| 61 | /// runtime. This is done using the following at the bottom of this |
| 62 | /// file. |
| 63 | /// |
| 64 | /// static_assert(example::tests() == bsl::ut_success()); |
| 65 | /// return example::tests(); |
| 66 | /// |
| 67 | /// - Anything executed in a static_assert is executed at compile-time |
| 68 | /// while the other call is your typical runtime call. |
| 69 | /// |
| 70 | |
| 71 | /// NOTE: |
| 72 | /// - BSL unit tests use behavioral driven unit testing. |
| 73 | /// https://en.wikipedia.org/wiki/Behavior-driven_development |
| 74 | /// |
| 75 | /// - All of these calls are syntactic sugar, meaning they do |
| 76 | /// absolutely nothing, except to ensure you are setting up your |
| 77 | /// tests using behavioral driven methods. The actual work is |
| 78 | /// performed by the following: |
| 79 | /// - bsl::ut_check() - used inside bsl::ut_then, and is used to |
| 80 | /// verify the results of your unit test |
| 81 | /// - bsl::ut_required_step() - used inside of bsl::ut_when, and |
| 82 | /// is used to verify that your unit test's set up is correct. |
| 83 | /// |
| 84 | |
| 85 | bsl::ut_scenario{"this is what I am testing"} = [&]() noexcept { |
| 86 | bsl::ut_given{"given the following"} = [&]() noexcept { |
| 87 | /// add variables here |
| 88 | bsl::ut_when{"when we do the following"} = [&]() noexcept { |
| 89 | /// add function calls here |
| 90 | bsl::ut_then{"we expect the following"} = [&]() noexcept { |
| 91 | /// add results checking here |
| 92 | }; |
| 93 | }; |
| 94 | }; |
| 95 | }; |
| 96 | |
| 97 | /// NOTE: |
| 98 | /// - There are a couple of different ways to do this. Once way |
| 99 | /// is to group tests under the same scenario as follows: |
| 100 | /// |
| 101 | |
| 102 | bsl::ut_scenario{"verify +="} = [&]() noexcept { |
| 103 | bsl::ut_given{} = [&]() noexcept { |
| 104 | constexpr auto data1{42_umx}; |
| 105 | bsl::safe_umx mut_data2{}; |
| 106 | bsl::ut_when{} = [&]() noexcept { |
| 107 | mut_data2 += data1; |
| 108 | bsl::ut_then{"adds correctly"} = [&]() noexcept { |
| 109 | bsl::ut_check(mut_data2.checked() == data1); |
| 110 | }; |
| 111 | }; |
| 112 | }; |
| 113 |
no test coverage detected