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: textfiletest.cpp 56161 2008-10-08 16:06:36Z VZ $
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:
TextFileTestCase()34     TextFileTestCase() { }
35 
tearDown()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 #if wxUSE_UNICODE
46         CPPUNIT_TEST( ReadUTF8 );
47         CPPUNIT_TEST( ReadUTF16 );
48 #endif // wxUSE_UNICODE
49     CPPUNIT_TEST_SUITE_END();
50 
51     void ReadEmpty();
52     void ReadDOS();
53     void ReadUnix();
54     void ReadMac();
55     void ReadMixed();
56 #if wxUSE_UNICODE
57     void ReadUTF8();
58     void ReadUTF16();
59 #endif // wxUSE_UNICODE
60 
61     // return the name of the test file we use
GetTestFileName()62     static const char *GetTestFileName() { return "textfiletest.txt"; }
63 
64     // create the test file with the given contents
CreateTestFile(const char * contents)65     static void CreateTestFile(const char *contents)
66     {
67         CreateTestFile(strlen(contents), contents);
68     }
69 
70     // create the test file with the given contents (version must be used if
71     // contents contains NULs)
72     static void CreateTestFile(size_t len, const char *contents);
73 
74 
75     DECLARE_NO_COPY_CLASS(TextFileTestCase)
76 };
77 
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase );
80 
81 // also include in it's own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" );
83 
CreateTestFile(size_t len,const char * contents)84 void TextFileTestCase::CreateTestFile(size_t len, const char *contents)
85 {
86     FILE *f = fopen(GetTestFileName(), "wb");
87     CPPUNIT_ASSERT( f );
88 
89     CPPUNIT_ASSERT( fwrite(contents, 1, len, f) >= 0 );
90     CPPUNIT_ASSERT( fclose(f) == 0 );
91 }
92 
ReadEmpty()93 void TextFileTestCase::ReadEmpty()
94 {
95     CreateTestFile("");
96 
97     wxTextFile f;
98     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
99 
100     CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() );
101 }
102 
ReadDOS()103 void TextFileTestCase::ReadDOS()
104 {
105     CreateTestFile("foo\r\nbar\r\nbaz");
106 
107     wxTextFile f;
108     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
109 
110     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
111     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
112     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
113     CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
114     CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
115 }
116 
ReadUnix()117 void TextFileTestCase::ReadUnix()
118 {
119     CreateTestFile("foo\nbar\nbaz");
120 
121     wxTextFile f;
122     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
123 
124     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
125     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
126     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
127     CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
128     CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
129 }
130 
ReadMac()131 void TextFileTestCase::ReadMac()
132 {
133     CreateTestFile("foo\rbar\rbaz");
134 
135     wxTextFile f;
136     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
137 
138     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
139     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
140     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
141     CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
142     CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
143 }
144 
ReadMixed()145 void TextFileTestCase::ReadMixed()
146 {
147     CreateTestFile("foo\rbar\r\nbaz\n");
148 
149     wxTextFile f;
150     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
151 
152     CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
153     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
154     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(1) );
155     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(2) );
156     CPPUNIT_ASSERT_EQUAL( wxString(_T("foo")), f.GetFirstLine() );
157     CPPUNIT_ASSERT_EQUAL( wxString(_T("bar")), f.GetLine(1) );
158     CPPUNIT_ASSERT_EQUAL( wxString(_T("baz")), f.GetLastLine() );
159 }
160 
161 #if wxUSE_UNICODE
162 
ReadUTF8()163 void TextFileTestCase::ReadUTF8()
164 {
165     CreateTestFile("\xd0\x9f\n"
166                    "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
167 
168     wxTextFile f;
169     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8) );
170 
171     CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
172     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
173     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
174 #ifdef wxHAVE_U_ESCAPE
175     CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
176     CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
177                           f.GetLastLine() );
178 #endif // wxHAVE_U_ESCAPE
179 }
180 
ReadUTF16()181 void TextFileTestCase::ReadUTF16()
182 {
183     CreateTestFile(16,
184                    "\x1f\x04\x0d\x00\x0a\x00"
185                    "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
186 
187     wxTextFile f;
188     wxMBConvUTF16LE conv;
189     CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), conv) );
190 
191     CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
192     CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
193     CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
194 
195 #ifdef wxHAVE_U_ESCAPE
196     CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
197     CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
198                           f.GetLastLine() );
199 #endif // wxHAVE_U_ESCAPE
200 }
201 
202 #endif // wxUSE_UNICODE
203 
204 #endif // wxUSE_TEXTFILE
205 
206