1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/interactive/input.cpp
3 // Purpose: Miscellaneous tests requiring user input
4 // Author: Francesco Montorsi (extracted from console sample)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
25 #include "wx/wxcrt.h" // for wxPuts
26 #include "wx/wxcrtvararg.h" // for wxPrintf
28 // ----------------------------------------------------------------------------
29 // conditional compilation
30 // ----------------------------------------------------------------------------
34 #define TEST_INFO_FUNCTIONS
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 class InteractiveInputTestCase
: public CppUnit::TestCase
47 InteractiveInputTestCase() { }
50 CPPUNIT_TEST_SUITE( InteractiveInputTestCase
);
51 CPPUNIT_TEST( TestSingleIstance
);
52 CPPUNIT_TEST( TestFtpInteractive
);
53 CPPUNIT_TEST( TestDiskInfo
);
54 CPPUNIT_TEST( TestRegExInteractive
);
55 CPPUNIT_TEST( TestDateTimeInteractive
);
56 CPPUNIT_TEST_SUITE_END();
58 void TestSingleIstance();
59 void TestFtpInteractive();
61 void TestRegExInteractive();
62 void TestDateTimeInteractive();
64 wxDECLARE_NO_COPY_CLASS(InteractiveInputTestCase
);
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveInputTestCase );
72 // do not run this test by default!
74 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveInputTestCase
, "InteractiveInputTestCase" );
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
81 // misc information functions
82 // ----------------------------------------------------------------------------
86 void InteractiveInputTestCase::TestDiskInfo()
88 #ifdef TEST_INFO_FUNCTIONS
89 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
94 wxPrintf(wxT("Enter a directory name (press ENTER or type 'quit' to escape): "));
95 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
99 pathname
[wxStrlen(pathname
) - 1] = 0;
101 if (pathname
[0] == '\0' || wxStrcmp(pathname
, "quit") == 0)
104 wxLongLong total
, free
;
105 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
107 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
111 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
112 (total
/ 1024).ToString().c_str(),
113 (free
/ 1024).ToString().c_str(),
121 #endif // TEST_INFO_FUNCTIONS
125 // ----------------------------------------------------------------------------
126 // regular expressions
127 // ----------------------------------------------------------------------------
129 #include "wx/regex.h"
131 void InteractiveInputTestCase::TestRegExInteractive()
134 wxPuts(wxT("*** Testing RE interactively ***"));
139 wxPrintf(wxT("Enter a pattern (press ENTER or type 'quit' to escape): "));
140 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
143 // kill the last '\n'
144 pattern
[wxStrlen(pattern
) - 1] = 0;
146 if (pattern
[0] == '\0' || wxStrcmp(pattern
, "quit") == 0)
150 if ( !re
.Compile(pattern
) )
158 wxPrintf(wxT("Enter text to match: "));
159 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
162 // kill the last '\n'
163 text
[wxStrlen(text
) - 1] = 0;
165 if ( !re
.Matches(text
) )
167 wxPrintf(wxT("No match.\n"));
171 wxPrintf(wxT("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
174 for ( size_t n
= 1; ; n
++ )
176 if ( !re
.GetMatch(&start
, &len
, n
) )
181 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
182 n
, wxString(text
+ start
, len
).c_str());
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 #include "wx/protocol/ftp.h"
198 #include "wx/protocol/log.h"
200 #define FTP_ANONYMOUS
202 static const wxChar
*hostname
= wxT("ftp.wxwidgets.org");
204 static const wxChar
*hostname
= "localhost";
207 void InteractiveInputTestCase::TestFtpInteractive()
212 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
215 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
216 #else // !FTP_ANONYMOUS
218 wxFgets(user
, WXSIZEOF(user
), stdin
);
219 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
222 wxChar password
[256];
223 wxPrintf(wxT("Password for %s: "), password
);
224 wxFgets(password
, WXSIZEOF(password
), stdin
);
225 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
226 ftp
.SetPassword(password
);
228 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
229 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
231 if ( !ftp
.Connect(hostname
) )
233 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
239 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
240 hostname
, ftp
.Pwd().c_str());
246 wxPrintf(wxT("Enter FTP command (press ENTER or type 'quit' to escape): "));
247 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
250 // kill the last '\n'
251 buf
[wxStrlen(buf
) - 1] = 0;
253 if (buf
[0] == '\0' || wxStrcmp(buf
, "quit") == 0)
256 // special handling of LIST and NLST as they require data connection
257 wxString
start(buf
, 4);
259 if ( start
== wxT("LIST") || start
== wxT("NLST") )
262 if ( wxStrlen(buf
) > 4 )
266 if ( !ftp
.GetList(files
, wildcard
, start
== wxT("LIST")) )
268 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start
.c_str());
272 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
273 start
.c_str(), wildcard
.c_str(), ftp
.Pwd().c_str());
274 size_t count
= files
.GetCount();
275 for ( size_t n
= 0; n
< count
; n
++ )
277 wxPrintf(wxT("\t%s\n"), files
[n
].c_str());
279 wxPuts(wxT("--- End of the file list"));
284 wxChar ch
= ftp
.SendCommand(buf
);
285 wxPrintf(wxT("Command %s"), ch
? wxT("succeeded") : wxT("failed"));
288 wxPrintf(wxT(" (return code %c)"), ch
);
291 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp
.GetLastResult().c_str());
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
304 #include "wx/datetime.h"
306 void InteractiveInputTestCase::TestDateTimeInteractive()
309 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
315 wxPrintf(wxT("Enter a date (press ENTER or type 'quit' to escape): "));
316 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
319 // kill the last '\n'
320 buf
[wxStrlen(buf
) - 1] = 0;
322 if ( buf
[0] == '\0' || wxStrcmp(buf
, "quit") == 0 )
326 const wxChar
*p
= dt
.ParseDate(buf
);
329 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf
);
335 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p
- buf
);
338 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
339 dt
.Format(wxT("%b %d, %Y")).c_str(),
341 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
342 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
343 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
347 #endif // TEST_DATETIME
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
355 #include "wx/snglinst.h"
357 void InteractiveInputTestCase::TestSingleIstance()
360 wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
362 wxSingleInstanceChecker checker
;
363 if ( checker
.Create(wxT(".wxconsole.lock")) )
365 if ( checker
.IsAnotherRunning() )
367 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
371 // wait some time to give time to launch another instance
372 wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
373 wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
376 else // failed to create
378 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
382 #endif // defined(TEST_SNGLINST)