GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 0 / 52 / 52
Functions: -% 0 / 14 / 14
Branches: -% 0 / 164 / 164

test/unittest/UnitFileUtil.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 <exception/CompilerError.h>
7 #include <util/FileUtil.h>
8
9 namespace spice::testing {
10
11 using namespace spice::compiler;
12
13 class FileUtilTest : public ::testing::Test {
14 protected:
15 std::filesystem::path testFilePath;
16 std::string errorMessage;
17
18 void SetUp() override {
19 const auto *testInfo = ::testing::UnitTest::GetInstance()->current_test_info();
20 const std::string fileName = std::string("spice-file-util-") + testInfo->name() + ".txt";
21 testFilePath = std::filesystem::temp_directory_path() / fileName;
22 errorMessage = "[Error|Compiler]:\nI/O Error: Failed to open file: " + testFilePath.string();
23 std::error_code ec;
24 std::filesystem::remove(testFilePath, ec);
25 }
26
27 void TearDown() override {
28 std::error_code ec;
29 std::filesystem::remove(testFilePath, ec);
30 }
31 };
32
33 TEST_F(FileUtilTest, WriteToAndReadFromFile) {
34 const std::string expectedFileContent = "This is some test content";
35 FileUtil::writeToFile(testFilePath, expectedFileContent);
36 ASSERT_TRUE(exists(testFilePath));
37 const std::string actualFileContent = FileUtil::getFileContent(testFilePath);
38 ASSERT_EQ(expectedFileContent, actualFileContent);
39 }
40
41 TEST_F(FileUtilTest, ReadFromFileNonExisting) {
42 ASSERT_TRUE(!exists(testFilePath));
43 try {
44 FileUtil::getFileContent(testFilePath);
45 FAIL();
46 } catch (CompilerError &error) {
47 ASSERT_EQ(errorMessage, error.what());
48 }
49 }
50
51 TEST_F(FileUtilTest, GetLineCount) {
52 const std::string expectedFileContent = "Line 1\nLine2\nLine3\n\nLine 5";
53 FileUtil::writeToFile(testFilePath, expectedFileContent);
54 ASSERT_TRUE(exists(testFilePath));
55 const size_t lineCount = FileUtil::getLineCount(testFilePath);
56 ASSERT_EQ(5, lineCount);
57 }
58
59 TEST_F(FileUtilTest, GetLineCountNonExisting) {
60 ASSERT_TRUE(!exists(testFilePath));
61 try {
62 FileUtil::getLineCount(testFilePath);
63 FAIL();
64 } catch (CompilerError &error) {
65 ASSERT_EQ(errorMessage, error.what());
66 }
67 }
68
69 } // namespace spice::testing
70
71 // LCOV_EXCL_STOP
72