Unicode compilation fixes
[wxWidgets.git] / tests / textfile / textfiletest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/textfile/textfile.cpp
3 // Purpose: wxTextFile unit test
4 // Author: Vadim Zeitlin
5 // Created: 2006-03-31
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_TEXTFILE
21
22 #ifndef WX_PRECOMP
23 #endif // WX_PRECOMP
24
25 #include "wx/textfile.h"
26
27 // ----------------------------------------------------------------------------
28 // test class
29 // ----------------------------------------------------------------------------
30
31 class TextFileTestCase : public CppUnit::TestCase
32 {
33 public:
34 TextFileTestCase() { }
35
36 virtual void tearDown() { unlink(GetTestFileName()); }
37
38 private:
39 CPPUNIT_TEST_SUITE( TextFileTestCase );
40 CPPUNIT_TEST( ReadEmpty );
41 CPPUNIT_TEST( ReadDOS );
42 CPPUNIT_TEST( ReadUnix );
43 CPPUNIT_TEST( ReadMac );
44 CPPUNIT_TEST( ReadMixed );
45 CPPUNIT_TEST_SUITE_END();
46
47 void ReadEmpty();
48 void ReadDOS();
49 void ReadUnix();
50 void ReadMac();
51 void ReadMixed();
52
53 // return the name of the test file we use
54 static const char *GetTestFileName() { return "textfiletest.txt"; }
55
56 // create the test file with the given contents
57 static void CreateTestFile(const char *contents);
58
59
60 DECLARE_NO_COPY_CLASS(TextFileTestCase)
61 };
62
63 // register in the unnamed registry so that these tests are run by default
64 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase );
65
66 // also include in it's own registry so that these tests can be run alone
67 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" );
68
69 void TextFileTestCase::CreateTestFile(const char *contents)
70 {
71 FILE *f = fopen(GetTestFileName(), "wb");
72 CPPUNIT_ASSERT( f );
73
74 CPPUNIT_ASSERT( fputs(contents, f) >= 0 );
75 CPPUNIT_ASSERT( fclose(f) == 0 );
76 }
77
78 void TextFileTestCase::ReadEmpty()
79 {
80 CreateTestFile("");
81
82 wxTextFile f;
83 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
84
85 CPPUNIT_ASSERT_EQUAL( 0u, f.GetLineCount() );
86 }
87
88 void TextFileTestCase::ReadDOS()
89 {
90 CreateTestFile("foo\r\nbar\r\nbaz");
91
92 wxTextFile f;
93 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
94
95 CPPUNIT_ASSERT_EQUAL( 3u, f.GetLineCount() );
96 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
97 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
98 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
99 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
100 }
101
102 void TextFileTestCase::ReadUnix()
103 {
104 CreateTestFile("foo\nbar\nbaz");
105
106 wxTextFile f;
107 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
108
109 CPPUNIT_ASSERT_EQUAL( 3u, f.GetLineCount() );
110 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
111 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
112 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
113 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
114 }
115
116 void TextFileTestCase::ReadMac()
117 {
118 CreateTestFile("foo\rbar\rbaz");
119
120 wxTextFile f;
121 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
122
123 CPPUNIT_ASSERT_EQUAL( 3u, f.GetLineCount() );
124 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
125 CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
126 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
127 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
128 }
129
130 void TextFileTestCase::ReadMixed()
131 {
132 CreateTestFile("foo\rbar\r\nbaz\n");
133
134 wxTextFile f;
135 CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
136
137 CPPUNIT_ASSERT_EQUAL( 3u, f.GetLineCount() );
138 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
139 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(1) );
140 CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(2) );
141 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo")), f.GetFirstLine() );
142 CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
143 CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
144 }
145
146 #endif // wxUSE_TEXTFILE
147