@@ -76,7 +76,41 @@ Now that you've created the solution, you'll create a class library named String
76
76
77
77
5 . Replace all of the existing code in the code editor with the following code:
78
78
79
- :::code language="csharp" source="samples/csharp/UtilityLibraries/StringLibrary/Class1.cs" id="Snippet1":::
79
+ ``` csharp
80
+ using System ;
81
+
82
+ namespace UtilityLibraries
83
+ {
84
+ public static class StringLibrary
85
+ {
86
+ public static bool StartsWithUpper (this string s )
87
+ {
88
+ if (String .IsNullOrWhiteSpace (s ))
89
+ return false ;
90
+
91
+ return Char .IsUpper (s [0 ]);
92
+ }
93
+
94
+ public static bool StartsWithLower (this string s )
95
+ {
96
+ if (String .IsNullOrWhiteSpace (s ))
97
+ return false ;
98
+
99
+ return Char .IsLower (s [0 ]);
100
+ }
101
+
102
+ public static bool HasEmbeddedSpaces (this string s )
103
+ {
104
+ foreach (var ch in s .Trim ())
105
+ {
106
+ if (ch == ' ' )
107
+ return true ;
108
+ }
109
+ return false ;
110
+ }
111
+ }
112
+ }
113
+ ```
80
114
81
115
StringLibrary has three static methods:
82
116
@@ -134,7 +168,59 @@ The next step is to create the unit test project to test the StringLibrary libra
134
168
135
169
6 . Replace the boilerplate unit test code provided by the template with the following code:
136
170
137
- :::code language="csharp" source="samples/snippets/csharp/lut-start/unittest1.cs" id="Snippet1":::
171
+ ``` csharp
172
+ using System ;
173
+ using Microsoft .VisualStudio .TestTools .UnitTesting ;
174
+ using UtilityLibraries ;
175
+
176
+ namespace StringLibraryTest
177
+ {
178
+ [TestClass ]
179
+ public class UnitTest1
180
+ {
181
+ [TestMethod ]
182
+ public void TestStartsWithUpper ()
183
+ {
184
+ // Tests that we expect to return true.
185
+ string [] words = { " Alphabet" , " Zebra" , " ABC" , " Αθήνα" , " Москва" };
186
+ foreach (var word in words )
187
+ {
188
+ bool result = word .StartsWithUpper ();
189
+ Assert .IsTrue (result ,
190
+ $" Expected for '{word }': true; Actual: {result }" );
191
+ }
192
+ }
193
+
194
+ [TestMethod ]
195
+ public void TestDoesNotStartWithUpper ()
196
+ {
197
+ // Tests that we expect to return false.
198
+ string [] words = { " alphabet" , " zebra" , " abc" , " αυτοκινητοβιομηχανία" , " государство" ,
199
+ " 1234" , " ." , " ;" , " " };
200
+ foreach (var word in words )
201
+ {
202
+ bool result = word .StartsWithUpper ();
203
+ Assert .IsFalse (result ,
204
+ $" Expected for '{word }': false; Actual: {result }" );
205
+ }
206
+ }
207
+
208
+ [TestMethod ]
209
+ public void DirectCallWithNullOrEmpty ()
210
+ {
211
+ // Tests that we expect to return false.
212
+ string [] words = { String .Empty , null };
213
+ foreach (var word in words )
214
+ {
215
+ bool result = StringLibrary .StartsWithUpper (word );
216
+ Assert .IsFalse (result ,
217
+ $" Expected for '{(word == null ? " <null>" : word )}': " +
218
+ $" false; Actual: {result }" );
219
+ }
220
+ }
221
+ }
222
+ }
223
+ ```
138
224
139
225
7 . Save your project by selecting the ** Save** icon on the toolbar.
140
226
0 commit comments