diff --git a/src/Chapter03.Tests/Listing03.02.DereferencingAnUnassignedVariable.Tests.cs b/src/Chapter03.Tests/Listing03.02.DereferencingAnUnassignedVariable.Tests.cs index 1fbc0fe6c..0db5c3fea 100644 --- a/src/Chapter03.Tests/Listing03.02.DereferencingAnUnassignedVariable.Tests.cs +++ b/src/Chapter03.Tests/Listing03.02.DereferencingAnUnassignedVariable.Tests.cs @@ -13,6 +13,6 @@ public class UppercaseTests public async Task UnassignedVariableThrowsError() { await CompilerAssert.CompileTestTargetFileAsync( - new string[] { "CS0165", "CS8602" }); + ["CS0165", "CS8602"]); } } \ No newline at end of file diff --git a/src/Chapter03.Tests/Listing03.24.Tests.cs b/src/Chapter03.Tests/Listing03.24.Tests.cs new file mode 100644 index 000000000..1e53ecfe0 --- /dev/null +++ b/src/Chapter03.Tests/Listing03.24.Tests.cs @@ -0,0 +1,11 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_24.Tests; + +[TestClass] +public class ProgramTests +{ + [TestMethod] + public void Main_ThrowsIndexOutOfRangeException() + { + Assert.ThrowsException(Program.Main); + } +} diff --git a/src/Chapter03.Tests/Listing03.27.Tests.cs b/src/Chapter03.Tests/Listing03.27.Tests.cs new file mode 100644 index 000000000..f97a9d3e9 --- /dev/null +++ b/src/Chapter03.Tests/Listing03.27.Tests.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using System.Diagnostics.Metrics; +using System; + +using IntelliTect.TestTools.Console; + +using Microsoft.VisualStudio.TestPlatform.Utilities; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_27.Tests; + +[TestClass] +public class ProgrammingLanguagesTests +{ + [TestMethod] + public void Main_AssertConsoleOutput_MatchesExpected() + { + var expected = +""" +The wave of the future, COBOL, is at index 2. + +First Element Last Element +------------- ------------ +C# TypeScript +TypeScript C# + +After clearing, the array size is: 9 +"""; + + ConsoleAssert.Expect( + expected, ProgrammingLanguages.Main); + } +} diff --git a/src/Chapter04.Tests/Listing04.28.Tests.cs b/src/Chapter04.Tests/Listing04.28.Tests.cs index e36efc6fb..c6055f11d 100644 --- a/src/Chapter04.Tests/Listing04.28.Tests.cs +++ b/src/Chapter04.Tests/Listing04.28.Tests.cs @@ -12,6 +12,6 @@ public class ProgramTests [TestMethod] public async Task CompileError_OutOfScope() { - await CompilerAssert.CompileAsync($"Listing04.28.OutOfScope.cs", ExpectedErrorIds); + await CompilerAssert.CompileTestTargetFileAsync(ExpectedErrorIds); } } diff --git a/src/Chapter04.Tests/Listing04.55.Tests.cs b/src/Chapter04.Tests/Listing04.55.Tests.cs new file mode 100644 index 000000000..efdd29931 --- /dev/null +++ b/src/Chapter04.Tests/Listing04.55.Tests.cs @@ -0,0 +1,28 @@ +using IntelliTect.TestTools.Console; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_55.Tests; + +[TestClass] +public class GoToStatementsTests +{ + [TestMethod] + public void Main_out_NoOutput() + { + string expected = ""; + ConsoleAssert.Expect(expected, () => GoToStatements.Main(["/out"])); + } + + [TestMethod] + public void Main_f_isFiltered() + { + string expected = "Filtering..."; + ConsoleAssert.Expect(expected, () => GoToStatements.Main(["/f"])); + } + + [TestMethod] + public void Main_f_out_isFiltered() + { + string expected = "Filtering..."; + ConsoleAssert.Expect(expected, () => GoToStatements.Main(["/f", "/out"])); + } +} diff --git a/src/Chapter04/Listing04.45.DoWhileLoop.cs b/src/Chapter04/Listing04.45.DoWhileLoop.cs index fa3140ea1..977068662 100644 --- a/src/Chapter04/Listing04.45.DoWhileLoop.cs +++ b/src/Chapter04/Listing04.45.DoWhileLoop.cs @@ -1,6 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_45; -public class Program +public class DoWhileLoop { public static void Main() { diff --git a/src/Chapter04/Listing04.55.GotoStatements.cs b/src/Chapter04/Listing04.55.GotoStatements.cs index c4400302b..7ab03578c 100644 --- a/src/Chapter04/Listing04.55.GotoStatements.cs +++ b/src/Chapter04/Listing04.55.GotoStatements.cs @@ -2,7 +2,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_55; -public class Program +public class GoToStatements { #region INCLUDE // ... diff --git a/src/Chapter05.Tests/Listing05.05.Tests.cs b/src/Chapter05.Tests/Listing05.05.Tests.cs new file mode 100644 index 000000000..0a76a01a6 --- /dev/null +++ b/src/Chapter05.Tests/Listing05.05.Tests.cs @@ -0,0 +1,24 @@ + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_05.Tests; + +[TestClass] +public class ProgramTests +{ + [TestMethod] + public void Main_Quit_False() + { + string expected = "<>False"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => Program.Main()); + } + + [TestMethod] + public void Main_AnotherCommand_True() + { + string expected = "<>True"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => Program.Main()); + } +} diff --git a/src/Chapter05.Tests/Listing05.25.Tests.cs b/src/Chapter05.Tests/Listing05.25.Tests.cs new file mode 100644 index 000000000..0b8ca53db --- /dev/null +++ b/src/Chapter05.Tests/Listing05.25.Tests.cs @@ -0,0 +1,24 @@ + +using IntelliTect.TestTools.Console; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_25.Tests; + +[TestClass] +public class SpecifyingParametersByNameTests +{ + [TestMethod] + public void SpecifyingParametersByName_WithNamedParameters_WritesToConsole() + { + const string expected = "First Name: Inigo Middle Name: Last Name: Montoya"; + + ConsoleAssert.Expect(expected, () => SpecifyingParametersByName.Main()); + } + + [TestMethod] + public void SpecifyingParametersByName_WithNamedParametersInReverseOrder_WritesToConsole() + { + const string expected = "First Name: Inigo Middle Name: MiddleName Last Name: Montoya"; + + ConsoleAssert.Expect(expected, () => SpecifyingParametersByName.DisplayGreeting(firstName: "Inigo", middleName: "MiddleName", lastName: "Montoya")); + } +} diff --git a/src/Chapter05/Listing05.05.AReturnStatementBeforeTheEndOfAmethod.cs b/src/Chapter05/Listing05.05.AReturnStatementBeforeTheEndOfAmethod.cs index a20637506..89a6f80a9 100644 --- a/src/Chapter05/Listing05.05.AReturnStatementBeforeTheEndOfAmethod.cs +++ b/src/Chapter05/Listing05.05.AReturnStatementBeforeTheEndOfAmethod.cs @@ -26,7 +26,7 @@ public static bool MyMethod() #region EXCLUDE private static string ObtainCommand() { - throw new NotImplementedException(); + return Console.ReadLine() ?? string.Empty; } #endregion EXCLUDE } diff --git a/src/Chapter05/Listing05.10.StringBuilderImplicitUsingStatements.cs b/src/Chapter05/Listing05.10.StringBuilderImplicitUsingStatements.cs index 5c0f5ab86..52dcc0a7d 100644 --- a/src/Chapter05/Listing05.10.StringBuilderImplicitUsingStatements.cs +++ b/src/Chapter05/Listing05.10.StringBuilderImplicitUsingStatements.cs @@ -3,9 +3,7 @@ // the specified namespace into the project global using System.Text; #region EXCLUDE - namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_10; - #endregion EXCLUDE public class Program { diff --git a/src/Chapter05/Listing05.25.SpecifyingParametersByName.cs b/src/Chapter05/Listing05.25.SpecifyingParametersByName.cs index 108fcef9e..5b174a445 100644 --- a/src/Chapter05/Listing05.25.SpecifyingParametersByName.cs +++ b/src/Chapter05/Listing05.25.SpecifyingParametersByName.cs @@ -1,6 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_25; -public class Program +public class SpecifyingParametersByName { #region INCLUDE public static void Main() @@ -17,7 +17,10 @@ public static void DisplayGreeting( string? lastName = null ) { - // ... + #region EXCLUDE + string fullName = $"First Name: {firstName} Middle Name: {middleName} Last Name: {lastName}"; + Console.Write(fullName); + #endregion EXCLUDE } #endregion INCLUDE } diff --git a/src/Chapter06.Tests/Listing06.07.Tests.cs b/src/Chapter06.Tests/Listing06.07.Tests.cs new file mode 100644 index 000000000..8110cf2ac --- /dev/null +++ b/src/Chapter06.Tests/Listing06.07.Tests.cs @@ -0,0 +1,19 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_07.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public void Employee_SettingFields_GetFieldValues() + { + const string expected = + @"Inigo Montoya"; + + Employee employee = new() + { + FirstName = "Inigo", + LastName = "Montoya" + }; + Assert.AreEqual(expected, employee.GetName()); + } +} diff --git a/src/Chapter06.Tests/Listing06.09.Tests.cs b/src/Chapter06.Tests/Listing06.09.Tests.cs new file mode 100644 index 000000000..7b9deba86 --- /dev/null +++ b/src/Chapter06.Tests/Listing06.09.Tests.cs @@ -0,0 +1,17 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_09.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public void Employee_SetFieldsViaMethod_GetFieldValues() + { + Employee employee = new(); + + employee.SetName("Inigo", "Montoya"); + + Assert.AreEqual("Inigo", employee.FirstName); + Assert.AreEqual("Montoya", employee.LastName); + Assert.AreEqual("Inigo Montoya", employee.GetName()); + } +} diff --git a/src/Chapter06.Tests/Listing06.10.Tests.cs b/src/Chapter06.Tests/Listing06.10.Tests.cs new file mode 100644 index 000000000..38ffec1aa --- /dev/null +++ b/src/Chapter06.Tests/Listing06.10.Tests.cs @@ -0,0 +1,17 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_10.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public void Employee_SetFieldsViaMethod_GetFieldValues() + { + Employee employee = new(); + + employee.SetName("Inigo", "Montoya"); + + Assert.AreEqual("Inigo", employee.FirstName); + Assert.AreEqual("Montoya", employee.LastName); + Assert.AreEqual("Inigo Montoya", employee.GetName()); + } +} diff --git a/src/Chapter06.Tests/Listing06.11.UsingThisWithAMethod.Tests.cs b/src/Chapter06.Tests/Listing06.11.UsingThisWithAMethod.Tests.cs index b98f4271c..f381fb401 100644 --- a/src/Chapter06.Tests/Listing06.11.UsingThisWithAMethod.Tests.cs +++ b/src/Chapter06.Tests/Listing06.11.UsingThisWithAMethod.Tests.cs @@ -1,4 +1,3 @@ - namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_11.Tests; [TestClass] diff --git a/src/Chapter06.Tests/Listing06.12.Tests.cs b/src/Chapter06.Tests/Listing06.12.Tests.cs new file mode 100644 index 000000000..bad5a5bb7 --- /dev/null +++ b/src/Chapter06.Tests/Listing06.12.Tests.cs @@ -0,0 +1,22 @@ +using IntelliTect.TestTools.Console; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_12.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public void DataStorage_ReadTrace() + { + const string expected = + "Writing employee (Inigo Montoya) information to file."; + + Employee employee = new() + { + FirstName = "Inigo", + LastName = "Montoya" + }; + ConsoleAssert.Expect(expected, () => + DataStorage.Store(employee)); + } +} diff --git a/src/Chapter06.Tests/Listing06.21.Tests.cs b/src/Chapter06.Tests/Listing06.21.Tests.cs index d3b71f643..63d59f3e5 100644 --- a/src/Chapter06.Tests/Listing06.21.Tests.cs +++ b/src/Chapter06.Tests/Listing06.21.Tests.cs @@ -8,8 +8,6 @@ public class ProgramTests [TestMethod] public async Task UnassignedVariableThrowsError() { - await CompilerAssert.CompileAsync( - "Listing06.21.DefiningReadOnlyProperties.cs", - "CS0200"); + await CompilerAssert.CompileTestTargetFileAsync(["CS0200"]); } } \ No newline at end of file diff --git a/src/Chapter06.Tests/Listing06.23.Tests.cs b/src/Chapter06.Tests/Listing06.23.Tests.cs index 32d66d6fb..ace880c9e 100644 --- a/src/Chapter06.Tests/Listing06.23.Tests.cs +++ b/src/Chapter06.Tests/Listing06.23.Tests.cs @@ -8,8 +8,6 @@ public class ProgramTests [TestMethod] public async Task UnassignedVariableThrowsError() { - await CompilerAssert.CompileAsync( - "Listing06.23.PlacingAccessModifiersOnSetters.cs", - "CS0272"); + await CompilerAssert.CompileTestTargetFileAsync(["CS0272"]); } } \ No newline at end of file diff --git a/src/Chapter06.Tests/Listing06.26.DefiningAConstructor.Tests.cs b/src/Chapter06.Tests/Listing06.26.DefiningAConstructor.Tests.cs index c11777346..3f162c592 100644 --- a/src/Chapter06.Tests/Listing06.26.DefiningAConstructor.Tests.cs +++ b/src/Chapter06.Tests/Listing06.26.DefiningAConstructor.Tests.cs @@ -1,9 +1,10 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28.Tests; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_26.Tests; [TestClass] public class ProgramTests { +#if NET8_0_OR_GREATER [TestMethod] public void Main_UsingAConstructor_WriteFirstNameLastNameSalary() { @@ -13,4 +14,23 @@ public void Main_UsingAConstructor_WriteFirstNameLastNameSalary() IntelliTect.TestTools.Console.ConsoleAssert.Expect( expected, Program.Main); } + + [TestMethod] + public void Employee_NotFullNamePassed_ThrowArgumentException() + { + Employee employee = new("Inigo", "Montoya"); + Assert.ThrowsException( + () => employee.FullName = "FirstName"); + } + + [TestMethod] + public void Employee_FullNamePassed_SetsProperly() + { + Employee employee = new("Inigo", "Montoya"); + employee.FullName = "Inigo Montoya"; + Assert.AreEqual("Inigo Montoya", employee.FullName); + Assert.AreEqual("Inigo", employee.FirstName); + Assert.AreEqual("Montoya", employee.LastName); + } +#endif // NET8_0_OR_GREATER } diff --git a/src/Chapter06.Tests/Listing06.28.Tests.cs b/src/Chapter06.Tests/Listing06.28.Tests.cs new file mode 100644 index 000000000..7464ac2b7 --- /dev/null +++ b/src/Chapter06.Tests/Listing06.28.Tests.cs @@ -0,0 +1,43 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_28.Tests; + +[TestClass] +public class ProgramTests +{ + [TestMethod] + public void Main_UsingAConstructor_WriteFirstNameLastNameSalary() + { + const string expected = + @"Inigo Montoya: Too Little"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect( + expected, Program.Main); + } + + [TestMethod] + public void Employee_Constructor_SetsValuesProperly() + { + Employee employee = new("Inigo", "Montoya"); + Assert.AreEqual("Inigo Montoya", employee.FullName); + Assert.AreEqual("Inigo", employee.FirstName); + Assert.AreEqual("Montoya", employee.LastName); + } + + [TestMethod] + public void Employee_SetInvalidFullName_ThrowsArgumentNullException() + { + Employee employee = new("Inigo", "Montoya"); + Assert.ThrowsException(() => employee.FullName = "FirstName"); + } + + [TestMethod] + public void Employee_SetFullName_SetsNameCorrectly() + { + Employee employee = new("Blah", "Blah") + { + FullName = "Inigo Montoya" + }; + Assert.AreEqual("Inigo Montoya", employee.FullName); + Assert.AreEqual("Inigo", employee.FirstName); + Assert.AreEqual("Montoya", employee.LastName); + } +} diff --git a/src/Chapter06.Tests/Listing06.29.DefaultConstructorNoLongerAvailable.Tests.cs b/src/Chapter06.Tests/Listing06.29.DefaultConstructorNoLongerAvailable.Tests.cs index a4d8ac58c..b850c06b5 100644 --- a/src/Chapter06.Tests/Listing06.29.DefaultConstructorNoLongerAvailable.Tests.cs +++ b/src/Chapter06.Tests/Listing06.29.DefaultConstructorNoLongerAvailable.Tests.cs @@ -12,6 +12,6 @@ await CompilerAssert.CompileAsync( new string[] { CompilerAssert.GetTargetFileNameToCompileFromTestFileName(), "Listing06.28.DefiningAConstructor.cs" }, - new string[] { "CS7036" }); + ["CS7036"]); } } diff --git a/src/Chapter06.Tests/Listing06.30.CallingAnObjectInitializerWithExplicitMemberAssignment.Tests.cs b/src/Chapter06.Tests/Listing06.30.CallingAnObjectInitializerWithExplicitMemberAssignment.Tests.cs index 1c13915fa..73437ab1e 100644 --- a/src/Chapter06.Tests/Listing06.30.CallingAnObjectInitializerWithExplicitMemberAssignment.Tests.cs +++ b/src/Chapter06.Tests/Listing06.30.CallingAnObjectInitializerWithExplicitMemberAssignment.Tests.cs @@ -1,4 +1,3 @@ - namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_30.Tests; [TestClass] diff --git a/src/Chapter06.Tests/Listing06.32.Tests.cs b/src/Chapter06.Tests/Listing06.32.Tests.cs new file mode 100644 index 000000000..f5b6ed962 --- /dev/null +++ b/src/Chapter06.Tests/Listing06.32.Tests.cs @@ -0,0 +1,13 @@ +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_32.Tests; + +[TestClass] +public class ProgramTests +{ + [TestMethod] + public async Task Main_CompileErrorCS8852() + { + await CompilerAssert.CompileTestTargetFileAsync(["CS8852"]); + } +} diff --git a/src/Chapter06.Tests/Listing06.36.ProvidingValidationOnNon-NullableProperty.Tests.cs b/src/Chapter06.Tests/Listing06.36.ProvidingValidationOnNon-NullableProperty.Tests.cs index a2f9074e3..c558c0f1a 100644 --- a/src/Chapter06.Tests/Listing06.36.ProvidingValidationOnNon-NullableProperty.Tests.cs +++ b/src/Chapter06.Tests/Listing06.36.ProvidingValidationOnNon-NullableProperty.Tests.cs @@ -1,4 +1,3 @@ - namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_36.Tests; [TestClass] diff --git a/src/Chapter06.Tests/Listing06.37.Tests.cs b/src/Chapter06.Tests/Listing06.37.Tests.cs new file mode 100644 index 000000000..4a2206220 --- /dev/null +++ b/src/Chapter06.Tests/Listing06.37.Tests.cs @@ -0,0 +1,20 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_37.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public void Employee_SetNameAsNull_ThrowsArgumentNullException() + { + Employee employee; + Assert.ThrowsException(() => employee = new(null!)); + } + + [TestMethod] + public void Employee_SetNameCorrectly() + { + Employee employee; + employee = new("Inigo Montoya"); + Assert.AreEqual("Inigo Montoya", employee.Name); + } +} diff --git a/src/Chapter06.Tests/Listing06.45.Tests.cs b/src/Chapter06.Tests/Listing06.45.Tests.cs new file mode 100644 index 000000000..09283947e --- /dev/null +++ b/src/Chapter06.Tests/Listing06.45.Tests.cs @@ -0,0 +1,17 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_45.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public void Employee_NextId_IncrementsAcrossInstances() + { + Assert.AreEqual(0, Employee.NextId); + Employee employee = new("Inigo", "Montoya"); + Assert.AreEqual(1, Employee.NextId); + Assert.AreEqual(0, employee.Id); + employee = new("Fezzik", "Giant"); + Assert.AreEqual(2, Employee.NextId); + Assert.AreEqual(1, employee.Id); + } +} diff --git a/src/Chapter06.Tests/Listing06.46.Tests.cs b/src/Chapter06.Tests/Listing06.46.Tests.cs new file mode 100644 index 000000000..a75faa3f7 --- /dev/null +++ b/src/Chapter06.Tests/Listing06.46.Tests.cs @@ -0,0 +1,13 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_46.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public void Employee_NextId_SetsAtDeclaration() + { + Assert.AreEqual(42, Employee.NextId); + _ = new Employee(); + Assert.AreEqual(42, Employee.NextId); + } +} diff --git a/src/Chapter06.Tests/Listing06.49.Tests.cs b/src/Chapter06.Tests/Listing06.49.Tests.cs new file mode 100644 index 000000000..d34bd29fa --- /dev/null +++ b/src/Chapter06.Tests/Listing06.49.Tests.cs @@ -0,0 +1,14 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_49.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public void Employee_NextId_GetsSetInStaticConstructor() + { + Assert.IsTrue((100 < Employee.NextId) && (Employee.NextId < 1000)); + int id = Employee.NextId; + _ = new Employee(); + Assert.AreEqual(id, Employee.NextId); + } +} diff --git a/src/Chapter06.Tests/Listing06.54.Tests.cs b/src/Chapter06.Tests/Listing06.54.Tests.cs new file mode 100644 index 000000000..a7024c38d --- /dev/null +++ b/src/Chapter06.Tests/Listing06.54.Tests.cs @@ -0,0 +1,13 @@ +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_54.Tests; + +[TestClass] +public class EmployeeTests +{ + [TestMethod] + public async Task Employee_ReadonlyField_CompileErrorCS0191() + { + await CompilerAssert.CompileTestTargetFileAsync(["CS0191"]); + } +} diff --git a/src/Chapter06.Tests/Listing06.59.Tests.cs b/src/Chapter06.Tests/Listing06.59.Tests.cs new file mode 100644 index 000000000..361a09110 --- /dev/null +++ b/src/Chapter06.Tests/Listing06.59.Tests.cs @@ -0,0 +1,53 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_59.Tests; + +[TestClass] +public class PersonTests +{ + [TestMethod] + public void Person_OnLastNameChanging_ArgumentNullExceptionThrown() + { + Person person = new() + { + LastName = "Montoya" + }; + Assert.AreEqual("Montoya", person.LastName); + Assert.ThrowsException( + () => person.LastName = null!); + } + + [TestMethod] + public void Person_OnLastNameChanging_ArgumentExceptionThrown() + { + Person person = new() + { + LastName = "Montoya" + }; + Assert.AreEqual("Montoya", person.LastName); + Assert.ThrowsException( + () => person.LastName = string.Empty); + } + + [TestMethod] + public void Person_OnFirstNameChanging_ArgumentNullExceptionThrown() + { + Person person = new() + { + FirstName = "Inigo" + }; + Assert.AreEqual("Inigo", person.FirstName); + Assert.ThrowsException( + () => person.FirstName = null!); + } + + [TestMethod] + public void Person_OnFirstNameChanging_ArgumentExceptionThrown() + { + Person person = new() + { + FirstName = "Inigo" + }; + Assert.AreEqual("Inigo", person.FirstName); + Assert.ThrowsException( + () => person.FirstName = string.Empty); + } +} diff --git a/src/Chapter06/Listing06.07.AccessingFieldsFromWithinTheContainingClass.cs b/src/Chapter06/Listing06.07.AccessingFieldsFromWithinTheContainingClass.cs index 7a7196310..c688e296f 100644 --- a/src/Chapter06/Listing06.07.AccessingFieldsFromWithinTheContainingClass.cs +++ b/src/Chapter06/Listing06.07.AccessingFieldsFromWithinTheContainingClass.cs @@ -13,7 +13,7 @@ public class Employee #region HIGHLIGHT public string GetName() { - return $"{ FirstName } { LastName }"; + return $"{ FirstName } { LastName }"; } #endregion HIGHLIGHT } diff --git a/src/Chapter06/Listing06.09.UsingThisToIdentifyTheFieldsOwnerExplicitly.cs b/src/Chapter06/Listing06.09.UsingThisToIdentifyTheFieldsOwnerExplicitly.cs index e009aa8f9..285a6b1d3 100644 --- a/src/Chapter06/Listing06.09.UsingThisToIdentifyTheFieldsOwnerExplicitly.cs +++ b/src/Chapter06/Listing06.09.UsingThisToIdentifyTheFieldsOwnerExplicitly.cs @@ -12,7 +12,7 @@ public class Employee public string GetName() { - return $"{ FirstName } { LastName }"; + return $"{ FirstName } { LastName }"; } #region HIGHLIGHT diff --git a/src/Chapter06/Listing06.10.UsingThisToAvoidAmbiguity.cs b/src/Chapter06/Listing06.10.UsingThisToAvoidAmbiguity.cs index 0d9f576e6..54db23e01 100644 --- a/src/Chapter06/Listing06.10.UsingThisToAvoidAmbiguity.cs +++ b/src/Chapter06/Listing06.10.UsingThisToAvoidAmbiguity.cs @@ -12,10 +12,10 @@ public class Employee public string GetName() { - return $"{ FirstName } { LastName }"; + return $"{ FirstName } { LastName }"; } - // Caution: Parameter names use PascalCase + // Caution: Parameter names use PascalCase public void SetName(string FirstName, string LastName) { this.FirstName = FirstName; diff --git a/src/Chapter06/Listing06.12.PassingThisInAMethodCall.cs b/src/Chapter06/Listing06.12.PassingThisInAMethodCall.cs index 4bab229bc..62e604526 100644 --- a/src/Chapter06/Listing06.12.PassingThisInAMethodCall.cs +++ b/src/Chapter06/Listing06.12.PassingThisInAMethodCall.cs @@ -25,7 +25,7 @@ public class DataStorage public static void Store(Employee employee) { #region EXCLUDE - System.Diagnostics.Trace.WriteLine( + Console.WriteLine( $@"Writing employee ({ employee.FirstName} {employee.LastName }) information to file."); diff --git a/src/Chapter06/Listing06.26.DefiningAPrimaryConstructor.cs b/src/Chapter06/Listing06.26.DefiningAPrimaryConstructor.cs index 9a805753b..30837ea49 100644 --- a/src/Chapter06/Listing06.26.DefiningAPrimaryConstructor.cs +++ b/src/Chapter06/Listing06.26.DefiningAPrimaryConstructor.cs @@ -5,10 +5,12 @@ public class Program { public static void Main() { - Employee employee = new("Inigo", "Montoya"); - employee.Salary = "Too Little"; + Employee employee = new("Inigo", "Montoya") + { + Salary = "Too Little" + }; - System.Console.WriteLine( + Console.WriteLine( $"{employee.FirstName} {employee.LastName}: {employee.Salary}"); } } @@ -48,7 +50,7 @@ public string FullName { // Throw an exception if the full // name was not assigned - throw new System.ArgumentException( + throw new ArgumentException( $"Assigned value '{ value }' is invalid", nameof(value)); } diff --git a/src/Chapter06/Listing06.27.CallingAConstructor.cs b/src/Chapter06/Listing06.27.CallingAConstructor.cs index 8fcf2defc..bfa67a602 100644 --- a/src/Chapter06/Listing06.27.CallingAConstructor.cs +++ b/src/Chapter06/Listing06.27.CallingAConstructor.cs @@ -13,7 +13,7 @@ public static void Main() #endregion HIGHLIGHT employee.Salary = "Too Little"; - System.Console.WriteLine( + Console.WriteLine( "{0} {1}: {2}", employee.FirstName, employee.LastName, diff --git a/src/Chapter06/Listing06.28.DefiningAConstructor.cs b/src/Chapter06/Listing06.28.DefiningAConstructor.cs index 15eaa4b86..aae08e48e 100644 --- a/src/Chapter06/Listing06.28.DefiningAConstructor.cs +++ b/src/Chapter06/Listing06.28.DefiningAConstructor.cs @@ -5,10 +5,12 @@ public class Program public static void Main() { Employee employee; - employee = new("Inigo", "Montoya"); - employee.Salary = "Too Little"; + employee = new("Inigo", "Montoya") + { + Salary = "Too Little" + }; - System.Console.WriteLine( + Console.WriteLine( "{0} {1}: {2}", employee.FirstName, employee.LastName, @@ -36,8 +38,8 @@ public Employee(string firstName, string lastName) public string? Title { get; set; } public Employee? Manager { get; set; } - // Name property - public string Name + // FullName property + public string FullName { get { @@ -58,7 +60,7 @@ public string Name { // Throw an exception if the full // name was not assigned - throw new System.ArgumentException( + throw new ArgumentException( string.Format( $"Assigned value '{ value }' is invalid", nameof(value))); diff --git a/src/Chapter06/Listing06.36.ProvidingValidationOnNon-NullableProperty.cs b/src/Chapter06/Listing06.36.ProvidingValidationOnNon-NullableProperty.cs index 02b6bef67..dced318ab 100644 --- a/src/Chapter06/Listing06.36.ProvidingValidationOnNon-NullableProperty.cs +++ b/src/Chapter06/Listing06.36.ProvidingValidationOnNon-NullableProperty.cs @@ -8,7 +8,7 @@ public static void Main() { Employee employee = new("Inigo Montoya"); - System.Console.WriteLine(employee.Name); + Console.WriteLine(employee.Name); // ... } diff --git a/src/Shared/Tests/CompilerAssert.cs b/src/Shared/Tests/CompilerAssert.cs index b85f76189..fdb0d7e93 100644 --- a/src/Shared/Tests/CompilerAssert.cs +++ b/src/Shared/Tests/CompilerAssert.cs @@ -115,6 +115,9 @@ public static async Task CompileAsync(string[] fileNames, string[] expectedError public static async Task CompileAsync(string fileName, params string[] expectedErrorIds) => await CompileAsync(new string[] { fileName }, expectedErrorIds); + public static async Task CompileAsync(string fileName, string expectedErrorId) => + await CompileAsync(new string[] { fileName }, [expectedErrorId]); + //public static async Task CompileAsync(string[] expectedErrorIds, [CallerFilePath] string fileName = null!) => // await CompileAsync(new string[] { fileName }, expectedErrorIds);