GCC Code Coverage Report


Directory: ../
File: test/util/TestUtil.h
Date: 2025-05-08 21:00:15
Exec Total Coverage
Lines: 0 0 100.0%
Functions: 0 0 -%
Branches: 0 0 -%

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 // GCOV_EXCL_START
4
5 #pragma once
6
7 #include <filesystem>
8 #include <functional>
9 #include <string>
10 #include <vector>
11
12 #include <gtest/gtest.h>
13
14 namespace spice::testing {
15
16 const char *const PATH_TEST_FILES = "./test-files/";
17 static constexpr unsigned int EXPECTED_NUMBER_OF_TESTS = 250;
18 const char *const GDB_READING_SYMBOLS_MESSAGE = "Reading symbols from ";
19 const char *const GDB_INFERIOR_MESSAGE = "[Inferior";
20
21 const char *const INPUT_NAME_LINKER_FLAGS = "linker-flags.txt";
22 const char *const INPUT_NAME_CLI_FLAGS = "cli-flags.txt";
23
24 const char *const REF_NAME_SOURCE = "source.spice";
25 const char *const REF_NAME_PARSE_TREE = "parse-tree.dot";
26 const char *const REF_NAME_SYNTAX_TREE = "syntax-tree.dot";
27 const char *const REF_NAME_DEP_GRAPH = "dependency-graph.dot";
28 const char *const REF_NAME_SYMBOL_TABLE = "symbol-table.json";
29 const char *const REF_NAME_TYPE_REGISTRY = "type-registry.out";
30 const char *const REF_NAME_CACHE_STATS = "cache-stats.out";
31 const char *const REF_NAME_IR = "ir-code.ll";
32 const char *const REF_NAME_ASM = "assembly.asm";
33 static constexpr const char *const REF_NAME_OPT_IR[5] = {
34 "ir-code-O1.ll", "ir-code-O2.ll", "ir-code-O3.ll", "ir-code-Os.ll", "ir-code-Oz.ll",
35 };
36 const char *const REF_NAME_EXECUTION_OUTPUT = "cout.out";
37 const char *const REF_NAME_GDB_OUTPUT = "debug.out";
38 const char *const REF_NAME_ERROR_OUTPUT = "exception.out";
39 const char *const REF_NAME_WARNING_OUTPUT = "warning.out";
40 const char *const REF_NAME_EXIT_CODE = "exit-code.out";
41
42 const char *const CTL_SKIP_DISABLED = "disabled";
43 const char *const CTL_SKIP_GH = "skip-gh-actions";
44 const char *const CTL_DEBUG_INFO = "with-debug-info";
45 const char *const CTL_RUN_BUILTIN_TESTS = "run-builtin-tests";
46 const char *const CTL_DEBUG_SCRIPT = "debug.gdb";
47 const char *const CTL_LTO = "with-lto";
48
49 struct TestCase {
50 const std::string testSuite;
51 const std::string testName;
52 const std::filesystem::path testPath;
53 };
54
55 // Typedefs
56 typedef const std::function<std::string()> &GetOutputFct;
57 typedef const std::function<void(std::string &expectedOutput, std::string &actualOutput)> &ModifyOutputFct;
58
59 class TestUtil {
60 public:
61 // Public structs
62 struct NameResolver {
63 template <class T> std::string operator()(const ::testing::TestParamInfo<T> &info) const {
64 T testCase = static_cast<T>(info.param);
65 return TestUtil::toCamelCase(testCase.testSuite) + "_" + TestUtil::toCamelCase(testCase.testName);
66 }
67 };
68
69 // Public static methods
70 static std::vector<TestCase> collectTestCases(const char *suiteName, bool useSubDirs);
71 static bool checkRefMatch(
72 const std::filesystem::path &originalRefPath, GetOutputFct getActualOutput,
73 ModifyOutputFct modifyOutputFct = [](std::string &, std::string &) {});
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
97 // GCOV_EXCL_STOP
98