55 bool result = (f1.
eof() && f2.
eof());
75 FileReplacer paramReplacer(
"test.txt",
"find",
"replace");
92 replacer2 = replacer1;
109 inputFile <<
"This is a basic test. find this find.\n";
112 std::ofstream expectedOutputFile(
"basic_input.txt.expected");
113 expectedOutputFile <<
"This is a basic test. replace this replace.\n";
114 expectedOutputFile.
close();
116 FileReplacer replacer(
"basic_input.txt",
"find",
"replace");
119 test.
expectTrue(
compareFiles(
"basic_input.txt.replace",
"basic_input.txt.expected") == 0,
"Basic Replacement");
121 system(
"rm -f basic_input.txt basic_input.txt.replace basic_input.txt.expected");
134 inputFile <<
"This is a test.\n";
137 FileReplacer replacer(
"empty_s1_input.txt",
"",
"replace");
140 test.
expectTrue(
false,
"Empty s1: No exception thrown");
142 test.
expectTrue(
true,
"Empty s1: Exception thrown");
145 system(
"rm -f empty_s1_input.txt empty_s1_input.txt.replace");
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)
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &&__is, basic_string< _CharT, _Traits, _Alloc > &__str)
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.
void expectEq(const T &val1, const T &val2, const std::string &message)
Asserts that two values are equal.
void expectTrue(const T &condition, const std::string &message)
Asserts that a condition is true.
void runTest(const std::string &testName, void(*testFunction)(Test &))
Runs a single test case.