Apply suggestions from code review
Apply suggestions from code review
Minor corrections based on code review.
MODIFY
src/Chapter07.Tests/Listing07.26.TuplePatternMatchingWithIsOperator.Tests.cs
src/Chapter07.Tests/Listing07.26.TuplePatternMatchingWithIsOperator.Tests.cs
@@ -2,20 +2,12 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_26.Tests;
-[TestClass]
-public class ProgramTests
+static class TuplePatternMatchingTests
{
- string DataFile = "data.dat";
+ static string DataFile = "data.dat";
const string EncryptedFileName = "temp.out";
- [TestMethod]
- public async Task Main_EncryptFileUsingIsOperator_Success()
- {
- await InvokeMainWithEncriptAction(
- Program.Main);
- }
-
- private async Task InvokeMainWithEncriptAction(Action<string[]> action)
+ public static async Task InvokeMainWithEncriptAction(Action<string[]> action)
{
const string data = "DATA";
await File.WriteAllTextAsync(DataFile, data);
@@ -28,19 +20,30 @@ public class ProgramTests
Assert.AreEqual<string>(expected, actual);
}
- [TestMethod]
- public async Task Main_ShowFileWithIsOperator_Success()
- {
- await InvokeMainWithShowFile(Program.Main);
- }
-
- private async Task InvokeMainWithShowFile(Action<string[]> action)
+ public static async Task InvokeMainWithShowFile(Action<string[]> action)
{
const string data = "DATA";
await File.WriteAllTextAsync(DataFile, data);
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
data,
- () => action(new string[] { "show" }));
+ () => action(new string[] { "cat" }));
+ }
+}
+
+[TestClass]
+public class ProgramTests
+{
+ [TestMethod]
+ public async Task Main_EncryptFileUsingIsOperator_Success()
+ {
+ await TuplePatternMatchingTests.InvokeMainWithEncriptAction(
+ Program.Main);
+ }
+
+ [TestMethod]
+ public async Task Main_ShowFileWithIsOperator_Success()
+ {
+ await TuplePatternMatchingTests.InvokeMainWithShowFile(Program.Main);
}
}

MODIFY
src/Chapter07.Tests/Listing07.27.TuplePatternMatchingWithSwitchStatement.Tests.cs
src/Chapter07.Tests/Listing07.27.TuplePatternMatchingWithSwitchStatement.Tests.cs
+using AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_26.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_27.Tests;
@@ -11,37 +12,13 @@ public class ProgramTests
[TestMethod]
public async Task Main_EncryptFileUsingSwitchStatement_Success()
{
- await InvokeMainWithEncriptAction(
+ await TuplePatternMatchingTests.InvokeMainWithEncriptAction(
Program.Main);
}
-
- private async Task InvokeMainWithEncriptAction(Action<string[]> action)
- {
- const string data = "DATA";
- await File.WriteAllTextAsync(DataFile, data);
- string expected = $"ENCRYPTED <{data}> ENCRYPTED";
-
-
- action(new string[] { "Encrypt", EncryptedFileName });
-
- string actual = await File.ReadAllTextAsync(EncryptedFileName);
- Assert.AreEqual<string>(expected, actual);
- }
-
[TestMethod]
public async Task Main_ShowFileWithSwitchStatement_Success()
{
- await InvokeMainWithShowFile(Program.Main);
- }
-
- private async Task InvokeMainWithShowFile(Action<string[]> action)
- {
- const string data = "DATA";
- await File.WriteAllTextAsync(DataFile, data);
-
- IntelliTect.TestTools.Console.ConsoleAssert.Expect(
- data,
- () => action(new string[] { "show" }));
+ await TuplePatternMatchingTests.InvokeMainWithShowFile(Program.Main);
}
}

MODIFY
src/Chapter07.Tests/Listing07.32.Tests.cs
src/Chapter07.Tests/Listing07.32.Tests.cs
@@ -48,24 +48,17 @@ public class ProgramTests
}
}
- private static List<string> PrefixCommands(string command)
+ private static IEnumerable<string> PrefixCommands(string command)
{
- List<string> commands = new();
-
- string[] shortPrefix = new[] { "/", "-" };
-
if (command.Length == 1)
{
- foreach (string item in shortPrefix)
- {
- commands.Add($"{item}{command}");
- }
+ yield return $"/{command}";
+ yield return $"-{command}";
}
else
{
- commands.Add($"--{command}");
+ yield return $"--{command}";
}
- return commands;
}

MODIFY
src/Chapter07/Chapter07.csproj
src/Chapter07/Chapter07.csproj
<PropertyGroup>
<!--Listing the warning here causes it to be a warning rather than an error.-->
<!--CS0108 hides inherited member warning where 'new' is advised.-->
- <ExpectedWarnings>$(ExpectedWarnings)CS0108,</ExpectedWarnings>
+ <!--IDE0170 Property pattern can be simplified (to use expanded property pattern) -->
+ <ExpectedWarnings>$(ExpectedWarnings)CS0108,IDE0170</ExpectedWarnings>
<IgnoredWarnings>$(IgnoredWarnings)</IgnoredWarnings>
</PropertyGroup>
<Import Project="..\Chapter.props" />

MODIFY
src/Chapter07/Listing07.19.TypeCheckingWithIsOperator.cs
src/Chapter07/Listing07.19.TypeCheckingWithIsOperator.cs
@@ -39,7 +39,7 @@ public class Program
if (id.StartsWith(nameof(Employee)))
{
- return new Employee("Inigo", "Montoya", id.Replace(nameof(Employee), ""));
+ return new Employee("Inigo", "Montoya", id.Remove(0,nameof(Employee).Length));
}
else if (id.StartsWith(nameof(Person)))
{

MODIFY
src/Chapter07/Listing07.20.SwitchStatement.cs
src/Chapter07/Listing07.20.SwitchStatement.cs
@@ -13,7 +13,7 @@ public class TimeOnlyHelper
case string dateText:
return TimeOnly.Parse(dateText);
case null:
- throw new Exception();
+ throw new ArgumentNullException(nameof(input));
default:
throw new ArgumentException(
$"Invalid type - {input.GetType().FullName}");

MODIFY
src/Chapter07/Listing07.21.UsingSwtichExpressionToReturnValue.cs
src/Chapter07/Listing07.21.UsingSwtichExpressionToReturnValue.cs
@@ -12,7 +12,7 @@ public class TimeOnlyHelper
=> TimeOnly.FromDateTime(datetimeOffset.DateTime),
string dateText => TimeOnly.Parse(
dateText),
- null => throw new Exception(),
+ null => throw new ArgumentNullException(nameof(input)),
_ => throw new ArgumentException(
$"Invalid type - {input.GetType().FullName}"),
};

MODIFY
src/Chapter07/Listing07.22.ConstantPatternWithSwitchExpression.cs
src/Chapter07/Listing07.22.ConstantPatternWithSwitchExpression.cs
@@ -26,7 +26,7 @@ public static class SolisticeHelper
[NotNullWhen(true)] out string? solstice
)
{
- if ((decimal)date.Day is 21)
+ if (date.Day is 21)
{
if ((solstice = date.Month switch
{

MODIFY
src/Chapter07/Listing07.26.TuplePatternMatchingWithIsOperator.cs
src/Chapter07/Listing07.26.TuplePatternMatchingWithIsOperator.cs
@@ -16,7 +16,7 @@ public class Program
// ...
#region HIGHLIGHT
- if ((args.Length, args[command]) is (1, "show"))
+ if ((args.Length, args[command]) is (1, "cat"))
#endregion HIGHLIGHT
{
Console.WriteLine(File.ReadAllText(dataFile));

MODIFY
src/Chapter07/Listing07.27.TuplePatternMatchingWithSwitchStatement.cs
src/Chapter07/Listing07.27.TuplePatternMatchingWithSwitchStatement.cs
@@ -13,7 +13,7 @@ public class Program
#region HIGHLIGHT
switch ((args.Length, args[action]))
{
- case (1, "show"):
+ case (1, "cat"):
Console.WriteLine(File.ReadAllText(dataFile));
break;
case (2, "encrypt"):

MODIFY
src/Chapter07/Listing07.29.PropertyPatternMatching.cs
src/Chapter07/Listing07.29.PropertyPatternMatching.cs
@@ -18,7 +18,7 @@ public class ExpenseItem
public int Id { get; set; }
public string ItemName { get; set; }
public decimal CostAmount { get; set; }
- public DateTime ExpenseDate { get; set; } = DateTime.Now;
+ public DateTime ExpenseDate { get; set; }
public Employee Employee { get; set; }
public ExpenseItem(

MODIFY
src/Chapter07/Listing07.32.ListPatternMatching.cs
src/Chapter07/Listing07.32.ListPatternMatching.cs
@@ -45,7 +45,7 @@ public class Program
}
}
- static bool ExecuteCommand(string command, string[] args) =>
+ private static bool ExecuteCommand(string command, string[] args) =>
(command, args) switch
{
("cat" or "c", [string fileName]) =>

Apply suggestions from code review

Co-authored-by: Kevin B <Keboo@users.noreply.github.com>
Apply suggestions from code review

Co-authored-by: Kevin B <Keboo@users.noreply.github.com>
Minor corrections based on code review.