move console sample's interactive tests to two different CppUnit testsuites: Interact...
[wxWidgets.git] / tests / interactive / input.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/interactive/input.cpp
3 // Purpose: Miscellaneous tests requiring user input
4 // Author: Francesco Montorsi (extracted from console sample)
5 // Created: 2010-06-21
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #include "testprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 // ----------------------------------------------------------------------------
25 // conditional compilation
26 // ----------------------------------------------------------------------------
27
28 #define TEST_SNGLINST
29 #define TEST_FTP
30 #define TEST_INFO_FUNCTIONS
31 #define TEST_REGEX
32 #define TEST_DATETIME
33
34 // ----------------------------------------------------------------------------
35 // test class
36 // ----------------------------------------------------------------------------
37
38 class InteractiveInputTestCase : public CppUnit::TestCase
39 {
40 public:
41 InteractiveInputTestCase() { }
42
43 private:
44 CPPUNIT_TEST_SUITE( InteractiveInputTestCase );
45 CPPUNIT_TEST( TestSingleIstance );
46 CPPUNIT_TEST( TestFtpInteractive );
47 CPPUNIT_TEST( TestDiskInfo );
48 CPPUNIT_TEST( TestRegExInteractive );
49 CPPUNIT_TEST( TestDateTimeInteractive );
50 CPPUNIT_TEST_SUITE_END();
51
52 void TestSingleIstance();
53 void TestFtpInteractive();
54 void TestDiskInfo();
55 void TestRegExInteractive();
56 void TestDateTimeInteractive();
57
58 wxDECLARE_NO_COPY_CLASS(InteractiveInputTestCase);
59 };
60
61 // ----------------------------------------------------------------------------
62 // CppUnit macros
63 // ----------------------------------------------------------------------------
64
65 //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveInputTestCase );
66 // do not run this test by default!
67
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveInputTestCase, "InteractiveInputTestCase" );
69
70 // ============================================================================
71 // implementation
72 // ============================================================================
73
74 // ----------------------------------------------------------------------------
75 // misc information functions
76 // ----------------------------------------------------------------------------
77
78 #include "wx/utils.h"
79
80 void InteractiveInputTestCase::TestDiskInfo()
81 {
82 #ifdef TEST_INFO_FUNCTIONS
83 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
84
85 for ( ;; )
86 {
87 wxChar pathname[128];
88 wxPrintf(wxT("Enter a directory name (press ENTER or type 'quit' to escape): "));
89 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
90 break;
91
92 // kill the last '\n'
93 pathname[wxStrlen(pathname) - 1] = 0;
94
95 if (pathname[0] == '\0' || wxStrcmp(pathname, "quit") == 0)
96 break;
97
98 wxLongLong total, free;
99 if ( !wxGetDiskSpace(pathname, &total, &free) )
100 {
101 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
102 }
103 else
104 {
105 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
106 (total / 1024).ToString().c_str(),
107 (free / 1024).ToString().c_str(),
108 pathname);
109 }
110
111 wxPuts("\n");
112 }
113
114 wxPuts("\n");
115 #endif // TEST_INFO_FUNCTIONS
116 }
117
118
119 // ----------------------------------------------------------------------------
120 // regular expressions
121 // ----------------------------------------------------------------------------
122
123 #include "wx/regex.h"
124
125 void InteractiveInputTestCase::TestRegExInteractive()
126 {
127 #ifdef TEST_REGEX
128 wxPuts(wxT("*** Testing RE interactively ***"));
129
130 for ( ;; )
131 {
132 wxChar pattern[128];
133 wxPrintf(wxT("Enter a pattern (press ENTER or type 'quit' to escape): "));
134 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
135 break;
136
137 // kill the last '\n'
138 pattern[wxStrlen(pattern) - 1] = 0;
139
140 if (pattern[0] == '\0' || wxStrcmp(pattern, "quit") == 0)
141 break;
142
143 wxRegEx re;
144 if ( !re.Compile(pattern) )
145 {
146 continue;
147 }
148
149 wxChar text[128];
150 for ( ;; )
151 {
152 wxPrintf(wxT("Enter text to match: "));
153 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
154 break;
155
156 // kill the last '\n'
157 text[wxStrlen(text) - 1] = 0;
158
159 if ( !re.Matches(text) )
160 {
161 wxPrintf(wxT("No match.\n"));
162 }
163 else
164 {
165 wxPrintf(wxT("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
166
167 size_t start, len;
168 for ( size_t n = 1; ; n++ )
169 {
170 if ( !re.GetMatch(&start, &len, n) )
171 {
172 break;
173 }
174
175 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
176 n, wxString(text + start, len).c_str());
177 }
178 }
179 }
180
181 wxPuts("\n");
182 }
183 #endif // TEST_REGEX
184 }
185
186
187 // ----------------------------------------------------------------------------
188 // FTP
189 // ----------------------------------------------------------------------------
190
191 #include "wx/protocol/ftp.h"
192 #include "wx/protocol/log.h"
193
194 #define FTP_ANONYMOUS
195 #ifdef FTP_ANONYMOUS
196 static const wxChar *hostname = wxT("ftp.wxwidgets.org");
197 #else
198 static const wxChar *hostname = "localhost";
199 #endif
200
201 void InteractiveInputTestCase::TestFtpInteractive()
202 {
203 #ifdef TEST_FTP
204 wxFTP ftp;
205
206 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
207
208 #ifdef FTP_ANONYMOUS
209 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
210 #else // !FTP_ANONYMOUS
211 wxChar user[256];
212 wxFgets(user, WXSIZEOF(user), stdin);
213 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
214 ftp.SetUser(user);
215
216 wxChar password[256];
217 wxPrintf(wxT("Password for %s: "), password);
218 wxFgets(password, WXSIZEOF(password), stdin);
219 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
220 ftp.SetPassword(password);
221
222 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
223 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
224
225 if ( !ftp.Connect(hostname) )
226 {
227 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname);
228
229 return;
230 }
231 else
232 {
233 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
234 hostname, ftp.Pwd().c_str());
235 }
236
237 wxChar buf[128];
238 for ( ;; )
239 {
240 wxPrintf(wxT("Enter FTP command (press ENTER or type 'quit' to escape): "));
241 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
242 break;
243
244 // kill the last '\n'
245 buf[wxStrlen(buf) - 1] = 0;
246
247 if (buf[0] == '\0' || wxStrcmp(buf, "quit") == 0)
248 break;
249
250 // special handling of LIST and NLST as they require data connection
251 wxString start(buf, 4);
252 start.MakeUpper();
253 if ( start == wxT("LIST") || start == wxT("NLST") )
254 {
255 wxString wildcard;
256 if ( wxStrlen(buf) > 4 )
257 wildcard = buf + 5;
258
259 wxArrayString files;
260 if ( !ftp.GetList(files, wildcard, start == wxT("LIST")) )
261 {
262 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start.c_str());
263 }
264 else
265 {
266 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
267 start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
268 size_t count = files.GetCount();
269 for ( size_t n = 0; n < count; n++ )
270 {
271 wxPrintf(wxT("\t%s\n"), files[n].c_str());
272 }
273 wxPuts(wxT("--- End of the file list"));
274 }
275 }
276 else // !list
277 {
278 wxChar ch = ftp.SendCommand(buf);
279 wxPrintf(wxT("Command %s"), ch ? wxT("succeeded") : wxT("failed"));
280 if ( ch )
281 {
282 wxPrintf(wxT(" (return code %c)"), ch);
283 }
284
285 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
286 }
287 }
288
289 wxPuts(wxT("\n"));
290 #endif // TEST_FTP
291 }
292
293 // ----------------------------------------------------------------------------
294 // date time
295 // ----------------------------------------------------------------------------
296
297 #include "wx/math.h"
298 #include "wx/datetime.h"
299
300 void InteractiveInputTestCase::TestDateTimeInteractive()
301 {
302 #ifdef TEST_DATETIME
303 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
304
305 wxChar buf[128];
306
307 for ( ;; )
308 {
309 wxPrintf(wxT("Enter a date (press ENTER or type 'quit' to escape): "));
310 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
311 break;
312
313 // kill the last '\n'
314 buf[wxStrlen(buf) - 1] = 0;
315
316 if ( buf[0] == '\0' || wxStrcmp(buf, "quit") == 0 )
317 break;
318
319 wxDateTime dt;
320 const wxChar *p = dt.ParseDate(buf);
321 if ( !p )
322 {
323 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf);
324
325 continue;
326 }
327 else if ( *p )
328 {
329 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p - buf);
330 }
331
332 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
333 dt.Format(wxT("%b %d, %Y")).c_str(),
334 dt.GetDayOfYear(),
335 dt.GetWeekOfMonth(wxDateTime::Monday_First),
336 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
337 dt.GetWeekOfYear(wxDateTime::Monday_First));
338 }
339
340 wxPuts("\n");
341 #endif // TEST_DATETIME
342 }
343
344
345 // ----------------------------------------------------------------------------
346 // single instance
347 // ----------------------------------------------------------------------------
348
349 #include "wx/snglinst.h"
350
351 void InteractiveInputTestCase::TestSingleIstance()
352 {
353 #ifdef TEST_SNGLINST
354 wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
355
356 wxSingleInstanceChecker checker;
357 if ( checker.Create(wxT(".wxconsole.lock")) )
358 {
359 if ( checker.IsAnotherRunning() )
360 {
361 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
362 return;
363 }
364
365 // wait some time to give time to launch another instance
366 wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
367 wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
368 wxFgetc(stdin);
369 }
370 else // failed to create
371 {
372 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
373 }
374
375 wxPuts("\n");
376 #endif // defined(TEST_SNGLINST)
377 }
378