1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Tests
.Helpers
18 using System
.Collections
;
19 using System
.Globalization
;
20 using System
.Threading
;
21 using Castle
.MonoRail
.Framework
.Helpers
;
22 using NUnit
.Framework
;
25 public class TextHelperTestCase
27 private static TextHelper helper
= new TextHelper();
30 public void FormatPhone_CanFormatForUSCulture()
32 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
33 Thread
.CurrentThread
.CurrentCulture
= en
;
34 Thread
.CurrentThread
.CurrentUICulture
= en
;
36 Assert
.AreEqual("(123) 456-1234", helper
.FormatPhone("1234561234"));
40 public void FormatPhone_IgnoresStringTooSmall()
42 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
43 Thread
.CurrentThread
.CurrentCulture
= en
;
44 Thread
.CurrentThread
.CurrentUICulture
= en
;
46 Assert
.AreEqual("123", helper
.FormatPhone("123"));
50 public void FormatPhone_IgnoresStringIfSomeNonNumericCharsArePresent()
52 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
53 Thread
.CurrentThread
.CurrentCulture
= en
;
54 Thread
.CurrentThread
.CurrentUICulture
= en
;
56 Assert
.AreEqual("123-111-1212", helper
.FormatPhone("123-111-1212"));
57 Assert
.AreEqual("(123)111-1212", helper
.FormatPhone("(123)111-1212"));
58 Assert
.AreEqual("123.111.1212", helper
.FormatPhone("123.111.1212"));
62 [ExpectedException(typeof(ArgumentNullException
))]
63 public void PascalCaseToWord_CannotAcceptNulls()
65 TextHelper
.PascalCaseToWord(null);
69 public void PascalCaseToWord_SeparatesWordsBasedOnCase()
71 Assert
.AreEqual("Sequence Info", TextHelper
.PascalCaseToWord("SequenceInfo"));
75 [ExpectedException(typeof(ArgumentNullException
))]
76 public void ToSentenceWithNull()
78 string sentence
= TextHelper
.ToSentence(null);
79 Assert
.AreEqual("", sentence
);
83 public void ToSentenceWithStringArrayNoElements()
85 string sentence
= TextHelper
.ToSentence(new string[0]);
86 Assert
.AreEqual("", sentence
);
90 public void ToSentenceWithStringArrayOneElement()
92 string sentence
= TextHelper
.ToSentence(new string[] { "apple" }
);
93 Assert
.AreEqual("apple", sentence
);
97 public void ToSentenceWithStringArrayTwoElements()
99 string sentence
= TextHelper
.ToSentence(new string[] { "apple", "banana" }
);
100 Assert
.AreEqual("apple and banana", sentence
);
104 public void ToSentenceWithStringArrayThreeElements()
106 string sentence
= TextHelper
.ToSentence(new string[] { "apple", "banana", "mango" }
);
107 Assert
.AreEqual("apple, banana and mango", sentence
);
111 public void ToSentenceWithSpecifiedConnector()
113 string sentence
= TextHelper
.ToSentence(new string[] { "apple", "banana", "mango" }
, "y");
114 Assert
.AreEqual("apple, banana y mango", sentence
);
118 public void ToSentenceWithCommaBeforeConnectorSpecified()
120 string sentence
= TextHelper
.ToSentence(new string[] { "apple", "banana", "mango" }
, TextHelper
.DefaultConnector
, false);
121 Assert
.AreEqual("apple, banana, and mango", sentence
);
126 private readonly string _firstName
;
127 private readonly string _lastName
;
129 public Person(string firstName
, string lastName
)
131 _firstName
= firstName
;
132 _lastName
= lastName
;
135 public override string ToString()
137 return _firstName
+ " " + _lastName
;
142 public void ToSentenceWithArrayListOfPeople()
144 ArrayList people
= new ArrayList();
145 people
.Add(new Person("Clark", "Kent"));
146 people
.Add(new Person("Lois", "Lane"));
147 people
.Add(new Person("Lex", "Luther"));
148 string sentence
= TextHelper
.ToSentence(people
);
149 Assert
.AreEqual("Clark Kent, Lois Lane and Lex Luther", sentence
);