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('"');
25 str
<< wxT('"') << arr
[i
] << wxT('"');
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
)
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
)
66 CONTEXT(wxT("Checking default parameters"));
68 const wxChar
* lines
[] = {
74 CompareReadLines(ArraySize(lines
), lines
, txtReadDefault
);
78 CONTEXT(wxT("Checking without criteria"));
80 const wxChar
* lines
[] = {
93 CompareReadLines(ArraySize(lines
), lines
, (EReadTextFile
)0);
97 CONTEXT(wxT("Checking txtIgnoreEmptyLines"));
99 const wxChar
* lines
[] = {
110 CompareReadLines(ArraySize(lines
), lines
, txtIgnoreEmptyLines
);
114 CONTEXT(wxT("Checking txtIgnoreComments"));
116 const wxChar
* lines
[] = {
126 CompareReadLines(ArraySize(lines
), lines
, txtIgnoreComments
);
130 CONTEXT(wxT("Checking txtStripWhitespace"));
132 const wxChar
* lines
[] = {
145 CompareReadLines(ArraySize(lines
), lines
, txtStripWhitespace
);
150 class TextFileTest
: public Test
154 : Test(wxT("TextFile"), wxT("WriteLines"))
160 const CPath path
= CPath(wxT("testfile.txt"));
161 if (path
.FileExists()) {
162 ASSERT_TRUE(CPath::RemoveFile(path
));
167 virtual void tearDown()
174 const wxChar
* lines
[] = {
188 CONTEXT(wxT("Writing lines manually"));
191 ASSERT_TRUE(file
.Open(wxT("testfile.txt"), CTextFile::write
));
193 for (size_t i
= 0; i
< ArraySize(lines
); ++i
) {
194 CONTEXT(wxString::Format(wxT("Writing the %i'th line."), i
));
196 ASSERT_TRUE(file
.WriteLine(lines
[i
]));
201 CONTEXT(wxT("Reading manually written lines"));
204 ASSERT_TRUE(file
.Open(wxT("testfile.txt"), CTextFile::read
));
205 ASSERT_FALSE(file
.Eof());
207 for (size_t i
= 0; i
< ArraySize(lines
); ++i
) {
208 CONTEXT(wxString::Format(wxT("Reading the %i'th line."), i
));
210 ASSERT_EQUALS(lines
[i
], file
.GetNextLine());
212 ASSERT_TRUE(file
.Eof());
216 CONTEXT(wxT("Writing lines automatically"));
219 ASSERT_FALSE(file
.IsOpened());
220 ASSERT_TRUE(file
.Open(wxT("testfile.txt"), CTextFile::write
));
221 ASSERT_TRUE(file
.WriteLines(wxArrayString(ArraySize(lines
), lines
)));
222 ASSERT_TRUE(file
.IsOpened());
226 CONTEXT(wxT("Reading automatically written lines"));
229 ASSERT_FALSE(file
.IsOpened());
230 ASSERT_TRUE(file
.Open(wxT("testfile.txt"), CTextFile::read
));
231 ASSERT_TRUE(file
.IsOpened());
232 ASSERT_FALSE(file
.Eof());
234 for (size_t i
= 0; i
< ArraySize(lines
); ++i
) {
235 CONTEXT(wxString::Format(wxT("Reading the %i'th line."), i
));
237 ASSERT_EQUALS(lines
[i
], file
.GetNextLine());
240 ASSERT_TRUE(file
.Eof());