| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include <gtest/gtest.h> | ||
| 4 | |||
| 5 | #include <exception/CompilerError.h> | ||
| 6 | #include <util/FileUtil.h> | ||
| 7 | |||
| 8 | namespace spice::testing { | ||
| 9 | |||
| 10 | using namespace spice::compiler; | ||
| 11 | |||
| 12 | const auto TEST_FILE_NAME = "file-util-test-file.txt"; | ||
| 13 | const std::filesystem::path TEST_FILE_PATH = std::filesystem::temp_directory_path() / TEST_FILE_NAME; | ||
| 14 | const std::string ERROR_MESSAGE = "[Error|Compiler]:\nI/O Error: Failed to open file: " + TEST_FILE_PATH.string(); | ||
| 15 | |||
| 16 | 4 | TEST(FileUtilTest, WriteToAndReadFromFile) { | |
| 17 |
1/2✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 53 not taken.
|
1 | const std::string expectedFileContent = "This is some test content"; |
| 18 |
1/2✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 80 not taken.
|
1 | FileUtil::writeToFile(TEST_FILE_PATH, expectedFileContent); |
| 19 |
3/14✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 56 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 20 taken 1 time.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 65 not taken.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 61 not taken.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 59 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 57 not taken.
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 25 not taken.
|
1 | ASSERT_TRUE(exists(TEST_FILE_PATH)); |
| 20 |
1/2✓ Branch 24 → 26 taken 1 time.
✗ Branch 24 → 80 not taken.
|
1 | const std::string actualFileContent = FileUtil::getFileContent(TEST_FILE_PATH); |
| 21 |
3/12✓ Branch 26 → 27 taken 1 time.
✗ Branch 26 → 77 not taken.
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 36 taken 1 time.
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 74 not taken.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 71 not taken.
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 69 not taken.
✓ Branch 38 → 39 taken 1 time.
✗ Branch 38 → 41 not taken.
|
1 | ASSERT_EQ(expectedFileContent, actualFileContent); |
| 22 |
1/2✓ Branch 40 → 42 taken 1 time.
✗ Branch 40 → 78 not taken.
|
1 | remove(TEST_FILE_PATH); |
| 23 |
1/2✓ Branch 48 → 49 taken 1 time.
✗ Branch 48 → 51 not taken.
|
1 | } |
| 24 | |||
| 25 | 4 | TEST(FileUtilTest, ReadFromFileNonExisting) { | |
| 26 |
3/14✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 29 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 15 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 38 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 34 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 32 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 30 not taken.
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 20 not taken.
|
1 | ASSERT_TRUE(!exists(TEST_FILE_PATH)); |
| 27 | try { | ||
| 28 |
1/2✗ Branch 19 → 21 not taken.
✓ Branch 19 → 42 taken 1 time.
|
1 | FileUtil::getFileContent(TEST_FILE_PATH); |
| 29 | ✗ | FAIL(); | |
| 30 |
1/2✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 1 time.
|
1 | } catch (CompilerError &error) { |
| 31 |
3/12✓ Branch 53 → 54 taken 1 time.
✗ Branch 53 → 73 not taken.
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 63 taken 1 time.
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 79 not taken.
✗ Branch 58 → 59 not taken.
✗ Branch 58 → 76 not taken.
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 74 not taken.
✓ Branch 65 → 66 taken 1 time.
✗ Branch 65 → 68 not taken.
|
1 | ASSERT_EQ(ERROR_MESSAGE, error.what()); |
| 32 |
1/2✗ Branch 70 → 28 not taken.
✓ Branch 70 → 71 taken 1 time.
|
1 | } |
| 33 | } | ||
| 34 | |||
| 35 | 4 | TEST(FileUtilTest, GetLineCount) { | |
| 36 |
1/2✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 49 not taken.
|
1 | const std::string expectedFileContent = "Line 1\nLine2\nLine3\n\nLine 5"; |
| 37 |
1/2✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 75 not taken.
|
1 | FileUtil::writeToFile(TEST_FILE_PATH, expectedFileContent); |
| 38 |
3/14✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 52 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 20 taken 1 time.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 61 not taken.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 57 not taken.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 55 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 53 not taken.
✓ Branch 22 → 23 taken 1 time.
✗ Branch 22 → 25 not taken.
|
1 | ASSERT_TRUE(exists(TEST_FILE_PATH)); |
| 39 |
1/2✓ Branch 24 → 26 taken 1 time.
✗ Branch 24 → 75 not taken.
|
1 | const size_t lineCount = FileUtil::getLineCount(TEST_FILE_PATH); |
| 40 |
3/12✓ Branch 26 → 27 taken 1 time.
✗ Branch 26 → 65 not taken.
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 36 taken 1 time.
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 71 not taken.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 68 not taken.
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 66 not taken.
✓ Branch 38 → 39 taken 1 time.
✗ Branch 38 → 41 not taken.
|
1 | ASSERT_EQ(5, lineCount); |
| 41 |
1/2✓ Branch 40 → 42 taken 1 time.
✗ Branch 40 → 75 not taken.
|
1 | remove(TEST_FILE_PATH); |
| 42 |
1/2✓ Branch 44 → 45 taken 1 time.
✗ Branch 44 → 47 not taken.
|
1 | } |
| 43 | |||
| 44 | 4 | TEST(FileUtilTest, GetLineCountNonExisting) { | |
| 45 |
3/14✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 28 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 15 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 37 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 33 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 31 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 29 not taken.
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 20 not taken.
|
1 | ASSERT_TRUE(!exists(TEST_FILE_PATH)); |
| 46 | try { | ||
| 47 |
1/2✗ Branch 19 → 21 not taken.
✓ Branch 19 → 47 taken 1 time.
|
1 | FileUtil::getLineCount(TEST_FILE_PATH); |
| 48 | ✗ | FAIL(); | |
| 49 |
1/2✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 1 time.
|
1 | } catch (CompilerError &error) { |
| 50 |
3/12✓ Branch 51 → 52 taken 1 time.
✗ Branch 51 → 71 not taken.
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 61 taken 1 time.
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 77 not taken.
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 74 not taken.
✗ Branch 57 → 58 not taken.
✗ Branch 57 → 72 not taken.
✓ Branch 63 → 64 taken 1 time.
✗ Branch 63 → 66 not taken.
|
1 | ASSERT_EQ(ERROR_MESSAGE, error.what()); |
| 51 |
1/2✗ Branch 68 → 27 not taken.
✓ Branch 68 → 69 taken 1 time.
|
1 | } |
| 52 | } | ||
| 53 | |||
| 54 | } // namespace spice::testing | ||
| 55 |