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 / 172 / 172

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