88namespace ListingManager . Tests
99{
1010 [ TestClass ]
11- public class ListingManagerTests
11+ public class ListingManagerTests : TempFileTestBase
1212 {
13- private List < string > CreatedFiles { get ; } = new List < string > ( ) ;
14- private List < string > CreatedDirectories { get ; } = new List < string > ( ) ;
13+ private List < string > CreatedFiles { get ; } = new ( ) ;
14+ private List < string > CreatedDirectories { get ; } = new ( ) ;
1515
1616 [ TestMethod ]
1717 [ DataRow ( "Listing02.01.SpecifyingLiteralValues.cs" , false ) ]
1818 [ DataRow ( "Listing02.01A.SpecifyingLiteralValues.cs" , true ) ]
19- [ DataRow ( "Chapter02.Tests/Listing02.01A.SpecifyingLiteralValues.cs" , false ) ]
2019 [ DataRow ( "Listing02.01.cs" , false ) ]
2120 public void IsIncorrectListingFromPath_FindsIncorrectListing ( string fileName , bool expectedResult )
2221 {
23- WriteFile ( fileName ) ;
22+ var fileInfo = CreateTempFile ( null , fileName , fileName ) ;
2423
25- string directoryName = Path . GetDirectoryName ( fileName ) ;
24+ string directoryName = fileInfo . DirectoryName ?? string . Empty ;
2625
2726 if ( ! string . IsNullOrWhiteSpace ( directoryName ) )
2827 {
2928 CreatedDirectories . Add ( directoryName ) ;
3029 }
3130
32- string path = Path . Combine ( Environment . CurrentDirectory , fileName ) ;
31+ string path = Path . Combine ( TempDirectory . ToString ( ) , fileName ) ;
32+
33+ bool actualResult = ListingManager . IsExtraListing ( path ) ;
34+
35+ Assert . AreEqual ( expectedResult , actualResult ) ;
36+ }
37+
38+ [ TestMethod ]
39+ [ DataRow ( "/Chapter02.Tests" , "Listing02.01A.SpecifyingLiteralValues.cs" , false ) ]
40+ public void ListingsInTestDirectories_AreNotCountedAsExtraListings ( string parentDirectory , string fileName , bool expectedResult )
41+ {
42+ var directory = CreateTempDirectory ( name : parentDirectory ) ;
43+ var fileInfo = CreateTempFile ( directory , fileName , fileName ) ;
44+
45+ string directoryName = fileInfo . DirectoryName ?? string . Empty ;
46+
47+ if ( ! string . IsNullOrWhiteSpace ( directoryName ) )
48+ {
49+ CreatedDirectories . Add ( directoryName ) ;
50+ }
51+
52+ string path = Path . Combine ( directory . FullName , fileName ) ;
3353
3454 bool actualResult = ListingManager . IsExtraListing ( path ) ;
3555
@@ -52,35 +72,35 @@ public void GetAllExtraListings_ExtraListingsReturned()
5272
5373 ICollection < string > expectedFiles = filesToMake ;
5474 expectedFiles . Remove ( @"Chapter02\Listing02.02.cs" ) ;
55- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
75+ expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) . ToList ( ) ;
5676
57- WriteFiles ( filesToMake ) ;
77+ WriteFiles ( filesToMake , null ) ;
5878
5979 var extraListings = ListingManager . GetAllExtraListings ( Environment . CurrentDirectory ) . ToList ( ) ;
6080
61- CollectionAssert . AreEquivalent ( ( ICollection ) expectedFiles , ( ICollection ) extraListings ) ;
81+ CollectionAssert . AreEquivalent ( ( ICollection ) expectedFiles , extraListings ) ;
6282 }
6383
6484 [ TestMethod ]
6585 public void UpdateChapterListingNumbers_ListingsWithinListMissing_ListingsRenumbered ( )
6686 {
67- ICollection < string > filesToMake = new List < string >
87+ List < string > filesToMake = new List < string >
6888 {
6989 "Listing01.01.SpecifyingLiteralValues.cs" ,
7090 "Listing01.02.cs" ,
7191 "Listing01.04.cs" ,
7292 "Listing01.06.Something.cs"
7393 } ;
7494
75- ICollection < string > expectedFiles = new List < string >
95+ List < string > expectedFiles = new List < string >
7696 {
7797 "Listing01.01.SpecifyingLiteralValues.cs" ,
7898 "Listing01.02.cs" ,
7999 "Listing01.03.cs" ,
80100 "Listing01.04.Something.cs"
81101 } ;
82102
83- IEnumerable < string > toWrite = new List < string >
103+ List < string > toWrite = new List < string >
84104 {
85105 "namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_01" ,
86106 "{" ,
@@ -90,7 +110,7 @@ public void UpdateChapterListingNumbers_ListingsWithinListMissing_ListingsRenumb
90110 "}"
91111 } ;
92112 WriteFiles ( filesToMake , toWrite ) ;
93- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
113+ expectedFiles = ( List < string > ) ConvertFilenamesToFullPath ( expectedFiles ) ;
94114 foreach ( string file in filesToMake )
95115 {
96116 CreatedFiles . Remove ( file ) ;
@@ -101,7 +121,7 @@ public void UpdateChapterListingNumbers_ListingsWithinListMissing_ListingsRenumb
101121
102122 var files = Directory . EnumerateFiles ( Environment . CurrentDirectory )
103123 . Where ( x => Path . GetExtension ( x ) == ".cs" ) . OrderBy ( x => x ) . ToList ( ) ;
104- CollectionAssert . AreEquivalent ( ( ICollection ) expectedFiles , files ) ;
124+ CollectionAssert . AreEquivalent ( expectedFiles , files ) ;
105125 }
106126
107127 [ TestMethod ]
@@ -131,7 +151,7 @@ public void UpdateChapterListingNumbers_ListingAtBeginningOfListMissing_Listings
131151 "}"
132152 } ;
133153 WriteFiles ( filesToMake , toWrite ) ;
134- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
154+ expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) . ToList ( ) ;
135155 foreach ( string file in filesToMake )
136156 {
137157 CreatedFiles . Remove ( file ) ;
@@ -201,7 +221,7 @@ public void UpdateChapterListingNumbers_MultipleListingsMissing_ListingsRenumber
201221 "}"
202222 } ;
203223 WriteFiles ( filesToMake , toWrite ) ;
204- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
224+ expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) . ToList ( ) ;
205225 foreach ( string file in filesToMake )
206226 {
207227 CreatedFiles . Remove ( file ) ;
@@ -249,7 +269,7 @@ public void UpdateChapterListingNumbers_AdditionalListings_ListingsRenumbered()
249269 "}"
250270 } ;
251271 WriteFiles ( filesToMake , toWrite ) ;
252- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
272+ expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) . ToList ( ) ;
253273 foreach ( string file in filesToMake )
254274 {
255275 CreatedFiles . Remove ( file ) ;
@@ -289,7 +309,7 @@ public void UpdateChapterListingNumbers_UnitTestsAlsoUpdated_ListingsAndTestsUpd
289309 @"Chapter01\Listing01.04.cs" ,
290310 @"Chapter01\Listing01.05.cs" ,
291311 @"Chapter01.Tests\Listing01.01.cs" ,
292- @"Chapter01.Tests\Listing01.02.Some. cs" ,
312+ @"Chapter01.Tests\Listing01.02.cs" ,
293313 @"Chapter01.Tests\Listing01.03.cs" ,
294314 @"Chapter01.Tests\Listing01.04.cs" ,
295315 @"Chapter01.Tests\Listing01.05.cs"
@@ -306,7 +326,7 @@ public void UpdateChapterListingNumbers_UnitTestsAlsoUpdated_ListingsAndTestsUpd
306326 } ;
307327
308328 WriteFiles ( filesToMake , toWrite ) ;
309- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
329+ expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) . ToList ( ) ;
310330 foreach ( string file in filesToMake )
311331 {
312332 CreatedFiles . Remove ( file ) ;
@@ -350,7 +370,7 @@ public void UpdateChapterListingNumbersUsingChapterNumberFromFolder_UnitTestsAls
350370 @"Chapter42\Listing42.04.cs" ,
351371 @"Chapter42\Listing42.05.cs" ,
352372 @"Chapter42.Tests\Listing42.01.cs" ,
353- @"Chapter42.Tests\Listing42.02.Some. cs" ,
373+ @"Chapter42.Tests\Listing42.02.cs" ,
354374 @"Chapter42.Tests\Listing42.03.cs" ,
355375 @"Chapter42.Tests\Listing42.04.cs" ,
356376 @"Chapter42.Tests\Listing42.05.cs"
@@ -367,7 +387,7 @@ public void UpdateChapterListingNumbersUsingChapterNumberFromFolder_UnitTestsAls
367387 } ;
368388
369389 WriteFiles ( filesToMake , toWrite ) ;
370- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
390+ expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) . ToList ( ) ;
371391 foreach ( string file in filesToMake )
372392 {
373393 CreatedFiles . Remove ( file ) ;
@@ -412,7 +432,7 @@ public void UpdateOnlyChapterNumberOfListingUsingChapterNumberFromFolder_UnitTes
412432 @"Chapter42\Listing42.01C.cs" ,
413433 @"Chapter42\Listing42.05.cs" ,
414434 @"Chapter42.Tests\Listing42.01.cs" ,
415- @"Chapter42.Tests\Listing42.01A.Some. cs" ,
435+ @"Chapter42.Tests\Listing42.01A.cs" ,
416436 @"Chapter42.Tests\Listing42.01B.cs" ,
417437 @"Chapter42.Tests\Listing42.01C.cs" ,
418438 @"Chapter42.Tests\Listing42.05.cs"
@@ -429,7 +449,7 @@ public void UpdateOnlyChapterNumberOfListingUsingChapterNumberFromFolder_UnitTes
429449 } ;
430450
431451 WriteFiles ( filesToMake , toWrite ) ;
432- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
452+ expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) . ToList ( ) ;
433453 foreach ( string file in filesToMake )
434454 {
435455 CreatedFiles . Remove ( file ) ;
@@ -460,17 +480,17 @@ public void GetPathToAccompanyingUnitTest_GivenListingWithNoTest_CorrectPathRetu
460480 string listingName )
461481 {
462482 bool result = ListingManager . GetPathToAccompanyingUnitTest ( chapter + Path . DirectorySeparatorChar + listingName , out string pathToTest ) ;
463- char directorySeperator = Path . DirectorySeparatorChar ;
483+ char directorySeparator = Path . DirectorySeparatorChar ;
464484 Assert . IsFalse ( result ) ;
465- Assert . AreEqual ( $ "{ chapter } .Tests{ directorySeperator } { listingName } ", pathToTest ) ;
485+ Assert . AreEqual ( $ "{ chapter } .Tests{ directorySeparator } { listingName } ", pathToTest ) ;
466486 }
467487
468488 [ TestMethod ]
469489 public void GenerateUnitTests_TestsGenerated ( )
470490 {
471491 string chapter = "Chapter01" ;
472492
473- ICollection < string > filesToCreate = new List < string >
493+ List < string > filesToCreate = new List < string >
474494 {
475495 @"Chapter01\Listing01.01.Something.cs" ,
476496 @"Chapter01\Listing01.02A.cs" ,
@@ -483,9 +503,9 @@ public void GenerateUnitTests_TestsGenerated()
483503 expectedFilesList . Add ( file . Replace ( chapter , chapter + ".Tests" ) ) ;
484504 }
485505 var expectedFiles = ( ICollection < string > ) expectedFilesList ;
486- expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) ;
506+ expectedFiles = ConvertFilenamesToFullPath ( expectedFiles ) . ToList ( ) ;
487507
488- WriteFiles ( filesToCreate ) ;
508+ WriteFiles ( filesToCreate , null ) ;
489509 CreatedDirectories . Add ( chapter ) ;
490510 CreatedDirectories . Add ( chapter + ".Tests" ) ;
491511
@@ -523,7 +543,7 @@ public void Cleanup()
523543 }
524544 }
525545
526- private ICollection < string > ConvertFilenamesToFullPath ( ICollection < string > fileNamesToConvert )
546+ private IEnumerable < string > ConvertFilenamesToFullPath ( IEnumerable < string > fileNamesToConvert )
527547 {
528548 var fullPaths = new List < string > ( ) ;
529549
@@ -535,7 +555,7 @@ private ICollection<string> ConvertFilenamesToFullPath(ICollection<string> fileN
535555 return fullPaths ;
536556 }
537557
538- private void WriteFile ( string fileName , IEnumerable < string > toWrite = null )
558+ private void WriteFile ( string fileName , IEnumerable < string > ? toWrite )
539559 {
540560 FileInfo file = new FileInfo ( fileName ) ;
541561 file . Directory ? . Create ( ) ;
@@ -545,11 +565,13 @@ private void WriteFile(string fileName, IEnumerable<string> toWrite = null)
545565 CreatedFiles . Add ( fileName ) ;
546566 }
547567
548- private void WriteFiles ( IEnumerable < string > fileNames , IEnumerable < string > toWrite = null )
568+ private void WriteFiles ( IEnumerable < string > fileNames , IEnumerable < string > ? toWrite )
549569 {
570+ List < string > filesToWrite = toWrite ? . ToList ( ) ?? new List < string > ( ) ;
571+
550572 foreach ( string file in fileNames )
551573 {
552- WriteFile ( file , toWrite ) ;
574+ WriteFile ( file , filesToWrite ) ;
553575 }
554576 }
555577 }
0 commit comments