CPP01 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
FileReplacerTest.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* FileReplacerTest.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/04/18 20:34:16 by kamitsui #+# #+# */
9/* Updated: 2025/04/23 00:44:42 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
21#include "FileReplacer.hpp" // Your FileReplacer class
22#include "Test.hpp" // Our custom Test class
23#include <cstdlib>
24#include <fstream>
25#include <sstream>
26
37bool compareFiles(const std::string &file1, const std::string &file2) {
38 std::ifstream f1(file1.c_str());
39 std::ifstream f2(file2.c_str());
40
41 if (!f1.is_open() || !f2.is_open()) {
42 std::cerr << "Error: Could not open files for comparison." << std::endl;
43 return false;
44 }
45
46 std::string line1, line2;
47 while (std::getline(f1, line1) && std::getline(f2, line2)) {
48 if (line1 != line2) {
49 f1.close();
50 f2.close();
51 return false;
52 }
53 }
54
55 bool result = (f1.eof() && f2.eof());
56 f1.close();
57 f2.close();
58 return result;
59}
60
70 FileReplacer defaultReplacer;
71 test.expectTrue(defaultReplacer.getFileName().empty(), "Default constructor filename");
72 test.expectTrue(defaultReplacer.getS1().empty(), "Default constructor s1");
73 test.expectTrue(defaultReplacer.getS2().empty(), "Default constructor s2");
74
75 FileReplacer paramReplacer("test.txt", "find", "replace");
76 test.expectEq(paramReplacer.getFileName(), std::string("test.txt"), "Param constructor filename");
77 test.expectEq(paramReplacer.getS1(), std::string("find"), "Param constructor s1");
78 test.expectEq(paramReplacer.getS2(), std::string("replace"), "Param constructor s2");
79}
80
90 FileReplacer replacer1("file1.txt", "old", "new");
91 FileReplacer replacer2;
92 replacer2 = replacer1;
93 test.expectEq(replacer2.getFileName(), std::string("file1.txt"), "Assignment filename");
94 test.expectEq(replacer2.getS1(), std::string("old"), "Assignment s1");
95 test.expectEq(replacer2.getS2(), std::string("new"), "Assignment s2");
96}
97
108 std::ofstream inputFile("basic_input.txt");
109 inputFile << "This is a basic test. find this find.\n";
110 inputFile.close();
111
112 std::ofstream expectedOutputFile("basic_input.txt.expected");
113 expectedOutputFile << "This is a basic test. replace this replace.\n";
114 expectedOutputFile.close();
115
116 FileReplacer replacer("basic_input.txt", "find", "replace");
117 replacer.processFile();
118
119 test.expectTrue(compareFiles("basic_input.txt.replace", "basic_input.txt.expected") == 0, "Basic Replacement");
120
121 system("rm -f basic_input.txt basic_input.txt.replace basic_input.txt.expected");
122}
123
133 std::ofstream inputFile("empty_s1_input.txt");
134 inputFile << "This is a test.\n";
135 inputFile.close();
136
137 FileReplacer replacer("empty_s1_input.txt", "", "replace");
138 try {
139 replacer.processFile();
140 test.expectTrue(false, "Empty s1: No exception thrown"); // Force fail if no exception
141 } catch (const std::runtime_error &e) {
142 test.expectTrue(true, "Empty s1: Exception thrown"); // Force pass if exception
143 }
144
145 system("rm -f empty_s1_input.txt empty_s1_input.txt.replace");
146}
147
155int main() {
156 Test test;
157
158 test.runTest("Constructors Test", constructorsTest);
159 test.runTest("Assignment Operator Test", assignmentOperatorTest);
160 test.runTest("processFile() Basic Test", processFileBasicTest);
161 test.runTest("processFile() Empty s1 Test", processFileEmptyS1Test);
162
163 return 0;
164}
bool compareFiles(const std::string &file1, const std::string &file2)
Helper function to compare the contents of two files.
void constructorsTest(Test &test)
Test suite for the constructors of the FileReplacer class.
void assignmentOperatorTest(Test &test)
Test suite for the assignment operator of the FileReplacer class.
void processFileEmptyS1Test(Test &test)
Test suite for the processFile() method when the string to find (s1) is empty.
void processFileBasicTest(Test &test)
Test suite for the basic functionality of the processFile() method.
int main()
The main function for running the unit tests.
Declares the FileReplacer class for replacing strings in a file.
Declares a custom Test class for unit testing.
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ostream cerr
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &&__is, basic_string< _CharT, _Traits, _Alloc > &__str)
bool eof() const
bool empty() const noexcept
const _CharT * c_str() const noexcept
A class that handles replacing occurrences of a string within a file.
void processFile()
Processes the input file and performs the string replacement.
const std::string & getS1() const
Gets the string to find (s1).
const std::string & getFileName() const
Gets the filename.
const std::string & getS2() const
Gets the string to replace with (s2).
A custom class for performing unit tests and reporting results.
Definition Test.hpp:36
void expectEq(const T &val1, const T &val2, const std::string &message)
Asserts that two values are equal.
Definition Test.hpp:82
void expectTrue(const T &condition, const std::string &message)
Asserts that a condition is true.
Definition Test.hpp:67
void runTest(const std::string &testName, void(*testFunction)(Test &))
Runs a single test case.
Definition Test.cpp:40