GCC Code Coverage Report


Directory: ../
File: test/util/TestUtil.h
Date: 2025-11-16 23:33:18
Coverage Exec Excl Total
Lines: 100.0% 5 0 5
Functions: 100.0% 2 0 2
Branches: 50.0% 7 0 14

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <filesystem>
6 #include <functional>
7 #include <string>
8 #include <vector>
9
10 #include <gtest/gtest.h>
11
12 namespace spice::testing {
13
14 const char *const PATH_TEST_FILES = "./test-files/";
15 static constexpr size_t EXPECTED_NUMBER_OF_TESTS = 250;
16
17 const char *const GDB_READING_SYMBOLS_MESSAGE = "Reading symbols from ";
18 const char *const GDB_INFERIOR_MESSAGE = "[Inferior";
19
20 const char *const INPUT_NAME_LINKER_FLAGS = "linker-flags.txt";
21 const char *const INPUT_NAME_CLI_FLAGS = "cli-flags.txt";
22
23 const char *const REF_NAME_SOURCE = "source.spice";
24 const char *const REF_NAME_PARSE_TREE = "parse-tree.dot";
25 const char *const REF_NAME_SYNTAX_TREE = "syntax-tree.dot";
26 const char *const REF_NAME_DEP_GRAPH = "dependency-graph.dot";
27 const char *const REF_NAME_SYMBOL_TABLE = "symbol-table.json";
28 const char *const REF_NAME_TYPE_REGISTRY = "type-registry.out";
29 const char *const REF_NAME_CACHE_STATS = "cache-stats.out";
30 const char *const REF_NAME_IR = "ir-code.ll";
31 const char *const REF_NAME_ASM = "assembly.asm";
32 static constexpr const char *const REF_NAME_OPT_IR[6] = {
33 "ir-code.ll", "ir-code-O1.ll", "ir-code-O2.ll", "ir-code-O3.ll", "ir-code-Os.ll", "ir-code-Oz.ll",
34 };
35 const char *const REF_NAME_EXECUTION_OUTPUT = "cout.out";
36 const char *const REF_NAME_GDB_OUTPUT = "debug.out";
37 const char *const REF_NAME_ERROR_OUTPUT = "exception.out";
38 const char *const REF_NAME_WARNING_OUTPUT = "warning.out";
39 const char *const REF_NAME_EXIT_CODE = "exit-code.out";
40
41 const char *const CTL_SKIP_DISABLED = "disabled";
42 const char *const CTL_SKIP_GH = "skip-gh-actions";
43 const char *const CTL_SKIP_WINDOWS = "skip-windows";
44 const char *const CTL_SKIP_MACOS = "skip-macos";
45 const char *const CTL_RUN_BUILTIN_TESTS = "run-builtin-tests";
46 const char *const CTL_DEBUG_SCRIPT = "debug.gdb";
47
48 struct TestCase {
49 const std::string testSuite;
50 const std::string testName;
51 const std::filesystem::path testPath;
52 };
53
54 // Typedefs
55 using GetOutputFct = const std::function<std::string()> &;
56 using ModifyOutputFct = const std::function<void(std::string &expectedOutput, std::string &actualOutput)> &;
57
58 class TestUtil {
59 public:
60 // Public structs
61 struct NameResolver {
62 433 template <class T> std::string operator()(const ::testing::TestParamInfo<T> &info) const {
63
1/2
✓ Branch 2 → 3 taken 433 times.
✗ Branch 2 → 35 not taken.
433 T testCase = static_cast<T>(info.param);
64
6/12
✓ Branch 3 → 4 taken 433 times.
✗ Branch 3 → 31 not taken.
✓ Branch 4 → 5 taken 433 times.
✗ Branch 4 → 29 not taken.
✓ Branch 5 → 6 taken 433 times.
✗ Branch 5 → 24 not taken.
✓ Branch 6 → 7 taken 433 times.
✗ Branch 6 → 22 not taken.
✓ Branch 7 → 8 taken 433 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 433 times.
✗ Branch 8 → 18 not taken.
866 return TestUtil::toCamelCase(testCase.testSuite) + "_" + TestUtil::toCamelCase(testCase.testName);
65 433 }
66 };
67
68 // Public static methods
69 static void parseTestArgs(const std::filesystem::path &sourceCodePath, std::vector<std::string> &args);
70 static std::vector<TestCase> collectTestCases(const char *suiteName, bool useSubDirs);
71 static bool checkRefMatch(
72 const std::filesystem::path &originalRefPath, GetOutputFct getActualOutput,
73 438 ModifyOutputFct modifyOutputFct = [](std::string &, std::string &) {}, bool x86Only = false);
74 static bool doesRefExist(const std::filesystem::path &originalRefPath);
75 static void handleError(const TestCase &testCase, const std::exception &error);
76 static std::vector<std::string> getSubdirs(const std::filesystem::path &basePath);
77 static std::vector<std::string> getFileContentLinesVector(const std::filesystem::path &filePath);
78 static std::string toCamelCase(std::string input);
79 static consteval const char *getDefaultExecutableName() {
80 #if OS_WINDOWS
81 return ".\\source.exe";
82 #else
83 return "./source";
84 #endif
85 }
86 static bool isDisabled(const TestCase &testCase);
87 static void eraseGDBHeader(std::string &gdbOutput);
88 static void eraseLinesBySubstring(std::string &irCode, const char *needle);
89
90 private:
91 // Private methods
92 static std::array<std::filesystem::path, 3> expandRefPaths(const std::filesystem::path &refPath);
93 };
94
95 } // namespace spice::testing
96