Tiny Types part 2–adding IEquatable
This is a series
- http://msprogrammer.serviciipeweb.ro/2018/03/12/tiny-types-in-cpart-1/
- http://msprogrammer.serviciipeweb.ro/2018/03/19/tiny-types-part-2adding-iequatable/
-
http://msprogrammer.serviciipeweb.ro/2018/03/26/tinytypesadding-equality-operatorpart-3/
-
http://msprogrammer.serviciipeweb.ro/2018/04/02/tiny-typesdocumentationpart-4/
As always, the bigger problem is adding equality. The Tiny Type should be equal with the inner value – and with the other type with the same value. And, in C#, when you implement equality , there is a whole theory – see https://msdn.microsoft.com/en-us/library/336aedhh(v=vs.100).aspx .
So the code to define equality is 60 lines long just for defining equality for
public class TinyType<T>:IEquatable<T>, IEquatable<TinyType<T>>
But this is not all. This is the code for testing equality
using Microsoft.VisualStudio.TestTools.UnitTesting; using TinyTypesObjects; namespace TinyTypesTest { [TestClass] public class TestTinyTypeEquals { [TestMethod] public void TestSimpleStringEquals() { #region arrange string s = "http://msprogrammer.serviciipeweb.ro"; #endregion #region act TinyType<string> tt1 = s; TinyType<string> tt2 = s; #endregion #region assert Assert.IsTrue(tt1.Equals(tt2)); Assert.AreEqual(tt1, tt2); Assert.AreEqual<string>(tt1, tt2); Assert.AreEqual<TinyType<string>>(tt1, tt2); Assert.AreEqual<string>(s, tt2); #endregion } [TestMethod] public void TestSimpleStringWithNull() { #region arrange string s = null; #endregion #region act TinyType<string> tt1 = s; TinyType<string> tt2 = null; #endregion #region assert Assert.IsFalse(tt1.Equals(tt2)); Assert.AreNotEqual(tt1, tt2); Assert.AreEqual<string>(tt1, tt2); Assert.AreNotEqual<TinyType<string>>(tt1, tt2); Assert.AreEqual<string>(s, tt2); #endregion } [TestMethod] public void TestSimpleStringNull() { #region arrange string s = null; #endregion #region act TinyType<string> tt1 = s; TinyType<string> tt2 = s; #endregion #region assert Assert.IsTrue(tt1.Equals(tt2)); Assert.AreEqual(tt1, tt2); Assert.AreEqual<string>(tt1, tt2); Assert.AreEqual<TinyType<string>>(tt1, tt2); Assert.AreEqual<string>(s, tt2); #endregion } [TestMethod] public void TestSimpleIntEquals() { #region arrange int s = 1; #endregion #region act TinyType<int> tt1 = s; TinyType<int> tt2 = s; #endregion #region assert Assert.IsTrue(tt1.Equals(tt2)); Assert.AreEqual(tt1, tt2); Assert.AreEqual<int>(tt1, tt2); Assert.AreEqual<TinyType<int>>(tt1, tt2); Assert.AreEqual<int>(s, tt2); #endregion } } }
Leave a Reply