1 #include <wx/arrstr.h>
2 
3 #include <muleunit/test.h>
4 #include <common/TextFile.h>
5 #include <common/Path.h>
6 
7 using namespace muleunit;
8 
9 DECLARE_SIMPLE(TextFile)
10 
11 const wxChar* g_filesDefault[] = {
12 	wxT("TextFileTest_dos.txt"),
13 	wxT("TextFileTest_unix.txt")
14 };
15 
16 
ArrToStr(const wxArrayString & arr)17 wxString ArrToStr(const wxArrayString& arr)
18 {
19 	wxString str = wxT("[");
20 
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('"');
26 		}
27 	}
28 
29 	str << wxT("]");
30 
31 	return str;
32 }
33 
34 
ArrToStr(size_t count,const wxChar * arr[])35 wxString ArrToStr(size_t count, const wxChar* arr[])
36 {
37 	return ArrToStr(wxArrayString(count, arr));
38 }
39 
40 
41 
CompareReadLines(size_t count,const wxChar * expected[],EReadTextFile criteria)42 void CompareReadLines(size_t count, const wxChar* expected[], EReadTextFile criteria)
43 {
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]);
49 
50 		ASSERT_TRUE(file.Open(CPath(wxSTRINGIZE_T(SRCDIR)).JoinPaths(CPath(g_filesDefault[j])).GetRaw(), CTextFile::read));
51 
52 		wxArrayString lines = file.ReadLines(criteria);
53 
54 		ASSERT_EQUALS(ArrToStr(count, expected), ArrToStr(lines));
55 		ASSERT_EQUALS(count, lines.GetCount());
56 	}
57 	ASSERT_TRUE(file.IsOpened());
58 	ASSERT_TRUE(file.Eof());
59 };
60 
61 
62 
TEST(TextFile,ReadLines)63 TEST(TextFile, ReadLines)
64 {
65 	ASSERT_TRUE(CPath::DirExists(wxSTRINGIZE_T(SRCDIR)));
66 
67 	{
68 		CONTEXT(wxT("Checking default parameters"));
69 
70 		const wxChar* lines[] = {
71 			wxT("abc"),
72 			wxT("def ghi"),
73 			wxT("xyz"),
74 		};
75 
76 		CompareReadLines(ArraySize(lines), lines, txtReadDefault);
77 	}
78 
79 	{
80 		CONTEXT(wxT("Checking without criteria"));
81 
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("")
93 		};
94 
95 		CompareReadLines(ArraySize(lines), lines, (EReadTextFile)0);
96 	}
97 
98 	{
99 		CONTEXT(wxT("Checking txtIgnoreEmptyLines"));
100 
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(" "),
110 		};
111 
112 		CompareReadLines(ArraySize(lines), lines, txtIgnoreEmptyLines);
113 	}
114 
115 	{
116 		CONTEXT(wxT("Checking txtIgnoreComments"));
117 
118 		const wxChar* lines[] = {
119 			wxT("abc"),
120 			wxT(" "),
121 			wxT("def ghi "),
122 			wxT(""),
123 			wxT(" xyz"),
124 			wxT(" "),
125 			wxT("")
126 		};
127 
128 		CompareReadLines(ArraySize(lines), lines, txtIgnoreComments);
129 	}
130 
131 	{
132 		CONTEXT(wxT("Checking txtStripWhitespace"));
133 
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("")
145 		};
146 
147 		CompareReadLines(ArraySize(lines), lines, txtStripWhitespace);
148 	}
149 }
150 
151 
152 class TextFileTest : public Test
153 {
154 public:
TextFileTest()155 	TextFileTest()
156 		: Test(wxT("TextFile"), wxT("WriteLines"))
157 	{
158 	}
159 
setUp()160 	virtual void setUp()
161 	{
162 		const CPath path = CPath(wxT("testfile.txt"));
163 		if (path.FileExists()) {
164 			ASSERT_TRUE(CPath::RemoveFile(path));
165 		}
166 
167 	}
168 
tearDown()169 	virtual void tearDown()
170 	{
171 		setUp();
172 	}
173 
run()174 	virtual void run()
175 	{
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("")
187 		};
188 
189 		{
190 			CONTEXT(wxT("Writing lines manually"));
191 
192 			CTextFile file;
193 			ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::write));
194 
195 			for (size_t i = 0; i < ArraySize(lines); ++i) {
196 				CONTEXT(wxString::Format(wxT("Writing the %i'th line."), static_cast<int>(i)));
197 
198 				ASSERT_TRUE(file.WriteLine(lines[i]));
199 			}
200 		}
201 
202 		{
203 			CONTEXT(wxT("Reading manually written lines"));
204 
205 			CTextFile file;
206 			ASSERT_TRUE(file.Open(wxT("testfile.txt"), CTextFile::read));
207 			ASSERT_FALSE(file.Eof());
208 
209 			for (size_t i = 0; i < ArraySize(lines); ++i) {
210 				CONTEXT(wxString::Format(wxT("Reading the %i'th line."), static_cast<int>(i)));
211 
212 				ASSERT_EQUALS(lines[i], file.GetNextLine());
213 			}
214 			ASSERT_TRUE(file.Eof());
215 		}
216 
217 		{
218 			CONTEXT(wxT("Writing lines automatically"));
219 
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());
225 		}
226 
227 		{
228 			CONTEXT(wxT("Reading automatically written lines"));
229 
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());
235 
236 			for (size_t i = 0; i < ArraySize(lines); ++i) {
237 				CONTEXT(wxString::Format(wxT("Reading the %i'th line."), static_cast<int>(i)));
238 
239 				ASSERT_EQUALS(lines[i], file.GetNextLine());
240 			}
241 
242 			ASSERT_TRUE(file.Eof());
243 		}
244 	}
245 } testInstance;
246