| 1 | /************************************************************************************ |
| 2 | * |
| 3 | * D++, A Lightweight C++ library for Discord |
| 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * Copyright 2021 Craig Edwards and D++ contributors |
| 7 | * (https://github.com/brainboxdotcc/DPP/graphs/contributors) |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | ************************************************************************************/ |
| 22 | #include "test.h" |
| 23 | #include <dpp/dpp.h> |
| 24 | #include <dpp/json.h> |
| 25 | |
| 26 | double start = dpp::utility::time_f(); |
| 27 | bool offline = false; |
| 28 | bool extended = false; |
| 29 | |
| 30 | dpp::snowflake TEST_GUILD_ID = std::stoull(SAFE_GETENV("TEST_GUILD_ID")); |
| 31 | dpp::snowflake TEST_TEXT_CHANNEL_ID = std::stoull(SAFE_GETENV("TEST_TEXT_CHANNEL_ID")); |
| 32 | dpp::snowflake TEST_VC_ID = std::stoull(SAFE_GETENV("TEST_VC_ID")); |
| 33 | dpp::snowflake TEST_USER_ID = std::stoull(SAFE_GETENV("TEST_USER_ID")); |
| 34 | dpp::snowflake TEST_EVENT_ID = std::stoull(SAFE_GETENV("TEST_EVENT_ID")); |
| 35 | |
| 36 | test_t::test_t(std::string_view testname, std::string_view testdesc, int testflags) : name{testname}, description{testdesc}, flags{static_cast<test_flags_t>(testflags)} { |
| 37 | tests.push_back(this); |
| 38 | } |
| 39 | |
| 40 | void set_status(test_t &test, test_status_t newstatus, std::string_view message) { |
| 41 | std::lock_guard<std::mutex> locker(loglock); |
| 42 | |
| 43 | if (is_skipped(test) || newstatus == test.status) { |
| 44 | return; |
| 45 | } |
| 46 | if (test.status != ts_failed) { // disallow changing the state of a failed test, but we still log |
| 47 | test.status = newstatus; |
| 48 | } |
| 49 | switch (newstatus) { |
| 50 | case ts_started: |
| 51 | std::cout << "[" << std::fixed << std::setprecision(3) << get_time() << "]: " << "[\u001b[33mTESTING\u001b[0m] " << test.description << (message.empty() ? "" : " - " + std::string{message} ) << "\n"; |
| 52 | break; |
| 53 | |
| 54 | case ts_failed: |
| 55 | std::cout << "[" << std::fixed << std::setprecision(3) << get_time() << "]: " << "[\u001b[31mFAILED\u001b[0m] " << test.description << (message.empty() ? "" : " - " + std::string{message} ) << "\n"; |
| 56 | break; |
| 57 | |
| 58 | case ts_success: |
| 59 | std::cout << "[" << std::fixed << std::setprecision(3) << get_time() << "]: " << "[\u001b[32mSUCCESS\u001b[0m] " << test.description << (message.empty() ? "" : " - " + std::string{message} ) << "\n"; |
| 60 | break; |
| 61 | |
| 62 | case ts_skipped: |
| 63 | std::cout << "[" << std::fixed << std::setprecision(3) << get_time() << "]: " << "[\u001b[33mSKIPPED\u001b[0m] " << test.description << (message.empty() ? "" : " - " + std::string{message} ) << "\n"; |
| 64 | break; |
| 65 | |
| 66 | default: |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void start_test(test_t &test) { |
| 72 | set_status(test, ts_started); |
| 73 | } |
| 74 | |
| 75 | void skip_test(test_t &test) { |
| 76 | set_status(test, ts_skipped); |
| 77 | } |
| 78 | |
| 79 | void set_test(test_t &test, bool success) { |
| 80 | if (test.status == ts_not_executed) { |
| 81 | set_status(test, success ? ts_success : ts_started); |
| 82 | } else { |
| 83 | set_status(test, success ? ts_success : ts_failed); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | bool is_skipped(const test_t &test) { |
| 88 | return (test.flags & tf_online && offline) || (test.flags & tf_extended && !extended) || (test.flags & tf_coro && !coro); |
| 89 | } |
| 90 | |
| 91 | double get_start_time() { |
| 92 | return start; |
| 93 | } |
| 94 | |
| 95 | double get_time() { |
| 96 | return dpp::utility::time_f() - get_start_time(); |
| 97 | } |
| 98 | |
| 99 | int test_summary() { |
| 100 | /* Report on all test cases */ |
| 101 | std::lock_guard<std::mutex> locker(loglock); |
| 102 | int failed = 0, passed = 0, skipped = 0; |
| 103 | std::cout << "\u001b[37;1m\n\nUNIT TEST SUMMARY\n==================\n\u001b[0m"; |
| 104 | for (auto & t : tests) { |
| 105 | std::cout << std::left << std::setw(60) << t->description << " " << std::fixed << std::setw(6); |
| 106 | if (t->status == ts_skipped || (t->flags & tf_online && offline) || (t->flags & tf_extended && !extended) || (t->flags & tf_coro && !coro)) { |
| 107 | skipped++; |
| 108 | std::cout << "\u001b[33mSKIPPED"; |
| 109 | } else { |
| 110 | if (t->status == ts_success) { |
| 111 | passed++; |
| 112 | std::cout << "\u001b[32mPASS"; |
| 113 | } else { |
| 114 | failed++; |
| 115 | std::cout << (t->status == ts_not_executed ? "\u001b[31mNOT EXECUTED" : "\u001b[31mFAIL"); |
| 116 | } |
| 117 | } |
| 118 | std::cout << std::setw(0) << "\u001b[0m\n"; |
| 119 | } |
| 120 | std::cout << "\u001b[37;1m\nExecution finished in " << std::fixed << std::setprecision(3) << get_time() << std::setprecision(0) << " seconds.\nFailed: " << failed << " Passed: " << passed << (skipped ? " Skipped: " : "") << (skipped ? std::to_string(skipped) : "") << " Percentage: " << std::setprecision(2) << ((float)(passed) / (float)(passed + failed) * 100.0f) << "%\u001b[0m\n"; |
| 121 | return failed; |
| 122 | } |
| 123 | |
| 124 | namespace { |
| 125 | std::string get_testdata_dir() { |
| 126 | char *env_var = getenv("TEST_DATA_DIR"); |
| 127 | |
| 128 | return (env_var ? env_var : "../../testdata/"); |
| 129 | } |
| 130 | } // namespace |
| 131 | |
| 132 | std::vector<uint8_t> load_test_audio() { |
| 133 | std::vector<uint8_t> testaudio; |
| 134 | std::string dir = get_testdata_dir(); |
| 135 | std::ifstream input (dir + "Robot.pcm", std::ios::in|std::ios::binary|std::ios::ate); |
| 136 | if (input.is_open()) { |
| 137 | size_t testaudio_size = input.tellg(); |
| 138 | testaudio.resize(testaudio_size); |
| 139 | input.seekg(0, std::ios::beg); |
| 140 | input.read((char*)testaudio.data(), testaudio_size); |
| 141 | input.close(); |
| 142 | } |
| 143 | else { |
| 144 | std::cout << "ERROR: Can't load " + dir + "Robot.pcm\n"; |
| 145 | exit(1); |
| 146 | } |
| 147 | return testaudio; |
| 148 | } |
| 149 | |
| 150 | std::vector<std::byte> load_data(const std::string& file) { |
| 151 | std::vector<std::byte> testimage; |
| 152 | std::string dir = get_testdata_dir(); |
| 153 | std::ifstream input (dir + file, std::ios::in|std::ios::binary|std::ios::ate); |
| 154 | if (input.is_open()) { |
| 155 | size_t testimage_size = input.tellg(); |
| 156 | testimage.resize(testimage_size); |
| 157 | input.seekg(0, std::ios::beg); |
| 158 | input.read((char*)testimage.data(), testimage_size); |
| 159 | input.close(); |
| 160 | } |
| 161 | else { |
| 162 | std::cout << "ERROR: Can't load " + dir + file + "\n"; |
| 163 | exit(1); |
| 164 | } |
| 165 | return testimage; |
| 166 | } |
| 167 | |
| 168 | std::string get_token() { |
| 169 | char* t = getenv("DPP_UNIT_TEST_TOKEN"); |
| 170 | std::string tok; |
| 171 | if (!t) { |
| 172 | offline = true; |
| 173 | } else { |
| 174 | tok = std::string(t); |
| 175 | if (tok.empty()) { |
| 176 | offline = true; |
| 177 | } |
| 178 | } |
| 179 | return tok; |
| 180 | } |
| 181 | |
| 182 | void wait_for_tests() { |
| 183 | uint16_t ticks = 0; |
| 184 | while (ticks < TEST_TIMEOUT) { |
| 185 | size_t finished = 0; |
| 186 | for (auto t : tests) { |
| 187 | if (t->status != ts_started) { |
| 188 | finished++; |
| 189 | } else if (is_skipped(*t)) { |
| 190 | finished++; |
| 191 | t->status = ts_skipped; |
| 192 | std::cout << "[" << std::fixed << std::setprecision(3) << get_time() << "]: " << "[\u001b[33mSKIPPED\u001b[0m] " << t->description << "\n"; |
| 193 | } |
| 194 | } |
| 195 | if (finished == tests.size()) { |
| 196 | std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 197 | return; |
| 198 | } |
| 199 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 200 | ticks++; |
| 201 | } |
| 202 | for (auto t : tests) { |
| 203 | if (t->status == ts_started) { |
| 204 | std::cout << "[" << std::fixed << std::setprecision(3) << get_time() << "]: " << "[\u001b[31mTIMEOUT\u001b[0m] " << t->description << "\n"; |
| 205 | } |
| 206 | } |
| 207 | } |