Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update all references to other listings and update namespace #89

Merged
merged 7 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading