Skip to content

Commit

Permalink
feat: Update all references to other listings and update namespace (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMichaelis authored Oct 21, 2023
1 parent 96f56b3 commit 3365ba4
Show file tree
Hide file tree
Showing 6 changed files with 472 additions and 153 deletions.
169 changes: 155 additions & 14 deletions ListingManager.Tests/ListingInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,15 @@ public void GetPaddedListingNumber_ListingNumber_ReturnCorrectPaddedListingNumbe
}

[Theory]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_01", "Listing01.01.cs", false, false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_05", "Listing01.05.cs", false, false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_04a", "Listing01.04a.cs", false, false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_04a", "Listing01.04a.cs", true, false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_06B", "Listing01.06B.cs", false, false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_07C", "Listing01.07C.cs", true, false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_07C.Tests", "Listing01.07C.Tests.cs", true, true)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_08.Tests", "Listing01.08.Tests.cs", true, true)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_10.Tests", "Listing01.10.Tests.cs", true, true)]
public void GetNewNamespace_Namespace_ReturnCorrectNewNamespace(string expected, string listingPath, bool chapterOnly, bool isTest)
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_01", "Listing01.01.cs", false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_05", "Listing01.05.cs", false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_04a", "Listing01.04a.cs", false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_06B", "Listing01.06B.cs", false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_07C", "Listing01.07C.cs", false)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_07C.Tests", "Listing01.07C.Tests.cs", true)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_08.Tests", "Listing01.08.Tests.cs", true)]
[InlineData("AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_10.Tests", "Listing01.10.Tests.cs", true)]
public void GetNewNamespace_Namespace_ReturnCorrectNewNamespace(string expected, string listingPath, bool isTest)
{
List<string> filesToMake = new()
{
Expand All @@ -204,12 +203,12 @@ public void GetNewNamespace_Namespace_ReturnCorrectNewNamespace(string expected,
var writtenFiles = WriteFiles(tempDir, filesToMake, toWrite);
var writtenFile = Assert.Single(writtenFiles);

Assert.Equal(expected, new ListingInformation(writtenFile.FullName, isTest).GetNewNamespace(chapterOnly));
Assert.Equal(expected, new ListingInformation(writtenFile.FullName, isTest).GetNewNamespace());
}

[Theory]
[InlineData("Listing01.01.cs", "Listing01.01.cs", false, false)]
public void GetNewFileName_FileName_ReturnCorrectNewFileName(string expected, string listingPath, bool chapterOnly, bool isTest)
[InlineData("Listing01.01.cs", "Listing01.01.cs", false)]
public void GetNewFileName_FileName_ReturnCorrectNewFileName(string expected, string listingPath, bool isTest)
{
List<string> filesToMake = new()
{
Expand All @@ -230,6 +229,148 @@ public void GetNewFileName_FileName_ReturnCorrectNewFileName(string expected, st
var writtenFiles = WriteFiles(tempDir, filesToMake, toWrite);
var writtenFile = Assert.Single(writtenFiles);

Assert.Equal(expected, new ListingInformation(writtenFile.FullName, isTest).GetNewFileName(chapterOnly));
Assert.Equal(expected, new ListingInformation(writtenFile.FullName, isTest).GetNewFileName());
}

#region ReferencesInFile
[Fact]
public void UpdateChapterListingNumbers_UsingStatement_ListingReferencesUpdated()
{
List<string> filesToMake = new()
{
Path.Join("Chapter18","Listing18.01.UsingTypeGetPropertiesToObtainAnObjectsPublicProperties.cs"),
Path.Join("Chapter18","Listing18.04.UsingTypeofToCreateASystem.TypeInstance.cs"),
};
ICollection<string> expectedFiles = new List<string>
{
Path.Join("Chapter18","Listing18.01.UsingTypeGetPropertiesToObtainAnObjectsPublicProperties.cs"),
Path.Join("Chapter18","Listing18.02.UsingTypeofToCreateASystem.TypeInstance.cs")
};

IEnumerable<string> toWrite = new List<string>
{
"namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_04",
"{",
" using System;",
" using System.Reflection;",
" public class Program { }",
"}"
};

IEnumerable<string> toWriteAlso = new List<string>
{
"using Listing18_04;",
"namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_01",
"{",
" using System;",
" using System.Reflection;",
" public class Program { }",
"}"
};

IEnumerable<string> expectedFileContents = new List<string>
{
"using Listing18_02;",
"namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_01",
"{",
" using System;",
" using System.Reflection;",
" public class Program { }",
"}"
};

DirectoryInfo tempDir = CreateTempDirectory();
DirectoryInfo chapterDir = CreateTempDirectory(tempDir, name: "Chapter18");
CreateTempDirectory(tempDir, name: "Chapter18.Tests");
WriteFile(tempDir, filesToMake.Last(), toWrite.ToList());
WriteFile(tempDir, filesToMake.First(), toWriteAlso.ToList());
expectedFiles = ConvertFileNamesToFullPath(expectedFiles, tempDir).ToList();

ListingManager listingManager = new(tempDir.FullName, new OSStorageManager());
listingManager.UpdateChapterListingNumbers(chapterDir.FullName, byFolder: true);

List<string> files = FileManager.GetAllFilesAtPath(tempDir.FullName, true)
.Where(x => Path.GetExtension(x) == ".cs").OrderBy(x => x).ToList();

// Assert
Assert.Equal(2, files.Count);
Assert.Equivalent(expectedFiles, files);

Assert.Equal(string.Join(Environment.NewLine, expectedFileContents) + Environment.NewLine, File.ReadAllText(expectedFiles.First()));
}

[Fact]
public void UpdateChapterListingNumbers_StringLisingReference_ReferencesUpdated()
{
List<string> filesToMake = new()
{
Path.Join("Chapter18","Listing18.01.UsingTypeGetPropertiesToObtainAnObjectsPublicProperties.cs"),
Path.Join("Chapter18","Listing18.04.UsingTypeofToCreateASystem.TypeInstance.cs"),
};
ICollection<string> expectedFiles = new List<string>
{
Path.Join("Chapter18","Listing18.01.UsingTypeGetPropertiesToObtainAnObjectsPublicProperties.cs"),
Path.Join("Chapter18","Listing18.02.UsingTypeofToCreateASystem.TypeInstance.cs")
};

IEnumerable<string> toWrite = new List<string>
{
"namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_04",
"{",
" using System;",
" using System.Reflection;",
" public class Program { }",
"}"
};

IEnumerable<string> toWriteAlso = new List<string>
{
"using Listing18_04;",
"namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_01",
"{",
" using System;",
" using System.Reflection;",
" public class Program { " +
" static string Ps1Path { get; } =",
" Path.GetFullPath(",
" Path.Join(Ps1DirectoryPath, \"Listing18.04.HelloWorldInC#.ps1\"), \"Listing18.04.HelloWorldInC#.ps1\");",
" }",
"}"
};

IEnumerable<string> expectedFileContents = new List<string>
{
"using Listing18_02;",
"namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_01",
"{",
" using System;",
" using System.Reflection;",
" public class Program { " +
" static string Ps1Path { get; } =",
" Path.GetFullPath(",
" Path.Join(Ps1DirectoryPath, \"Listing18.02.HelloWorldInC#.ps1\"), \"Listing18.02.HelloWorldInC#.ps1\");",
" }",
"}"
};

DirectoryInfo tempDir = CreateTempDirectory();
DirectoryInfo chapterDir = CreateTempDirectory(tempDir, name: "Chapter18");
CreateTempDirectory(tempDir, name: "Chapter18.Tests");
WriteFile(tempDir, filesToMake.Last(), toWrite.ToList());
WriteFile(tempDir, filesToMake.First(), toWriteAlso.ToList());
expectedFiles = ConvertFileNamesToFullPath(expectedFiles, tempDir).ToList();

ListingManager listingManager = new(tempDir.FullName, new OSStorageManager());
listingManager.UpdateChapterListingNumbers(chapterDir.FullName, byFolder: true);

List<string> files = FileManager.GetAllFilesAtPath(tempDir.FullName, true)
.Where(x => Path.GetExtension(x) == ".cs").OrderBy(x => x).ToList();

// Assert
Assert.Equal(2, files.Count);
Assert.Equivalent(expectedFiles, files);

Assert.Equal(string.Join(Environment.NewLine, expectedFileContents) + Environment.NewLine, File.ReadAllText(expectedFiles.First()));
}
#endregion ReferencesInFile
}
Loading

0 comments on commit 3365ba4

Please # to comment.