]>
git.saurik.com Git - wxWidgets.git/blob - tests/textfile/textfiletest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/textfile/textfile.cpp
3 // Purpose: wxTextFile unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2006 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "wx/textfile.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class TextFileTestCase
: public CppUnit::TestCase
34 TextFileTestCase() { }
36 virtual void tearDown() { unlink(GetTestFileName()); }
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();
53 // return the name of the test file we use
54 static const char *GetTestFileName() { return "textfiletest.txt"; }
56 // create the test file with the given contents
57 static void CreateTestFile(const char *contents
);
60 DECLARE_NO_COPY_CLASS(TextFileTestCase
)
63 // register in the unnamed registry so that these tests are run by default
64 CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase
);
66 // also include in it's own registry so that these tests can be run alone
67 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase
, "TextFileTestCase" );
69 void TextFileTestCase::CreateTestFile(const char *contents
)
71 FILE *f
= fopen(GetTestFileName(), "wb");
74 CPPUNIT_ASSERT( fputs(contents
, f
) >= 0 );
75 CPPUNIT_ASSERT( fclose(f
) == 0 );
78 void TextFileTestCase::ReadEmpty()
83 CPPUNIT_ASSERT( f
.Open(GetTestFileName()) );
85 CPPUNIT_ASSERT_EQUAL( 0u, f
.GetLineCount() );
88 void TextFileTestCase::ReadDOS()
90 CreateTestFile("foo\r\nbar\r\nbaz");
93 CPPUNIT_ASSERT( f
.Open(GetTestFileName()) );
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("bar"), f
.GetLine(1) );
99 CPPUNIT_ASSERT_EQUAL( wxString("baz"), f
.GetLastLine() );
102 void TextFileTestCase::ReadUnix()
104 CreateTestFile("foo\nbar\nbaz");
107 CPPUNIT_ASSERT( f
.Open(GetTestFileName()) );
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("bar"), f
.GetLine(1) );
113 CPPUNIT_ASSERT_EQUAL( wxString("baz"), f
.GetLastLine() );
116 void TextFileTestCase::ReadMac()
118 CreateTestFile("foo\rbar\rbaz");
121 CPPUNIT_ASSERT( f
.Open(GetTestFileName()) );
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("bar"), f
.GetLine(1) );
127 CPPUNIT_ASSERT_EQUAL( wxString("baz"), f
.GetLastLine() );
130 void TextFileTestCase::ReadMixed()
132 CreateTestFile("foo\rbar\r\nbaz\n");
135 CPPUNIT_ASSERT( f
.Open(GetTestFileName()) );
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("foo"), f
.GetFirstLine() );
142 CPPUNIT_ASSERT_EQUAL( wxString("bar"), f
.GetLine(1) );
143 CPPUNIT_ASSERT_EQUAL( wxString("baz"), f
.GetLastLine() );
146 #endif // wxUSE_TEXTFILE