Unit testing for Chapter 6
MODIFY
src/Chapter06/Chapter06.csproj
src/Chapter06/Chapter06.csproj
</PropertyGroup>
<Import Project="..\Chapter.props" />
<ItemGroup>
- <Compile Remove="Listing06.15.UsingThePrivateAccessModifier.cs" />
<Compile Remove="Listing06.21.DefiningReadOnlyProperties.cs" />
<Compile Remove="Listing06.23.PlacingAccessModifiersOnSetters.cs" />
<Compile Remove="Listing06.24.CILCodeResultingFromProperties.cs" />
<Compile Remove="Listing06.25.PropertiesAreAnExplicitConstructInCIL.cs" />
- <Compile Remove="Listing06.28.DefaultConstructorNoLongerAvailable.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Shared\Program.cs">
</Compile>
</ItemGroup>
<ItemGroup>
- <None Include="Listing06.15.UsingThePrivateAccessModifier.cs">
- <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
- </None>
<None Include="Listing06.21.DefiningReadOnlyProperties.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</None>
<None Include="Listing06.24.CILCodeResultingFromProperties.cs" />
<None Include="Listing06.25.PropertiesAreAnExplicitConstructInCIL.cs" />
- <None Include="Listing06.28.DefaultConstructorNoLongerAvailable.cs">
- <CopyToOutputDirectory>Always</CopyToOutputDirectory>
- </None>
- </ItemGroup>
- <ItemGroup>
+ <Compile Update="Listing06.15.UsingThePrivateAccessModifier.cs">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Compile>
<Compile Update="Listing06.26.DefiningAConstructor.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
+ <Compile Update="Listing06.28.DefaultConstructorNoLongerAvailable.cs">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Compile>
+ <Compile Update="Listing06.35A.RequiredProperties.cs">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Compile>
+ <Compile Update="Listing06.35B.SpecifyingRequiredMembersWithinTheObjectInitializer.cs">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Compile>
</ItemGroup>
</Project>

MODIFY
src/Chapter06/Listing06.15.UsingThePrivateAccessModifier.cs
src/Chapter06/Listing06.15.UsingThePrivateAccessModifier.cs
using System;
-//namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_15
-//{
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_15
+{
#region INCLUDE
public class Employee
{
@@ -54,14 +54,17 @@ using System;
// ...
+
+ #if COMPILEERROR
#region HIGHLIGHT
// ERROR: Password is private, so it cannot be
// accessed from outside the class
Console.WriteLine(
"Password = {0}", employee.Password);
#endregion HIGHLIGHT
+ #endif // COMPILEERROR
}
// ...
}
#endregion INCLUDE
-//}
+}

MODIFY
src/Chapter06/Listing06.28.DefaultConstructorNoLongerAvailable.cs
src/Chapter06/Listing06.28.DefaultConstructorNoLongerAvailable.cs
@@ -12,14 +12,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28
{
Employee employee;
- //asdfasdfasdfdsaf
- //#if INCLUDE
+ #if COMPILEERROR
#region HIGHLIGHT
// ERROR: No overload because method 'Employee'
// takes '0' arguments
employee = new Employee();
#endregion HIGHLIGHT
- //#endif INCLUDE
+ #endif // COMPILEERROR
// ...
}

MODIFY
src/Chapter06/Listing06.35A.RequiredProperties.cs
src/Chapter06/Listing06.35A.RequiredProperties.cs
#if NET7_0_OR_GREATER
-using System.Diagnostics.CodeAnalysis;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35A
{
+using System.Diagnostics.CodeAnalysis;
#region INCLUDE
public class Book
{
@@ -17,9 +17,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35A
}
set
{
- ArgumentException.ThrowIfNullOrEmpty(
- value = value?.Trim()!);
- _Title = value;
+ _Title = value ?? throw new ArgumentNullException(nameof(value));
}
}
@@ -34,9 +32,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35A
}
set
{
- ArgumentException.ThrowIfNullOrEmpty(
- value = value?.Trim()!);
- _Isbn = value;
+ _Isbn = value ?? throw new ArgumentNullException(nameof(value));
}
}

MODIFY
src/Chapter06/Listing06.35B.SpecifyingRequiredMembersWithinTheObjectInitializer.cs
src/Chapter06/Listing06.35B.SpecifyingRequiredMembersWithinTheObjectInitializer.cs
#if NET7_0_OR_GREATER
- #if INCLUDE
+#if COMPILEERROR
using AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35A;
- #endif // INCLUDE
+#endif // COMPILEERROR
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35B
{
@@ -10,7 +10,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35B
{
public static void Main()
{
- #if INCLUDE
+ #if COMPILEERROR
#region INCLUDE
// Error CS9035:
// Required member 'Book.Isbn' must be set in the object
@@ -20,7 +20,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_35B
// ...
#endregion INCLUDE
- #endif // INCLUDE
+ #endif // COMPILEERROR
}
}
}

MODIFY
src/Chapter06/Listing06.35C.DisablingRequiredObjectInitialization.cs
src/Chapter06/Listing06.35C.DisablingRequiredObjectInitialization.cs
@@ -9,7 +9,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_34C
{
Book book = new(42) {
Subtitle = "The Comprehensive, Expert Guide " +
- "to C# for Programmers at All Levels" };
+ "to C# for Programmers at all levels" };
}
}

MODIFY
src/Chapter09.Tests/Listing09.02.ReadOnlyMembers.Tests.cs
src/Chapter09.Tests/Listing09.02.ReadOnlyMembers.Tests.cs
@@ -35,7 +35,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_02.Tests
}
}";
- CompilerAssert.ExpectErrorsAsync(thingCode,
+ CompilerAssertOld.ExpectErrors(thingCode,
new CompileError("CS1604", "Cannot assign to 'Number' because it is read-only"));
}
}

Unit testing for Chapter 6