@@ -104,7 +104,7 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
104104 if ( curListingData is null || ! chapterOnly && ! byFolder && listingNumber == curListingData . ListingNumber )
105105 {
106106 File . Copy ( curListingData ? . TemporaryPath , curListingData ? . Path , true ) ;
107- if ( testListingData . Where ( x => x ? . ListingNumber == curListingData . ListingNumber && x . ListingSuffix == curListingData . ListingSuffix ) . FirstOrDefault ( ) is ListingInformation currentTestListingData )
107+ if ( testListingData . FirstOrDefault ( x => x ? . ListingNumber == curListingData . ListingNumber && x . ListingSuffix == curListingData . ListingSuffix ) is ListingInformation currentTestListingData )
108108 {
109109 File . Copy ( currentTestListingData ? . TemporaryPath , currentTestListingData ? . Path , true ) ;
110110 }
@@ -127,14 +127,14 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
127127
128128 UpdateListingNamespace ( cur , listingChapterNumber ,
129129 completeListingNumber ,
130- curListingData . ListingDescription , curListingData , verbose , preview ) ;
130+ curListingData . ListingDescription , verbose , preview ) ;
131131
132132 if ( testListingData . Where ( x => x ? . ListingNumber == curListingData . ListingNumber && x . ListingSuffix == curListingData . ListingSuffix ) . FirstOrDefault ( ) is ListingInformation curTestListingData )
133133 {
134134 Console . Write ( "Updating test. " ) ;
135135 UpdateTestListingNamespace ( curTestListingData . TemporaryPath , listingChapterNumber ,
136136 completeListingNumber ,
137- curListingData . ListingDescription , curListingData , verbose , preview ) ;
137+ curListingData . ListingDescription , verbose , preview ) ;
138138 }
139139
140140 }
@@ -151,7 +151,7 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
151151 public static bool IsExtraListing ( string path ,
152152 string regexNamespace = @".*Listing\d{2}\.\d{2}(A|B|C|D).*\.cs$" )
153153 {
154- Regex fileNameRegex = new Regex ( regexNamespace ) ;
154+ Regex fileNameRegex = new ( regexNamespace ) ;
155155
156156 string directoryNameFull = Path . GetDirectoryName ( path ) ?? string . Empty ;
157157 string directoryName = Path . GetFileName ( directoryNameFull ) ;
@@ -166,16 +166,15 @@ public static bool IsExtraListing(string path,
166166 /// <param name="chapterNumber">The chapter the listing belongs to</param>
167167 /// <param name="listingNumber">The updated listing number</param>
168168 /// <param name="listingData">The name of the listing to be included in the namespace/path</param>
169- /// <param name="curListingData"></param>
170169 /// <param name="verbose">When true, enables verbose console output</param>
171170 /// <param name="preview">When true, leaves files in place and only print console output</param>
172171 private static void UpdateTestListingNamespace ( string path , int chapterNumber , string listingNumber ,
173- string listingData , ListingInformation curListingData , bool verbose = false , bool preview = false )
172+ string listingData , bool verbose = false , bool preview = false )
174173 {
175174 string paddedChapterNumber = chapterNumber . ToString ( "00" ) ;
176175
177176 string regexSingleDigitListingWithSuffix = @"\d{1}[A-Za-z]" ;
178- string paddedListingNumber = "" ;
177+ string paddedListingNumber ;
179178 if ( Regex . IsMatch ( listingNumber , regexSingleDigitListingWithSuffix ) )
180179 { //allows for keeping the original listing number with a suffix. e.g. "01A"
181180 paddedListingNumber = listingNumber . PadLeft ( 3 , '0' ) ;
@@ -217,9 +216,8 @@ private static void UpdateTestListingNamespace(string path, int chapterNumber, s
217216 /// <param name="listingData">The name of the listing to be included in the namespace/path</param>
218217 /// <param name="verbose">When true, enables verbose console output</param>
219218 /// <param name="preview">When true, leaves files in place and only print console output</param>
220- /// <param name="curListingData">An instance of ListingInformation for the current listing.</param>
221219 private static void UpdateListingNamespace ( string path , int chapterNumber , string listingNumber ,
222- string listingData , ListingInformation curListingData , bool verbose = false , bool preview = false )
220+ string listingData , bool verbose = false , bool preview = false )
223221 {
224222 string paddedChapterNumber = chapterNumber . ToString ( "00" ) ;
225223
@@ -265,41 +263,39 @@ private static void UpdateNamespaceOfPath(string path, string newNamespace, stri
265263
266264 string targetPath = Path . Combine ( Path . GetDirectoryName ( path ) ?? string . Empty , newFileName ) ?? path ;
267265
268- using ( TextWriter textWriter = new StreamWriter ( targetPath , true ) )
266+ using TextWriter textWriter = new StreamWriter ( targetPath , true ) ;
267+ foreach ( string line in allLinesInFile )
269268 {
270- foreach ( string line in allLinesInFile )
269+ if ( line . StartsWith ( "namespace" ) )
271270 {
272- if ( line . StartsWith ( "namespace ") )
271+ if ( line . TrimEnd ( ) . EndsWith ( "; ") )
273272 {
274- if ( line . TrimEnd ( ) . EndsWith ( ";" ) )
275- {
276- textWriter . WriteLine ( "namespace " + newNamespace + ";" ) ;
277- }
278- else
279- {
280- textWriter . WriteLine ( "namespace " + newNamespace ) ;
281- }
273+ textWriter . WriteLine ( "namespace " + newNamespace + ";" ) ;
282274 }
283275 else
284276 {
285- textWriter . WriteLine ( line ) ;
277+ textWriter . WriteLine ( "namespace " + newNamespace ) ;
286278 }
287279 }
280+ else
281+ {
282+ textWriter . WriteLine ( line ) ;
283+ }
288284 }
289285 }
290286
291287 public static bool GetPathToAccompanyingUnitTest ( string listingPath , out string pathToTest )
292288 {
293289 string testDirectory = $ "{ Path . GetDirectoryName ( listingPath ) } .Tests";
294290
295- Regex regex = new Regex ( @"((Listing\d{2}\.\d{2})([A-Z]?)((\.Tests)?)).*\.cs.tmp$" ) ;
291+ Regex regex = new ( @"((Listing\d{2}\.\d{2})([A-Z]?)((\.Tests)?)).*\.cs.tmp$" ) ;
296292
297293 Match fileNameMatch = regex . Match ( listingPath ) ;
298294
299295 string testFileName = fileNameMatch . Success ? regex . Match ( listingPath ) . Groups [ 1 ] . Value : "" ;
300296
301297 Regex pathToTestRegex =
302- new Regex ( Regex . Escape ( $ "{ testDirectory } { Path . DirectorySeparatorChar } { testFileName } ")
298+ new ( Regex . Escape ( $ "{ testDirectory } { Path . DirectorySeparatorChar } { testFileName } ")
303299 + @".*\.cs" ) ;
304300
305301 if ( Directory . Exists ( testDirectory ) )
@@ -326,12 +322,12 @@ private static string GetTestLayout(string chapterNumber, string listingNumber)
326322 listingNumber . PadLeft ( 2 , '0' ) ) + TestBodyLayout ;
327323 }
328324
329- private static string TestHeaderLayout =
325+ private static readonly string TestHeaderLayout =
330326@"using Microsoft.VisualStudio.TestTools.UnitTesting;
331327
332328namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter{0}.Listing{0}_{1}.Tests" ;
333329
334- private static string TestBodyLayout =
330+ private static readonly string TestBodyLayout =
335331@"
336332{
337333 [TestClass]
@@ -393,7 +389,7 @@ private static bool GenerateUnitTest(string pathToTest)
393389 return false ;
394390 }
395391
396- Regex getListingData = new Regex ( @"Listing(\d{2})\.(\d{2})" ) ;
392+ Regex getListingData = new ( @"Listing(\d{2})\.(\d{2})" ) ;
397393
398394 var match = getListingData . Match ( pathToTest ) ;
399395
0 commit comments