Still fixing the unittests...
[amule.git] / unittests / tests / TextFileTest.cpp
blobb849f6bcec4de09cabb69f481e49650e1c226089
1 #include <wx/arrstr.h>
3 #include <muleunit/test.h>
4 #include <common/TextFile.h>
5 #include <common/Path.h>
7 using namespace muleunit;
9 DECLARE_SIMPLE(TextFile)
11 const wxChar* g_filesDefault[] = {
12 wxT("TextFileTest_dos.txt"),
13 wxT("TextFileTest_unix.txt")
17 wxString ArrToStr(const wxArrayString& arr)
19 wxString str = wxT("[");
21 for (size_t i = 0; i < arr.Count(); ++i) {
22 if (str != wxT("[")) {
23 str << wxT(", \"") << arr[i] << wxT('"');
24 } else {
25 str << wxT('"') << arr[i] << wxT('"');
29 str << wxT("]");
31 return str;
35 wxString ArrToStr(size_t count, const wxChar* arr[])
37 return ArrToStr(wxArrayString(count, arr));
42 void CompareReadLines(size_t count, const wxChar* expected[], EReadTextFile criteria)
44 CTextFile file;
45 ASSERT_FALSE(file.IsOpened());
46 ASSERT_TRUE(file.Eof());
47 for (size_t j = 0; j < ArraySize(g_filesDefault); ++j) {
48 CONTEXT(wxString(wxT("Checking file: ")) + g_filesDefault[j]);
50 ASSERT_TRUE(file.Open(CPath(wxSTRINGIZE_T(SRCDIR)).JoinPaths(CPath(g_filesDefault[j])).GetRaw(), CTextFile::read));
52 wxArrayString lines = file.ReadLines(criteria);
54 ASSERT_EQUALS(ArrToStr(count, expected), ArrToStr(lines));
55 ASSERT_EQUALS(count, lines.GetCount());
57 ASSERT_TRUE(file.IsOpened());
58 ASSERT_TRUE(file.Eof());
63 TEST(TextFile, ReadLines)
65 ASSERT_TRUE(CPath::DirExists(wxSTRINGIZE_T(SRCDIR)));
68 CONTEXT(wxT("Checking default parameters"));
70 const wxChar* lines[] = {
71 wxT("abc"),
72 wxT("def ghi"),
73 wxT("xyz"),
76 CompareReadLines(ArraySize(lines), lines, txtReadDefault);
80 CONTEXT(wxT("Checking without criteria"));
82 const wxChar* lines[] = {
83 wxT(" # comment"),
84 wxT("abc"),
85 wxT("# foo bar"),
86 wxT(" "),
87 wxT("def ghi "),
88 wxT(""),
89 wxT("# xyz"),
90 wxT(" xyz"),
91 wxT(" "),
92 wxT("")
95 CompareReadLines(ArraySize(lines), lines, (EReadTextFile)0);
99 CONTEXT(wxT("Checking txtIgnoreEmptyLines"));
101 const wxChar* lines[] = {
102 wxT(" # comment"),
103 wxT("abc"),
104 wxT("# foo bar"),
105 wxT(" "),
106 wxT("def ghi "),
107 wxT("# xyz"),
108 wxT(" xyz"),
109 wxT(" "),
112 CompareReadLines(ArraySize(lines), lines, txtIgnoreEmptyLines);
116 CONTEXT(wxT("Checking txtIgnoreComments"));
118 const wxChar* lines[] = {
119 wxT("abc"),
120 wxT(" "),
121 wxT("def ghi "),
122 wxT(""),
123 wxT(" xyz"),
124 wxT(" "),
125 wxT("")
128 CompareReadLines(ArraySize(lines), lines, txtIgnoreComments);
132 CONTEXT(wxT("Checking txtStripWhitespace"));
134 const wxChar* lines[] = {
135 wxT("# comment"),
136 wxT("abc"),
137 wxT("# foo bar"),
138 wxT(""),
139 wxT("def ghi"),
140 wxT(""),
141 wxT("# xyz"),
142 wxT("xyz"),
143 wxT(""),
144 wxT("")
147 CompareReadLines(ArraySize(lines), lines, txtStripWhitespace);
152 class TextFileTest : public Test
154 public:
155 TextFileTest()
156 : Test(wxT("TextFile"), wxT("WriteLines"))
160 virtual void setUp()
162 const CPath path = CPath(wxT("testfile.txt"));
163 if (path.FileExists()) {
164 ASSERT_TRUE(CPath::RemoveFile(path));
169 virtual void tearDown()
171 setUp();
174 virtual void run()
176 const wxChar* lines[] = {
177 wxT(" # comment"),
178 wxT("abc"),
179 wxT("# foo bar"),
180 wxT(" "),
181 wxT("def ghi "),
182 wxT(""),
183 wxT("# xyz"),
184 wxT(" xyz"),
185 wxT(" "),
186 wxT("")
190 CONTEXT(wxT("Writing lines manually"));
192 CTextFile file;
193 ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::write));
195 for (size_t i = 0; i < ArraySize(lines); ++i) {
196 CONTEXT(wxString::Format(wxT("Writing the %i'th line."), static_cast<int>(i)));
198 ASSERT_TRUE(file.WriteLine(lines[i]));
203 CONTEXT(wxT("Reading manually written lines"));
205 CTextFile file;
206 ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::read));
207 ASSERT_FALSE(file.Eof());
209 for (size_t i = 0; i < ArraySize(lines); ++i) {
210 CONTEXT(wxString::Format(wxT("Reading the %i'th line."), static_cast<int>(i)));
212 ASSERT_EQUALS(lines[i], file.GetNextLine());
214 ASSERT_TRUE(file.Eof());
218 CONTEXT(wxT("Writing lines automatically"));
220 CTextFile file;
221 ASSERT_FALSE(file.IsOpened());
222 ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::write));
223 ASSERT_TRUE(file.WriteLines(wxArrayString(ArraySize(lines), lines)));
224 ASSERT_TRUE(file.IsOpened());
228 CONTEXT(wxT("Reading automatically written lines"));
230 CTextFile file;
231 ASSERT_FALSE(file.IsOpened());
232 ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::read));
233 ASSERT_TRUE(file.IsOpened());
234 ASSERT_FALSE(file.Eof());
236 for (size_t i = 0; i < ArraySize(lines); ++i) {
237 CONTEXT(wxString::Format(wxT("Reading the %i'th line."), static_cast<int>(i)));
239 ASSERT_EQUALS(lines[i], file.GetNextLine());
242 ASSERT_TRUE(file.Eof());
245 } testInstance;