1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 | #include <wx/arrstr.h>
#include <muleunit/test.h>
#include <common/TextFile.h>
#include <common/Path.h>
using namespace muleunit;
DECLARE_SIMPLE(TextFile)<--- There is an unknown macro here somewhere. Configuration is required. If DECLARE_SIMPLE is a macro then please configure it.
const wxChar* g_filesDefault[] = {
wxT("TextFileTest_dos.txt"),
wxT("TextFileTest_unix.txt")
};
wxString ArrToStr(const wxArrayString& arr)
{
wxString str = wxT("[");
for (size_t i = 0; i < arr.Count(); ++i) {
if (str != wxT("[")) {
str << wxT(", \"") << arr[i] << wxT('"');
} else {
str << wxT('"') << arr[i] << wxT('"');
}
}
str << wxT("]");
return str;
}
wxString ArrToStr(size_t count, const wxChar* arr[])
{
return ArrToStr(wxArrayString(count, arr));
}
void CompareReadLines(size_t count, const wxChar* expected[], EReadTextFile criteria)
{
CTextFile file;
ASSERT_FALSE(file.IsOpened());
ASSERT_TRUE(file.Eof());
for (size_t j = 0; j < ArraySize(g_filesDefault); ++j) {
CONTEXT(wxString(wxT("Checking file: ")) + g_filesDefault[j]);
ASSERT_TRUE(file.Open(CPath(wxSTRINGIZE_T(SRCDIR)).JoinPaths(CPath(g_filesDefault[j])).GetRaw(), CTextFile::read));
wxArrayString lines = file.ReadLines(criteria);
ASSERT_EQUALS(ArrToStr(count, expected), ArrToStr(lines));
ASSERT_EQUALS(count, lines.GetCount());
}
ASSERT_TRUE(file.IsOpened());
ASSERT_TRUE(file.Eof());
};
TEST(TextFile, ReadLines)
{
ASSERT_TRUE(CPath::DirExists(wxSTRINGIZE_T(SRCDIR)));
{
CONTEXT(wxT("Checking default parameters"));
const wxChar* lines[] = {
wxT("abc"),
wxT("def ghi"),
wxT("xyz"),
};
CompareReadLines(ArraySize(lines), lines, txtReadDefault);
}
{
CONTEXT(wxT("Checking without criteria"));
const wxChar* lines[] = {
wxT(" # comment"),
wxT("abc"),
wxT("# foo bar"),
wxT(" "),
wxT("def ghi "),
wxT(""),
wxT("# xyz"),
wxT(" xyz"),
wxT(" "),
wxT("")
};
CompareReadLines(ArraySize(lines), lines, (EReadTextFile)0);
}
{
CONTEXT(wxT("Checking txtIgnoreEmptyLines"));
const wxChar* lines[] = {
wxT(" # comment"),
wxT("abc"),
wxT("# foo bar"),
wxT(" "),
wxT("def ghi "),
wxT("# xyz"),
wxT(" xyz"),
wxT(" "),
};
CompareReadLines(ArraySize(lines), lines, txtIgnoreEmptyLines);
}
{
CONTEXT(wxT("Checking txtIgnoreComments"));
const wxChar* lines[] = {
wxT("abc"),
wxT(" "),
wxT("def ghi "),
wxT(""),
wxT(" xyz"),
wxT(" "),
wxT("")
};
CompareReadLines(ArraySize(lines), lines, txtIgnoreComments);
}
{
CONTEXT(wxT("Checking txtStripWhitespace"));
const wxChar* lines[] = {
wxT("# comment"),
wxT("abc"),
wxT("# foo bar"),
wxT(""),
wxT("def ghi"),
wxT(""),
wxT("# xyz"),
wxT("xyz"),
wxT(""),
wxT("")
};
CompareReadLines(ArraySize(lines), lines, txtStripWhitespace);
}
}
class TextFileTest : public Test
{
public:
TextFileTest()
: Test(wxT("TextFile"), wxT("WriteLines"))
{
}
virtual void setUp()
{
const CPath path = CPath(wxT("testfile.txt"));
if (path.FileExists()) {
ASSERT_TRUE(CPath::RemoveFile(path));
}
}
virtual void tearDown()
{
setUp();
}
virtual void run()
{
const wxChar* lines[] = {
wxT(" # comment"),
wxT("abc"),
wxT("# foo bar"),
wxT(" "),
wxT("def ghi "),
wxT(""),
wxT("# xyz"),
wxT(" xyz"),
wxT(" "),
wxT("")
};
{
CONTEXT(wxT("Writing lines manually"));
CTextFile file;
ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::write));
for (size_t i = 0; i < ArraySize(lines); ++i) {
CONTEXT(wxString::Format(wxT("Writing the %i'th line."), static_cast<int>(i)));
ASSERT_TRUE(file.WriteLine(lines[i]));
}
}
{
CONTEXT(wxT("Reading manually written lines"));
CTextFile file;
ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::read));
ASSERT_FALSE(file.Eof());
for (size_t i = 0; i < ArraySize(lines); ++i) {
CONTEXT(wxString::Format(wxT("Reading the %i'th line."), static_cast<int>(i)));
ASSERT_EQUALS(lines[i], file.GetNextLine());
}
ASSERT_TRUE(file.Eof());
}
{
CONTEXT(wxT("Writing lines automatically"));
CTextFile file;
ASSERT_FALSE(file.IsOpened());
ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::write));
ASSERT_TRUE(file.WriteLines(wxArrayString(ArraySize(lines), lines)));
ASSERT_TRUE(file.IsOpened());
}
{
CONTEXT(wxT("Reading automatically written lines"));
CTextFile file;
ASSERT_FALSE(file.IsOpened());
ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::read));
ASSERT_TRUE(file.IsOpened());
ASSERT_FALSE(file.Eof());
for (size_t i = 0; i < ArraySize(lines); ++i) {
CONTEXT(wxString::Format(wxT("Reading the %i'th line."), static_cast<int>(i)));
ASSERT_EQUALS(lines[i], file.GetNextLine());
}
ASSERT_TRUE(file.Eof());
}
}
} testInstance;
|