xref: /haiku/src/tests/apps/haikudepot/StringUtilsTest.cpp (revision 9f3bdf3d039430b5172c424def20ce5d9f7367d4)
1 /*
2  * Copyright 2023-2024, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "StringUtilsTest.h"
6 
7 #include <String.h>
8 
9 #include <cppunit/TestCaller.h>
10 #include <cppunit/TestSuite.h>
11 
12 #include "StringUtils.h"
13 
14 
15 StringUtilsTest::StringUtilsTest()
16 {
17 }
18 
19 
20 StringUtilsTest::~StringUtilsTest()
21 {
22 }
23 
24 
25 void
26 StringUtilsTest::TestStartInSituTrimSpaceAndControl()
27 {
28 	BString string = "\t\n Tonic Water";
29 
30 // ----------------------
31 	StringUtils::InSituTrimSpaceAndControl(string);
32 // ----------------------
33 
34 	const BString expected = "Tonic Water";
35 	CPPUNIT_ASSERT_EQUAL(expected, string);
36 }
37 
38 
39 void
40 StringUtilsTest::TestEndInSituTrimSpaceAndControl()
41 {
42 	BString string = "Tonic Water  \x05\t";
43 
44 // ----------------------
45 	StringUtils::InSituTrimSpaceAndControl(string);
46 // ----------------------
47 
48 	const BString expected = "Tonic Water";
49 	CPPUNIT_ASSERT_EQUAL(expected, string);
50 }
51 
52 
53 void
54 StringUtilsTest::TestStartAndEndInSituTrimSpaceAndControl()
55 {
56 	BString string = "\x01Tonic Water\x02";
57 
58 // ----------------------
59 	StringUtils::InSituTrimSpaceAndControl(string);
60 // ----------------------
61 
62 	const BString expected = "Tonic Water";
63 	CPPUNIT_ASSERT_EQUAL(expected, string);
64 }
65 
66 
67 void
68 StringUtilsTest::TestNoTrimInSituTrimSpaceAndControl()
69 {
70 	BString string = "Tonic Water";
71 
72 // ----------------------
73 	StringUtils::InSituTrimSpaceAndControl(string);
74 // ----------------------
75 
76 	const BString expected = "Tonic Water";
77 	CPPUNIT_ASSERT_EQUAL(expected, string);
78 }
79 
80 
81 void
82 StringUtilsTest::TestInSituStripSpaceAndControl()
83 {
84 	BString string = "\x01 To\tnic Wa\nter  ";
85 
86 // ----------------------
87 	StringUtils::InSituStripSpaceAndControl(string);
88 // ----------------------
89 
90 	const BString expected = "TonicWater";
91 		// note intervening space also removed
92 	CPPUNIT_ASSERT_EQUAL(expected, string);
93 }
94 
95 
96 void
97 StringUtilsTest::TestNoInSituStripSpaceAndControl()
98 {
99 	BString string = "Tonic Water";
100 
101 // ----------------------
102 	StringUtils::InSituStripSpaceAndControl(string);
103 // ----------------------
104 
105 	const BString expected = "TonicWater";
106 	CPPUNIT_ASSERT_EQUAL(expected, string);
107 }
108 
109 
110 /*static*/ void
111 StringUtilsTest::AddTests(BTestSuite& parent)
112 {
113 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("StringUtilsTest");
114 
115 	suite.addTest(
116 		new CppUnit::TestCaller<StringUtilsTest>(
117 			"StringUtilsTest::TestStartInSituTrimSpaceAndControl",
118 			&StringUtilsTest::TestStartInSituTrimSpaceAndControl));
119 	suite.addTest(
120 		new CppUnit::TestCaller<StringUtilsTest>(
121 			"StringUtilsTest::TestEndInSituTrimSpaceAndControl",
122 			&StringUtilsTest::TestEndInSituTrimSpaceAndControl));
123 	suite.addTest(
124 		new CppUnit::TestCaller<StringUtilsTest>(
125 			"StringUtilsTest::TestStartAndEndInSituTrimSpaceAndControl",
126 			&StringUtilsTest::TestStartAndEndInSituTrimSpaceAndControl));
127 	suite.addTest(
128 		new CppUnit::TestCaller<StringUtilsTest>(
129 			"StringUtilsTest::TestNoTrimInSituTrimSpaceAndControl",
130 			&StringUtilsTest::TestNoTrimInSituTrimSpaceAndControl));
131 
132 	suite.addTest(
133 		new CppUnit::TestCaller<StringUtilsTest>(
134 			"StringUtilsTest::TestNoInSituStripSpaceAndControl",
135 			&StringUtilsTest::TestNoInSituStripSpaceAndControl));
136 	suite.addTest(
137 		new CppUnit::TestCaller<StringUtilsTest>(
138 			"StringUtilsTest::TestInSituStripSpaceAndControl",
139 			&StringUtilsTest::TestInSituStripSpaceAndControl));
140 
141 	parent.addTest("StringUtilsTest", &suite);
142 }