1 // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
\r
3 // StringBuilderTest.dll - NUnit Test Cases for the System.Text.StringBuilder class
\r
5 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
\r
7 // NOTES: I've also run all these tests against the MS implementation of
\r
8 // System.Text.StringBuilder to confirm that they return the same results
\r
11 // TODO: Add tests for the AppendFormat methods once the AppendFormat methods
\r
12 // are implemented in the StringBuilder class itself
\r
14 // TODO: Potentially add more variations on Insert / Append tests for all the
\r
15 // possible types. I don't really think that's necessary as they all
\r
16 // pretty much just do .ToString().ToCharArray() and then use the Append / Insert
\r
17 // CharArray function. The ToString() bit for each type should be in the unit
\r
18 // tests for those types, and the unit test for ToCharArray should be in the
\r
19 // string test type. If someone wants to add those tests here for completness
\r
20 // (and some double checking) then feel free :)
\r
23 using NUnit.Framework;
\r
27 namespace MonoTests.System.Text {
\r
29 public class StringBuilderTest : TestCase {
\r
31 private StringBuilder sb;
\r
33 public void TestConstructor1()
\r
35 // check the parameterless ctor
\r
36 sb = new StringBuilder();
\r
37 AssertEquals(String.Empty, sb.ToString());
\r
38 AssertEquals(0, sb.Length);
\r
39 AssertEquals(16, sb.Capacity);
\r
42 public void TestConstructor2()
\r
44 // check ctor that specifies the capacity
\r
45 sb = new StringBuilder(10);
\r
46 AssertEquals(String.Empty, sb.ToString());
\r
47 AssertEquals(0, sb.Length);
\r
48 // check that capacity is not less than default
\r
49 AssertEquals(10, sb.Capacity);
\r
51 sb = new StringBuilder(42);
\r
52 AssertEquals(String.Empty, sb.ToString());
\r
53 AssertEquals(0, sb.Length);
\r
54 // check that capacity is set
\r
55 AssertEquals(42, sb.Capacity);
\r
58 public void TestConstructor3() {
\r
59 // check ctor that specifies the capacity & maxCapacity
\r
60 sb = new StringBuilder(444, 1234);
\r
61 AssertEquals(String.Empty, sb.ToString());
\r
62 AssertEquals(0, sb.Length);
\r
63 AssertEquals(444, sb.Capacity);
\r
64 AssertEquals(1234, sb.MaxCapacity);
\r
67 public void TestConstructor4()
\r
69 // check for exception in ctor that specifies the capacity & maxCapacity
\r
71 sb = new StringBuilder(9999, 15);
\r
73 catch (ArgumentOutOfRangeException) {
\r
76 // if we didn't catch an exception, then we have a problem Houston.
\r
77 NUnit.Framework.Assertion.Fail("Capacity exeeds MaxCapacity");
\r
80 public void TestConstructor5() {
\r
81 String someString = null;
\r
82 sb = new StringBuilder(someString);
\r
83 AssertEquals("Should be empty string", String.Empty, sb.ToString());
\r
86 public void TestConstructor6() {
\r
87 // check for exception in ctor that prevents startIndex less than zero
\r
89 String someString = "someString";
\r
90 sb = new StringBuilder(someString, -1, 3, 18);
\r
92 catch (ArgumentOutOfRangeException) {
\r
95 // if we didn't catch an exception, then we have a problem Houston.
\r
96 NUnit.Framework.Assertion.Fail("StartIndex not allowed to be less than zero.");
\r
99 public void TestConstructor7() {
\r
100 // check for exception in ctor that prevents length less than zero
\r
102 String someString = "someString";
\r
103 sb = new StringBuilder(someString, 2, -222, 18);
\r
105 catch (ArgumentOutOfRangeException) {
\r
108 // if we didn't catch an exception, then we have a problem Houston.
\r
109 NUnit.Framework.Assertion.Fail("Length not allowed to be less than zero.");
\r
112 public void TestConstructor8() {
\r
113 // check for exception in ctor that ensures substring is contained in given string
\r
114 // check that startIndex is not too big
\r
116 String someString = "someString";
\r
117 sb = new StringBuilder(someString, 10000, 4, 18);
\r
119 catch (ArgumentOutOfRangeException) {
\r
122 // if we didn't catch an exception, then we have a problem Houston.
\r
123 NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
\r
126 public void TestConstructor9() {
\r
127 // check for exception in ctor that ensures substring is contained in given string
\r
128 // check that length doesn't go beyond end of string
\r
130 String someString = "someString";
\r
131 sb = new StringBuilder(someString, 4, 4000, 18);
\r
133 catch (ArgumentOutOfRangeException) {
\r
136 // if we didn't catch an exception, then we have a problem Houston.
\r
137 NUnit.Framework.Assertion.Fail("StartIndex and length must refer to a location within the string.");
\r
140 public void TestConstructor10() {
\r
141 // check that substring is taken properly and made into a StringBuilder
\r
142 String someString = "someString";
\r
143 sb = new StringBuilder(someString, 4, 6, 18);
\r
144 string expected = "String";
\r
145 AssertEquals( expected, sb.ToString());
\r
148 [ExpectedException(typeof(ArgumentOutOfRangeException))]
\r
149 public void TestConstructor11 () {
\r
150 new StringBuilder (-1);
\r
153 public void TestAppend() {
\r
154 StringBuilder sb = new StringBuilder( "Foo" );
\r
155 sb.Append( "Two" );
\r
156 string expected = "FooTwo";
\r
157 AssertEquals( expected, sb.ToString() );
\r
160 public void TestInsert() {
\r
161 StringBuilder sb = new StringBuilder();
\r
163 AssertEquals( String.Empty, sb.ToString() );
\r
164 /* Test empty StringBuilder conforms to spec */
\r
166 sb.Insert( 0, "Foo" ); /* Test insert at start of empty string */
\r
168 AssertEquals( "Foo", sb.ToString() );
\r
170 sb.Insert( 1, "!!" ); /* Test insert not at start of string */
\r
172 AssertEquals( "F!!oo", sb.ToString() );
\r
174 sb.Insert( 5, ".." ); /* Test insert at end of string */
\r
176 AssertEquals( "F!!oo..", sb.ToString() );
\r
178 sb.Insert( 0, 1234 ); /* Test insert of a number (at start of string) */
\r
180 // FIX: Why does this test fail?
\r
181 //AssertEquals( "1234F!!oo..", sb.ToString() );
\r
183 sb.Insert( 5, 1.5 ); /* Test insert of a decimal (and end of string) */
\r
185 // FIX: Why does this test fail?
\r
186 //AssertEquals( "1234F1.5!!oo..", sb.ToString() );
\r
188 sb.Insert( 4, 'A' ); /* Test char insert in middle of string */
\r
190 // FIX: Why does this test fail?
\r
191 //AssertEquals( "1234AF1.5!!oo..", sb.ToString() );
\r
195 public void TestReplace() {
\r
196 StringBuilder sb = new StringBuilder( "Foobarbaz" );
\r
198 sb.Replace( "bar", "!!!" ); /* Test same length replace in middle of string */
\r
200 AssertEquals( "Foo!!!baz", sb.ToString() );
\r
202 sb.Replace( "Foo", "ABcD" ); /* Test longer replace at start of string */
\r
204 AssertEquals( "ABcD!!!baz", sb.ToString() );
\r
206 sb.Replace( "baz", "00" ); /* Test shorter replace at end of string */
\r
208 AssertEquals( "ABcD!!!00", sb.ToString() );
\r
210 sb.Replace( sb.ToString(), null ); /* Test string clear as in spec */
\r
212 AssertEquals( String.Empty, sb.ToString() );
\r
215 /* |0123456789012345678901234567890| */
\r
216 sb.Append( "abc this is testing abc the abc abc partial replace abc" );
\r
218 sb.Replace( "abc", "!!!", 0, 31 ); /* Partial replace at start of string */
\r
220 AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
\r
222 sb.Replace( "testing", "", 0, 15 ); /* Test replace across boundary */
\r
224 AssertEquals( "!!! this is testing !!! the !!! abc partial replace abc", sb.ToString() );
\r
226 sb.Replace( "!!!", "" ); /* Test replace with empty string */
\r
228 AssertEquals(" this is testing the abc partial replace abc", sb.ToString() );
\r
231 public void TestAppendFormat() {
\r
235 public void MoreReplace ()
\r
237 StringBuilder sb = new StringBuilder ();
\r
238 sb.Append ("First");
\r
239 sb.Append ("Second");
\r
240 sb.Append ("Third");
\r
241 sb.Replace ("Second", "Gone", 2, sb.Length-2);
\r
242 AssertEquals ("#01", "FirstGoneThird", sb.ToString ());
\r
245 sb.Append ("This, is, a, list");
\r
246 sb.Replace (",", "comma-separated", 11, sb.Length-11);
\r
247 AssertEquals ("#02", "This, is, acomma-separated list", sb.ToString ());
\r