Upstream tarball 20080823
[amule.git] / unittests / tests / TextFileTest.cpp
blob07ace8f0c6675d346546d4111dbad1aea8bef7bb
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)
66 CONTEXT(wxT("Checking default parameters"));
68 const wxChar* lines[] = {
69 wxT("abc"),
70 wxT("def ghi"),
71 wxT("xyz"),
74 CompareReadLines(ArraySize(lines), lines, txtReadDefault);
78 CONTEXT(wxT("Checking without criteria"));
80 const wxChar* lines[] = {
81 wxT(" # comment"),
82 wxT("abc"),
83 wxT("# foo bar"),
84 wxT(" "),
85 wxT("def ghi "),
86 wxT(""),
87 wxT("# xyz"),
88 wxT(" xyz"),
89 wxT(" "),
90 wxT("")
93 CompareReadLines(ArraySize(lines), lines, (EReadTextFile)0);
97 CONTEXT(wxT("Checking txtIgnoreEmptyLines"));
99 const wxChar* lines[] = {
100 wxT(" # comment"),
101 wxT("abc"),
102 wxT("# foo bar"),
103 wxT(" "),
104 wxT("def ghi "),
105 wxT("# xyz"),
106 wxT(" xyz"),
107 wxT(" "),
110 CompareReadLines(ArraySize(lines), lines, txtIgnoreEmptyLines);
114 CONTEXT(wxT("Checking txtIgnoreComments"));
116 const wxChar* lines[] = {
117 wxT("abc"),
118 wxT(" "),
119 wxT("def ghi "),
120 wxT(""),
121 wxT(" xyz"),
122 wxT(" "),
123 wxT("")
126 CompareReadLines(ArraySize(lines), lines, txtIgnoreComments);
130 CONTEXT(wxT("Checking txtStripWhitespace"));
132 const wxChar* lines[] = {
133 wxT("# comment"),
134 wxT("abc"),
135 wxT("# foo bar"),
136 wxT(""),
137 wxT("def ghi"),
138 wxT(""),
139 wxT("# xyz"),
140 wxT("xyz"),
141 wxT(""),
142 wxT("")
145 CompareReadLines(ArraySize(lines), lines, txtStripWhitespace);
150 class TextFileTest : public Test
152 public:
153 TextFileTest()
154 : Test(wxT("TextFile"), wxT("WriteLines"))
158 virtual void setUp()
160 const CPath path = CPath(wxT("testfile.txt"));
161 if (path.FileExists()) {
162 ASSERT_TRUE(CPath::RemoveFile(path));
167 virtual void tearDown()
169 setUp();
172 virtual void run()
174 const wxChar* lines[] = {
175 wxT(" # comment"),
176 wxT("abc"),
177 wxT("# foo bar"),
178 wxT(" "),
179 wxT("def ghi "),
180 wxT(""),
181 wxT("# xyz"),
182 wxT(" xyz"),
183 wxT(" "),
184 wxT("")
188 CONTEXT(wxT("Writing lines manually"));
190 CTextFile file;
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"));
203 CTextFile file;
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"));
218 CTextFile file;
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"));
228 CTextFile file;
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());
243 } testInstance;