test/unittest/UnitDriver.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | // LCOV_EXCL_START | ||
| 3 | |||
| 4 | #include <gtest/gtest.h> | ||
| 5 | |||
| 6 | #include <driver/Driver.h> | ||
| 7 | #include <exception/CliError.h> | ||
| 8 | |||
| 9 | namespace spice::testing { | ||
| 10 | |||
| 11 | using namespace spice::compiler; | ||
| 12 | |||
| 13 | − | TEST(DriverTest, BuildSubcommandMinimal) { | |
| 14 | − | const char *argv[] = {"spice", "build", "../../media/test-project/test.spice"}; | |
| 15 | static constexpr int argc = std::size(argv); | ||
| 16 | − | CliOptions cliOptions; | |
| 17 | − | Driver driver(cliOptions, true); | |
| 18 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 19 | − | driver.enrich(); | |
| 20 | |||
| 21 | − | ASSERT_TRUE(driver.shouldCompile); | |
| 22 | − | ASSERT_FALSE(driver.shouldInstall); | |
| 23 | − | ASSERT_FALSE(driver.shouldUninstall); | |
| 24 | − | ASSERT_FALSE(driver.shouldExecute); | |
| 25 | − | ASSERT_FALSE(cliOptions.execute); | |
| 26 | − | ASSERT_EQ("../../media/test-project/test.spice", cliOptions.mainSourceFile.relative_path().generic_string()); | |
| 27 | − | ASSERT_EQ(OptLevel::O0, cliOptions.optLevel); | |
| 28 | − | ASSERT_EQ(BuildMode::DEBUG, cliOptions.buildMode); | |
| 29 | − | ASSERT_FALSE(cliOptions.generateTestMain); | |
| 30 | − | ASSERT_FALSE(cliOptions.testMode); | |
| 31 | − | ASSERT_FALSE(cliOptions.noEntryFct); | |
| 32 | − | } | |
| 33 | |||
| 34 | − | TEST(DriverTest, BuildSubcommandComplex) { | |
| 35 | − | const char *argv[] = { | |
| 36 | "spice", | ||
| 37 | "b", | ||
| 38 | "-d", | ||
| 39 | "-ir", | ||
| 40 | "-g", | ||
| 41 | "-Os", | ||
| 42 | "-m", | ||
| 43 | "release", | ||
| 44 | "-lto", | ||
| 45 | "--sanitizer=address", | ||
| 46 | "../../media/test-project/test.spice", | ||
| 47 | }; | ||
| 48 | static constexpr int argc = std::size(argv); | ||
| 49 | − | CliOptions cliOptions; | |
| 50 | − | Driver driver(cliOptions, true); | |
| 51 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 52 | − | driver.enrich(); | |
| 53 | |||
| 54 | − | ASSERT_TRUE(driver.shouldCompile); | |
| 55 | − | ASSERT_FALSE(driver.shouldInstall); | |
| 56 | − | ASSERT_FALSE(driver.shouldUninstall); | |
| 57 | − | ASSERT_FALSE(driver.shouldExecute); | |
| 58 | − | ASSERT_FALSE(cliOptions.execute); | |
| 59 | − | ASSERT_EQ("../../media/test-project/test.spice", cliOptions.mainSourceFile.relative_path().generic_string()); | |
| 60 | − | ASSERT_EQ(OptLevel::Os, cliOptions.optLevel); // -Os | |
| 61 | − | ASSERT_EQ(BuildMode::RELEASE, cliOptions.buildMode); // -m release | |
| 62 | − | ASSERT_FALSE(cliOptions.generateTestMain); | |
| 63 | − | ASSERT_FALSE(cliOptions.testMode); | |
| 64 | − | ASSERT_FALSE(cliOptions.noEntryFct); | |
| 65 | − | ASSERT_TRUE(cliOptions.instrumentation.generateDebugInfo); // -g | |
| 66 | − | ASSERT_EQ(Sanitizer::ADDRESS, cliOptions.instrumentation.sanitizer); // --sanitizer=address | |
| 67 | − | ASSERT_TRUE(cliOptions.useLTO); // -lto | |
| 68 | − | ASSERT_TRUE(cliOptions.printDebugOutput); // -d | |
| 69 | − | ASSERT_TRUE(cliOptions.dump.dumpIR); // -ir | |
| 70 | − | ASSERT_TRUE(cliOptions.useLifetimeMarkers); // implicitly due to enabled address sanitizer | |
| 71 | − | } | |
| 72 | |||
| 73 | − | TEST(DriverTest, RunSubcommandMinimal) { | |
| 74 | − | const char *argv[] = {"spice", "run", "../../media/test-project/test.spice"}; | |
| 75 | static constexpr int argc = std::size(argv); | ||
| 76 | − | CliOptions cliOptions; | |
| 77 | − | Driver driver(cliOptions, true); | |
| 78 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 79 | − | driver.enrich(); | |
| 80 | |||
| 81 | − | ASSERT_TRUE(driver.shouldCompile); | |
| 82 | − | ASSERT_FALSE(driver.shouldInstall); | |
| 83 | − | ASSERT_FALSE(driver.shouldUninstall); | |
| 84 | − | ASSERT_TRUE(driver.shouldExecute); | |
| 85 | − | ASSERT_TRUE(cliOptions.execute); | |
| 86 | − | ASSERT_EQ("../../media/test-project/test.spice", cliOptions.mainSourceFile.relative_path().generic_string()); | |
| 87 | − | ASSERT_EQ(OptLevel::O0, cliOptions.optLevel); | |
| 88 | − | ASSERT_FALSE(cliOptions.generateTestMain); | |
| 89 | − | ASSERT_FALSE(cliOptions.testMode); | |
| 90 | − | ASSERT_FALSE(cliOptions.noEntryFct); | |
| 91 | − | } | |
| 92 | |||
| 93 | − | TEST(DriverTest, RunSubcommandComplex) { | |
| 94 | − | const char *argv[] = {"spice", "r", "-O2", "-j", "8", "-ast", "../../media/test-project/test.spice"}; | |
| 95 | static constexpr int argc = std::size(argv); | ||
| 96 | − | CliOptions cliOptions; | |
| 97 | − | Driver driver(cliOptions, true); | |
| 98 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 99 | − | driver.enrich(); | |
| 100 | |||
| 101 | − | ASSERT_TRUE(driver.shouldCompile); | |
| 102 | − | ASSERT_FALSE(driver.shouldInstall); | |
| 103 | − | ASSERT_FALSE(driver.shouldUninstall); | |
| 104 | − | ASSERT_TRUE(driver.shouldExecute); | |
| 105 | − | ASSERT_TRUE(cliOptions.execute); | |
| 106 | − | ASSERT_EQ("../../media/test-project/test.spice", cliOptions.mainSourceFile.relative_path().generic_string()); | |
| 107 | − | ASSERT_EQ(OptLevel::O2, cliOptions.optLevel); // -O2 | |
| 108 | − | ASSERT_FALSE(cliOptions.generateTestMain); | |
| 109 | − | ASSERT_FALSE(cliOptions.testMode); | |
| 110 | − | ASSERT_FALSE(cliOptions.noEntryFct); | |
| 111 | − | ASSERT_EQ(8, cliOptions.compileJobCount); // -j 8 | |
| 112 | − | ASSERT_TRUE(cliOptions.dump.dumpAST); // -ast | |
| 113 | − | } | |
| 114 | |||
| 115 | − | TEST(DriverTest, TestSubcommandMinimal) { | |
| 116 | − | const char *argv[] = {"spice", "test", "../../media/test-project/test.spice"}; | |
| 117 | static constexpr int argc = std::size(argv); | ||
| 118 | − | CliOptions cliOptions; | |
| 119 | − | Driver driver(cliOptions, true); | |
| 120 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 121 | − | driver.enrich(); | |
| 122 | |||
| 123 | − | ASSERT_TRUE(driver.shouldCompile); | |
| 124 | − | ASSERT_FALSE(driver.shouldInstall); | |
| 125 | − | ASSERT_FALSE(driver.shouldUninstall); | |
| 126 | − | ASSERT_TRUE(driver.shouldExecute); | |
| 127 | − | ASSERT_TRUE(cliOptions.execute); | |
| 128 | − | ASSERT_EQ("../../media/test-project/test.spice", cliOptions.mainSourceFile.relative_path().generic_string()); | |
| 129 | − | ASSERT_EQ(OptLevel::O0, cliOptions.optLevel); | |
| 130 | − | ASSERT_EQ(BuildMode::TEST, cliOptions.buildMode); // -m test | |
| 131 | − | ASSERT_TRUE(cliOptions.generateTestMain); | |
| 132 | − | ASSERT_FALSE(cliOptions.testMode); | |
| 133 | − | ASSERT_TRUE(cliOptions.noEntryFct); | |
| 134 | − | } | |
| 135 | |||
| 136 | − | TEST(DriverTest, TestSubcommandComplex) { | |
| 137 | − | const char *argv[] = {"spice", "t", "-s", "-cst", "--sanitizer=thread", "../../media/test-project/test.spice"}; | |
| 138 | static constexpr int argc = std::size(argv); | ||
| 139 | − | CliOptions cliOptions; | |
| 140 | − | Driver driver(cliOptions, true); | |
| 141 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 142 | − | driver.enrich(); | |
| 143 | |||
| 144 | − | ASSERT_TRUE(driver.shouldCompile); | |
| 145 | − | ASSERT_FALSE(driver.shouldInstall); | |
| 146 | − | ASSERT_FALSE(driver.shouldUninstall); | |
| 147 | − | ASSERT_TRUE(driver.shouldExecute); | |
| 148 | − | ASSERT_TRUE(cliOptions.execute); | |
| 149 | − | ASSERT_EQ("../../media/test-project/test.spice", cliOptions.mainSourceFile.relative_path().generic_string()); | |
| 150 | − | ASSERT_EQ(OptLevel::O0, cliOptions.optLevel); | |
| 151 | − | ASSERT_TRUE(cliOptions.generateTestMain); | |
| 152 | − | ASSERT_TRUE(cliOptions.noEntryFct); | |
| 153 | − | ASSERT_TRUE(cliOptions.dump.dumpCST); // -cst | |
| 154 | − | ASSERT_TRUE(cliOptions.dump.dumpAssembly); // -s | |
| 155 | − | ASSERT_EQ(Sanitizer::THREAD, cliOptions.instrumentation.sanitizer); // --sanitizer=thread | |
| 156 | − | } | |
| 157 | |||
| 158 | − | TEST(DriverTest, InstallSubcommandMinimal) { | |
| 159 | − | const char *argv[] = {"spice", "install", "../../media/test-project/test.spice"}; | |
| 160 | static constexpr int argc = std::size(argv); | ||
| 161 | − | CliOptions cliOptions; | |
| 162 | − | Driver driver(cliOptions, true); | |
| 163 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 164 | − | driver.enrich(); | |
| 165 | |||
| 166 | − | ASSERT_TRUE(driver.shouldCompile); | |
| 167 | − | ASSERT_TRUE(driver.shouldInstall); | |
| 168 | − | ASSERT_FALSE(driver.shouldUninstall); | |
| 169 | − | ASSERT_FALSE(driver.shouldExecute); | |
| 170 | − | ASSERT_FALSE(cliOptions.execute); | |
| 171 | − | ASSERT_EQ("../../media/test-project/test.spice", cliOptions.mainSourceFile.relative_path().generic_string()); | |
| 172 | − | ASSERT_EQ(OptLevel::O0, cliOptions.optLevel); | |
| 173 | − | ASSERT_FALSE(cliOptions.generateTestMain); | |
| 174 | − | ASSERT_FALSE(cliOptions.testMode); | |
| 175 | − | ASSERT_FALSE(cliOptions.noEntryFct); | |
| 176 | − | } | |
| 177 | |||
| 178 | − | TEST(DriverTest, UninstallSubcommandMinimal) { | |
| 179 | − | const char *argv[] = {"spice", "uninstall", "../../media/test-project/test.spice"}; | |
| 180 | static constexpr int argc = std::size(argv); | ||
| 181 | − | CliOptions cliOptions; | |
| 182 | − | Driver driver(cliOptions, true); | |
| 183 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 184 | − | driver.enrich(); | |
| 185 | |||
| 186 | − | ASSERT_FALSE(driver.shouldCompile); | |
| 187 | − | ASSERT_FALSE(driver.shouldInstall); | |
| 188 | − | ASSERT_TRUE(driver.shouldUninstall); | |
| 189 | − | ASSERT_FALSE(driver.shouldExecute); | |
| 190 | − | ASSERT_FALSE(cliOptions.execute); | |
| 191 | − | ASSERT_EQ("../../media/test-project/test.spice", cliOptions.mainSourceFile.relative_path().generic_string()); | |
| 192 | − | ASSERT_EQ(OptLevel::O0, cliOptions.optLevel); | |
| 193 | − | ASSERT_FALSE(cliOptions.generateTestMain); | |
| 194 | − | ASSERT_FALSE(cliOptions.testMode); | |
| 195 | − | ASSERT_FALSE(cliOptions.noEntryFct); | |
| 196 | − | } | |
| 197 | |||
| 198 | − | TEST(DriverTest, MemorySanitizerOnlyLinux) { | |
| 199 | − | const char *argv[] = {"spice", "build", "--sanitizer=memory", "../../media/test-project/test.spice"}; | |
| 200 | static constexpr int argc = std::size(argv); | ||
| 201 | − | CliOptions cliOptions; | |
| 202 | − | Driver driver(cliOptions, true); | |
| 203 | |||
| 204 | − | ASSERT_EQ(EXIT_SUCCESS, driver.parse(argc, argv)); | |
| 205 | #if OS_LINUX | ||
| 206 | − | driver.enrich(); | |
| 207 | − | ASSERT_EQ(Sanitizer::MEMORY, cliOptions.instrumentation.sanitizer); | |
| 208 | − | ASSERT_TRUE(cliOptions.useLifetimeMarkers); // implicitly due to enabled address sanitizer | |
| 209 | #else | ||
| 210 | try { | ||
| 211 | driver.enrich(); | ||
| 212 | FAIL(); | ||
| 213 | } catch (CliError &error) { | ||
| 214 | auto errorMsg = "[Error|CLI] Feature is not supported for this target: Memory sanitizer is only supported for Linux targets"; | ||
| 215 | ASSERT_STREQ(errorMsg, error.what()); | ||
| 216 | } | ||
| 217 | #endif | ||
| 218 | − | } | |
| 219 | |||
| 220 | using DriverInvalidEnumTestParam = std::pair<const char *, const char *>; | ||
| 221 | class DriverTest : public ::testing::TestWithParam<DriverInvalidEnumTestParam> {}; | ||
| 222 | |||
| 223 | − | TEST_P(DriverTest, LengthGreaterThanZero) { | |
| 224 | − | const auto &[arg, errorMessage] = GetParam(); | |
| 225 | − | const char *argv[] = {"spice", "build", arg, "../../media/test-project/test.spice"}; | |
| 226 | static constexpr int argc = std::size(argv); | ||
| 227 | − | CliOptions cliOptions; | |
| 228 | − | Driver driver(cliOptions, true); | |
| 229 | try { | ||
| 230 | − | driver.parse(argc, argv); | |
| 231 | − | FAIL(); | |
| 232 | − | } catch (CliError &error) { | |
| 233 | − | ASSERT_STREQ(errorMessage, error.what()); | |
| 234 | − | } | |
| 235 | − | } | |
| 236 | |||
| 237 | const auto INVALID_ENUM_TEST_VALUES = ::testing::Values( | ||
| 238 | DriverInvalidEnumTestParam{ | ||
| 239 | "--build-mode=unknown", | ||
| 240 | "[Error|CLI] Invalid build mode: unknown", | ||
| 241 | }, | ||
| 242 | DriverInvalidEnumTestParam{ | ||
| 243 | "--sanitizer=unknown", | ||
| 244 | "[Error|CLI] Invalid sanitizer: unknown", | ||
| 245 | }); | ||
| 246 | − | INSTANTIATE_TEST_SUITE_P(DriverTest, DriverTest, INVALID_ENUM_TEST_VALUES); | |
| 247 | |||
| 248 | } // namespace spice::testing | ||
| 249 | |||
| 250 | // LCOV_EXCL_STOP | ||
| 251 |