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: Add missing tests through chapter 6 #603

Closed
wants to merge 7 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class UppercaseTests
public async Task UnassignedVariableThrowsError()
{
await CompilerAssert.CompileTestTargetFileAsync(
new string[] { "CS0165", "CS8602" });
["CS0165", "CS8602"]);
}
}
11 changes: 11 additions & 0 deletions src/Chapter03.Tests/Listing03.24.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_24.Tests;

[TestClass]
public class ProgramTests
{
[TestMethod]
public void Main_ThrowsIndexOutOfRangeException()
{
Assert.ThrowsException<IndexOutOfRangeException>(Program.Main);
}
}
32 changes: 32 additions & 0 deletions src/Chapter03.Tests/Listing03.27.Tests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Chapter04.Tests/Listing04.28.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions src/Chapter04.Tests/Listing04.55.Tests.cs
Original file line number Diff line number Diff line change
@@ -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"]));
}
}
2 changes: 1 addition & 1 deletion src/Chapter04/Listing04.45.DoWhileLoop.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_45;

public class Program
public class DoWhileLoop
{
public static void Main()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter04/Listing04.55.GotoStatements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_55;

public class Program
public class GoToStatements
{
#region INCLUDE
// ...
Expand Down
24 changes: 24 additions & 0 deletions src/Chapter05.Tests/Listing05.05.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_05.Tests;

[TestClass]
public class ProgramTests
{
[TestMethod]
public void Main_Quit_False()
{
string expected = "<<quit>>False";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected,
() => Program.Main());
}

[TestMethod]
public void Main_AnotherCommand_True()
{
string expected = "<<other>>True";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected,
() => Program.Main());
}
}
24 changes: 24 additions & 0 deletions src/Chapter05.Tests/Listing05.25.Tests.cs
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static bool MyMethod()
#region EXCLUDE
private static string ObtainCommand()
{
throw new NotImplementedException();
return Console.ReadLine() ?? string.Empty;
}
#endregion EXCLUDE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
7 changes: 5 additions & 2 deletions src/Chapter05/Listing05.25.SpecifyingParametersByName.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_25;

public class Program
public class SpecifyingParametersByName
{
#region INCLUDE
public static void Main()
Expand All @@ -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
}
19 changes: 19 additions & 0 deletions src/Chapter06.Tests/Listing06.07.Tests.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
17 changes: 17 additions & 0 deletions src/Chapter06.Tests/Listing06.09.Tests.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
17 changes: 17 additions & 0 deletions src/Chapter06.Tests/Listing06.10.Tests.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_11.Tests;

[TestClass]
Expand Down
22 changes: 22 additions & 0 deletions src/Chapter06.Tests/Listing06.12.Tests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
4 changes: 1 addition & 3 deletions src/Chapter06.Tests/Listing06.21.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class ProgramTests
[TestMethod]
public async Task UnassignedVariableThrowsError()
{
await CompilerAssert.CompileAsync(
"Listing06.21.DefiningReadOnlyProperties.cs",
"CS0200");
await CompilerAssert.CompileTestTargetFileAsync(["CS0200"]);
}
}
4 changes: 1 addition & 3 deletions src/Chapter06.Tests/Listing06.23.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class ProgramTests
[TestMethod]
public async Task UnassignedVariableThrowsError()
{
await CompilerAssert.CompileAsync(
"Listing06.23.PlacingAccessModifiersOnSetters.cs",
"CS0272");
await CompilerAssert.CompileTestTargetFileAsync(["CS0272"]);
}
}
22 changes: 21 additions & 1 deletion src/Chapter06.Tests/Listing06.26.DefiningAConstructor.Tests.cs
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -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<ArgumentException>(
() => 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
}
43 changes: 43 additions & 0 deletions src/Chapter06.Tests/Listing06.28.Tests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentException>(() => 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ await CompilerAssert.CompileAsync(
new string[] {
CompilerAssert.GetTargetFileNameToCompileFromTestFileName(),
"Listing06.28.DefiningAConstructor.cs" },
new string[] { "CS7036" });
["CS7036"]);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_30.Tests;

[TestClass]
Expand Down
Loading
Loading