Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/Chapter09.Tests/GlobalUsing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
29 changes: 29 additions & 0 deletions src/Chapter09.Tests/Listing09.01.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_01.Tests;

[TestClass]
public class AngleTests
{
[TestMethod]
public void Create_AngleArray_ElementsAreUninitialized()
{
Angle angle = default;
Angle[] angles = new Angle[1];
Assert.AreEqual(angle, angles[0]);
}

[TestMethod]
public void Equals_CopyWithSameDegreesMinutesSeconds_AreEqual()
{
Angle angle1 = new(0, 0, 0);
Angle angle2 = new(0, 0, 0);
Assert.AreEqual<Angle>(angle1, angle2);
}

[TestMethod]
public void With_CopyWithSameDegreesMinutesSeconds_AreEqual()
{
Angle angle1 = new(0, 0, 0);
Angle angle2 = angle1 with { };
Assert.AreEqual<Angle>(angle1, angle2);
}
}
15 changes: 15 additions & 0 deletions src/Chapter09.Tests/Listing09.02.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_02.Tests;

[TestClass]
public class AngleProgramTests
{
[TestMethod]
public void Create_Angle_Success()
{
string expected =
"Angle { Degrees = 90, Minutes = 0, Seconds = 0, Name = }";
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
}

}
93 changes: 93 additions & 0 deletions src/Chapter09.Tests/Listing09.04.DeclaringRecordClass.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_04.Tests;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_01;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_07;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public partial class CoordinateTests
{
[TestMethod]
public void With_Coordinate_IsReadOnly()
{
Coordinate coordinate1 = new(
new Angle(180, 0, 0), new Angle(180, 0, 0));
Coordinate coordinate2 = coordinate1 with { };

Assert.AreEqual<Coordinate>(coordinate1, coordinate2);
}

[TestMethod]
public void EqualityContract_GetType()
{
Coordinate coordinate1 = new(
new Angle(180, 0, 0), new Angle(180, 0, 0));
NamedCoordinate coordinate2 = new(
coordinate1.Longitude, coordinate1.Latitude, "Test");


Assert.AreNotEqual<Type>(coordinate1.GetType(), coordinate2.GetType());
}

record struct SampleStruct(int A, int B)
{
int C { get; set; }
}

[TestMethod]
public void GetHashCode_ChangeData_GetHashCodeChanges()
{
SampleStruct sample1 = new(1, 2);
int expected = sample1.GetHashCode();
sample1.A = 3;
Assert.AreNotEqual<int>(expected, sample1.GetHashCode());
}


[TestMethod]
public void GetHashCode_StoredInDictionary_NotFoundWhenChanged()
{
SampleStruct sample = new(1, 2);
Dictionary<SampleStruct, string> dictionary = new() { { sample, "" } };
sample.A = 3;
Assert.IsFalse(dictionary.ContainsKey(sample));
}


[TestMethod]
public void GetHashCode_OnTuplesStoredInDictionary_NotFoundWhenChanged()
{
(int A, int B) tuple = new(1, 2);
Dictionary<(int A, int B), string> dictionary = new() { { tuple, "" } };
tuple.A = 3;
Assert.IsFalse(dictionary.ContainsKey(tuple));
}

[TestMethod]
public void ToString_GivenAdditionalProperty_IgnoredInOutput()
{
SampleStruct sampleStruct = new(1, 2);
Assert.AreEqual<string>("SampleStruct { A = 1, B = 2 }", sampleStruct.ToString());
}

[TestMethod]
public void With_Instance_Equivalent()
{
Coordinate coordinate1 = new(
new Angle(180, 0, 0), new Angle(180, 0, 0));
Coordinate coordinate2 = coordinate1 with { };
Assert.AreEqual(coordinate1, coordinate2);
}

[TestMethod]
public void With_DifferentInstance_NotEquivalent()
{
Coordinate coordinate1 =
new(new Angle(180, 0, 0), new Angle(180, 0, 0));
Angle angle = new Angle(0, 0, 0);
Coordinate coordinate2 = coordinate1 with
{
Latitude = angle
};
Assert.AreNotEqual(coordinate1, coordinate2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_05.Tests;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_01;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_04;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_07;

[TestClass]
public class CoordinateTests
{
[TestMethod]
public void With_Coordinate_IsReadOnly()
{
Coordinate coordinate1 = new(
new Angle(180, 0, 0), new Angle(180, 0, 0));
Coordinate coordinate2 = new(
new Angle(180, 0, 0), new Angle(180, 0, 0));

Assert.AreEqual<Coordinate>(coordinate1, coordinate2);
}

[TestMethod]
public void EqualityContract_GetType()
{
Coordinate coordinate1 = new(
new Angle(180, 0, 0), new Angle(180, 0, 0));
NamedCoordinate geoCoordinate = new(
new Angle(180, 0, 0), new Angle(180, 0, 0), "GeoCoordinate");

Assert.AreEqual(coordinate1.ExternalEqualityContract, coordinate1.GetType());

Assert.AreEqual(geoCoordinate.ExternalEqualityContract, geoCoordinate.GetType());
Assert.AreEqual(((Coordinate)geoCoordinate).ExternalEqualityContract, ((Coordinate)geoCoordinate).GetType());
}
}
32 changes: 32 additions & 0 deletions src/Chapter09.Tests/Listing09.08.CustomizingARecord.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_08.Tests;

[TestClass]
public class AngleTests
{
[TestMethod]
public void ToString_90Degrees_90DegreesFormatted()
{
string expected = $"90° 0' 0\"";
string actual = new Angle(90, 0, 0).ToString();
Assert.AreEqual<string>(
expected,
actual);
}
[TestMethod]
public void ToString_90DegreesWithName_90DegreesFormatted()
{
string expected = $"RightAngle: 90° 0' 0\"";
string actual = new Angle(90, 0, 0, "RightAngle").ToString();
Assert.AreEqual<string>(
expected,
actual);
}

[TestMethod]
public void Equals_CopyWithSameDegreesMinutesSecondsBut_AreEqual()
{
Angle angle1 = new(0,0,0, "90 degrees");
Angle angle2 = new(0, 0, 0);
Assert.AreEqual<Angle>(angle1, angle2);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_04.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_09.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_06.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_11.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_07.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_12.Tests;

[TestClass]
public class ProgramTests
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_13.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_17.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Runtime.InteropServices;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_15.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_19.Tests;

[TestClass]
public class ProgramTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_17.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_21.Tests;

[TestClass]
public class ProgramTests
Expand Down
7 changes: 7 additions & 0 deletions src/Chapter09/Listing09.01.BasicStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_01;

#region INCLUDE
// Use the record struct construct to declare a value type
public readonly record struct Angle(
int Degrees, int Minutes, int Seconds, string? Name = null);
#endregion INCLUDE
39 changes: 0 additions & 39 deletions src/Chapter09/Listing09.01.DeclaringAStruct.cs

This file was deleted.

This file was deleted.

Loading