Skip to content

Commit

Permalink
test: Add Some Missing Tests from Chapter 7 (#605)
Browse files Browse the repository at this point in the history
Contributes to #12
  • Loading branch information
BenjaminMichaelis authored Dec 4, 2023
1 parent de8ca1f commit 3b045c1
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 11 deletions.
57 changes: 57 additions & 0 deletions src/Chapter07.Tests/Listing07.23.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_23.Tests;

[TestClass]
public class SolsticeHelperTests
{
[TestMethod]
public void IsSolstice_21DayWrongMonth_ReturnsFalse()
{
Assert.IsFalse(SolsticeHelper.IsSolstice(new DateTime(2019, 1, 21)));
}

[TestMethod]
public void IsSolstice_21DayRightMonth_ReturnsTrue()
{
Assert.IsTrue(SolsticeHelper.IsSolstice(new DateTime(2023, 12, 21)));
}

[TestMethod]
public void IsSolstice_20DayRightMonth_ReturnsTrue()
{
Assert.IsTrue(SolsticeHelper.IsSolstice(new DateTime(2024, 6, 21)));
}

[TestMethod]
public void IsSolstice_WrongDayRightMonth_ReturnsFalse()
{
Assert.IsFalse(SolsticeHelper.IsSolstice(new DateTime(2024, 6, 15)));
}

[TestMethod]
public void TryGetSolstice_21DayWrongMonth_ReturnsFalse()
{
Assert.IsFalse(SolsticeHelper.TryGetSolstice(new DateTime(2019, 1, 21), out string? solstice));
Assert.IsNull(solstice);
}

[TestMethod]
public void TryGetSolstice_21DayRightMonth_ReturnsTrueWinterSolstice()
{
Assert.IsTrue(SolsticeHelper.TryGetSolstice(new DateTime(2023, 12, 21), out string? solstice));
Assert.AreEqual("Winter Solstice", solstice);
}

[TestMethod]
public void TryGetSolstice_20DayRightMonth_ReturnsTrueSummerSolstice()
{
Assert.IsTrue(SolsticeHelper.TryGetSolstice(new DateTime(2024, 6, 21), out string? solstice));
Assert.AreEqual("Summer Solstice", solstice);
}

[TestMethod]
public void TryGetSolstice_WrongDayRightMonth_ReturnsFalse()
{
Assert.IsFalse(SolsticeHelper.TryGetSolstice(new DateTime(2024, 6, 15), out string? solstice));
Assert.IsNull(solstice);
}
}
47 changes: 47 additions & 0 deletions src/Chapter07.Tests/Listing07.24.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_24.Tests;

[TestClass]
public class PeriodsOfTheDayTests
{
[TestMethod]
public void IsDeveloperWorkHours_ValidWorkingHours_ReturnsTrue()
{
Assert.IsTrue(PeriodsOfTheDay.IsDeveloperWorkHours(12));
}

[TestMethod]
public void IsDeveloperWorkHours_ValidNonWorkingHours_ReturnsFalse()
{
Assert.IsFalse(PeriodsOfTheDay.IsDeveloperWorkHours(3));
}

[TestMethod]
public void GetPeriodOfDay_Dawn_ReturnsDawn()
{
Assert.AreEqual("Dawn", PeriodsOfTheDay.GetPeriodOfDay(5));
}

[TestMethod]
public void GetPeriodOfDay_Morning_ReturnsMorning()
{
Assert.AreEqual("Morning", PeriodsOfTheDay.GetPeriodOfDay(6));
}

[TestMethod]
public void GetPeriodOfDay_Afternoon_ReturnsAfternoon()
{
Assert.AreEqual("Afternoon", PeriodsOfTheDay.GetPeriodOfDay(13));
}

[TestMethod]
public void GetPeriodOfDay_Evening_ReturnsEvening()
{
Assert.AreEqual("Evening", PeriodsOfTheDay.GetPeriodOfDay(18));
}

[TestMethod]
public void GetPeriodOfDay_InvalidHour_ReturnsInvalid()
{
Assert.ThrowsException<ArgumentOutOfRangeException>(() => PeriodsOfTheDay.GetPeriodOfDay(25));
}
}
35 changes: 35 additions & 0 deletions src/Chapter07.Tests/Listing07.25.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_25.Tests;

[TestClass]
public class LogicalPatternMatchingTests
{
[TestMethod]
public void IsStandardWorkHours()
{
TimeOnly time = new(8, 0, 0);
Assert.IsFalse(PeriodsOfTheDay.IsStandardWorkHours(time));
time = new(9, 1, 0);
Assert.IsTrue(PeriodsOfTheDay.IsStandardWorkHours(time));
time = new(12, 0, 0);
Assert.IsFalse(PeriodsOfTheDay.IsStandardWorkHours(time));
time = new(13, 1, 0);
Assert.IsTrue(PeriodsOfTheDay.IsStandardWorkHours(time));
time = new(16, 59, 0);
Assert.IsTrue(PeriodsOfTheDay.IsStandardWorkHours(time));
time = new(17, 0, 0);
Assert.IsFalse(PeriodsOfTheDay.IsStandardWorkHours(time));
}

[TestMethod]
public void TryGetPhoneButton()
{
Assert.IsTrue(PeriodsOfTheDay.TryGetPhoneButton('a', out char? button));
Assert.AreEqual('2', button);
Assert.IsTrue(PeriodsOfTheDay.TryGetPhoneButton('z', out button));
Assert.AreEqual('9', button);
Assert.IsTrue(PeriodsOfTheDay.TryGetPhoneButton('+', out button));
Assert.AreEqual('0', button);
Assert.IsFalse(PeriodsOfTheDay.TryGetPhoneButton('=', out button));
Assert.IsNull(button);
}
}
35 changes: 35 additions & 0 deletions src/Chapter07.Tests/Listing07.26.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_26.Tests;

[TestClass]
public class ParenthesizedPatternsTests
{
[TestMethod]
public void IsOutsideOfStandardWorkHours()
{
TimeOnly time = new(8, 0, 0);
Assert.IsTrue(new PeriodsOfTheDay().IsOutsideOfStandardWorkHours(time));
time = new(9, 1, 0);
Assert.IsFalse(new PeriodsOfTheDay().IsOutsideOfStandardWorkHours(time));
time = new(12, 0, 0);
Assert.IsTrue(new PeriodsOfTheDay().IsOutsideOfStandardWorkHours(time));
time = new(13, 1, 0);
Assert.IsFalse(new PeriodsOfTheDay().IsOutsideOfStandardWorkHours(time));
time = new(16, 59, 0);
Assert.IsFalse(new PeriodsOfTheDay().IsOutsideOfStandardWorkHours(time));
time = new(17, 0, 0);
Assert.IsTrue(new PeriodsOfTheDay().IsOutsideOfStandardWorkHours(time));
}

[TestMethod]
public void TryGetPhoneButton()
{
Assert.IsTrue(PeriodsOfTheDay.TryGetPhoneButton('a', out char? button));
Assert.AreEqual('2', button);
Assert.IsTrue(PeriodsOfTheDay.TryGetPhoneButton('z', out button));
Assert.AreEqual('9', button);
Assert.IsTrue(PeriodsOfTheDay.TryGetPhoneButton('+', out button));
Assert.AreEqual('0', button);
Assert.IsFalse(PeriodsOfTheDay.TryGetPhoneButton('=', out button));
Assert.IsNull(button);
}
}
31 changes: 31 additions & 0 deletions src/Chapter07.Tests/Listing07.34.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using IntelliTect.TestTools.Console;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_34.Tests;

[TestClass]
public class DataConversionUsingTheAsOperatorTests
{
[TestMethod]
public void Load()
{
Contact contact = new("Inigo Montoya");
PdaItem pdaItem = contact;
Assert.AreEqual(contact, Contact.Load(pdaItem));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Load_InvalidType()
{
PdaItem pdaItem = new PdaItem();
Contact.Load(pdaItem);
}
[TestMethod]
public void LoadValidType_TestOutput()
{
Contact contact = new("Inigo Montoya");
PdaItem pdaItem = contact;
ConsoleAssert.Expect(
"ObjectKey: 00000000-0000-0000-0000-000000000000",
() => Contact.Load(pdaItem));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ public static TimeOnly GetTime(object input)
}
#endregion INCLUDE
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ DateTimeOffset datetimeOffset
$"Invalid type - {input.GetType().FullName}"),
};
#endregion INCLUDE

}

4 changes: 2 additions & 2 deletions src/Chapter07/Listing07.24.RelationalPatternWithIsOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public static string GetPeriodOfDay(int hourOfTheDay) =>
< 12 => "Morning",
< 18 => "Afternoon",
< 24 => "Evening",
int hour => throw new ArgumentOutOfRangeException(
$"The hour of the day specified is invalid.", nameof(hourOfTheDay))
int hour => throw new ArgumentOutOfRangeException(nameof(hourOfTheDay),
$"The hour of the day specified is invalid.")
};
#endregion INCLUDE
}
4 changes: 2 additions & 2 deletions src/Chapter07/Listing07.25.LogicalPatternExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_25;
public class PeriodsOfTheDay
{
#region INCLUDE
public bool IsStandardWorkHours(
public static bool IsStandardWorkHours(
TimeOnly time) =>
time.Hour is > 8
and < 17
and not 12; // lunch

static bool TryGetPhoneButton(
public static bool TryGetPhoneButton(
char character,
[NotNullWhen(true)] out char? button)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter07/Listing07.26.ParenthesizedPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ time.Hour is not
(> 8 and < 17 and not 12); // Parenthesis Pattern - C# 10.
#endregion INCLUDE

static bool TryGetPhoneButton(
public static bool TryGetPhoneButton(
char character,
[NotNullWhen(true)] out char? button)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ public class Contact : PdaItem

public static Contact Load(PdaItem pdaItem)
{
#pragma warning disable IDE0019 // Use pattern matching
Contact? contact = pdaItem as Contact;
#pragma warning restore IDE0019 // Use pattern matching
if (contact is not null)
{
System.Diagnostics.Trace.WriteLine(
Console.WriteLine(
$"ObjectKey: {contact.ObjectKey}");
return (Contact)pdaItem;
}
Expand Down

0 comments on commit 3b045c1

Please sign in to comment.