GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 0 / 42 / 42
Functions: -% 0 / 12 / 12
Branches: -% 0 / 148 / 148

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 const auto TEST_FILE_NAME = "file-util-test-file.txt";
14 const std::filesystem::path TEST_FILE_PATH = std::filesystem::temp_directory_path() / TEST_FILE_NAME;
15 const std::string ERROR_MESSAGE = "[Error|Compiler]:\nI/O Error: Failed to open file: " + TEST_FILE_PATH.string();
16
17 TEST(FileUtilTest, WriteToAndReadFromFile) {
18 const std::string expectedFileContent = "This is some test content";
19 FileUtil::writeToFile(TEST_FILE_PATH, expectedFileContent);
20 ASSERT_TRUE(exists(TEST_FILE_PATH));
21 const std::string actualFileContent = FileUtil::getFileContent(TEST_FILE_PATH);
22 ASSERT_EQ(expectedFileContent, actualFileContent);
23 remove(TEST_FILE_PATH);
24 }
25
26 TEST(FileUtilTest, ReadFromFileNonExisting) {
27 ASSERT_TRUE(!exists(TEST_FILE_PATH));
28 try {
29 FileUtil::getFileContent(TEST_FILE_PATH);
30 FAIL();
31 } catch (CompilerError &error) {
32 ASSERT_EQ(ERROR_MESSAGE, error.what());
33 }
34 }
35
36 TEST(FileUtilTest, GetLineCount) {
37 const std::string expectedFileContent = "Line 1\nLine2\nLine3\n\nLine 5";
38 FileUtil::writeToFile(TEST_FILE_PATH, expectedFileContent);
39 ASSERT_TRUE(exists(TEST_FILE_PATH));
40 const size_t lineCount = FileUtil::getLineCount(TEST_FILE_PATH);
41 ASSERT_EQ(5, lineCount);
42 remove(TEST_FILE_PATH);
43 }
44
45 TEST(FileUtilTest, GetLineCountNonExisting) {
46 ASSERT_TRUE(!exists(TEST_FILE_PATH));
47 try {
48 FileUtil::getLineCount(TEST_FILE_PATH);
49 FAIL();
50 } catch (CompilerError &error) {
51 ASSERT_EQ(ERROR_MESSAGE, error.what());
52 }
53 }
54
55 } // namespace spice::testing
56
57 // LCOV_EXCL_STOP
58