Commit 043e1d1

mo khan <mo@mokhan.ca>
2011-03-18 18:47:33
add value type class.
1 parent d1d1894
Changed files (4)
product/testing/unit/utility/ValueTypeSpecs.cs
@@ -0,0 +1,43 @@
+using System;
+using gorilla.utility;
+using Machine.Specifications;
+
+namespace specs.unit.utility
+{
+    public class ValueTypeSpecs
+    {
+        public class when_two_different_instances_of_the_same_type_have_the_same_values
+        {
+            It should_consider_them_equal = () =>
+            {
+                var birthDate = DateTime.Today;
+                new TestType {first = "mo", BirthDate = birthDate}
+                    .ShouldEqual(new TestType {first = "mo", BirthDate = birthDate});
+            };
+        }
+
+        public class when_comparing_a_single_instance
+        {
+            It should_consider_them_equal = () =>
+            {
+                var instance = new TestType {first = "mo", BirthDate = DateTime.Today};
+                instance.ShouldEqual(instance);
+            };
+        }
+
+        public class when_two_different_instances_of_the_same_type_have_different_values
+        {
+            It should_consider_them_equal = () =>
+            {
+                new TestType {first = "mo", BirthDate = DateTime.Today}
+                    .ShouldNotEqual(new TestType {first = "mo", BirthDate = DateTime.Today.AddDays(-1)});
+            };
+        }
+
+        class TestType : ValueType<TestType>
+        {
+            public string first { get; set; }
+            public DateTime BirthDate { get; set; }
+        }
+    }
+}
\ No newline at end of file
product/testing/specs.csproj
@@ -94,6 +94,7 @@
     <Compile Include="unit\utility\PredicateSpecificationSpecs.cs" />
     <Compile Include="unit\utility\SpecificationExtensionsSpecs.cs" />
     <Compile Include="unit\utility\TypeExtensionsSpecs.cs" />
+    <Compile Include="unit\utility\ValueTypeSpecs.cs" />
     <Compile Include="unit\utility\VisitorExtensions.cs" />
   </ItemGroup>
   <ItemGroup>
product/utility/utility.csproj
@@ -104,6 +104,7 @@
     <Compile Include="State.cs" />
     <Compile Include="SubjectOf.cs" />
     <Compile Include="ValueReturningVisitor.cs" />
+    <Compile Include="ValueType.cs" />
     <Compile Include="Visitable.cs" />
     <Compile Include="Visitor.cs" />
     <Compile Include="AnonymousMapper.cs" />
product/utility/ValueType.cs
@@ -0,0 +1,37 @@
+using System.Linq;
+
+namespace gorilla.utility
+{
+    public class ValueType<T>
+    {
+        public override bool Equals(object obj)
+        {
+            if (ReferenceEquals(null, obj)) return false;
+            if (ReferenceEquals(this, obj)) return true;
+            if (!(obj is ValueType<T>)) return false;
+            return Equals((ValueType<T>) obj);
+        }
+
+        public bool Equals(ValueType<T> other)
+        {
+            if (ReferenceEquals(null, other)) return false;
+            if (ReferenceEquals(this, other)) return true;
+            return PropertiesMatch(other);
+        }
+
+        bool PropertiesMatch(ValueType<T> other)
+        {
+            return !GetType().GetProperties().Any(x =>
+            {
+                var thisValue = x.GetValue(this, null);
+                var otherValue = x.GetValue(other, null);
+                return !thisValue.Equals(otherValue);
+            });
+        }
+
+        public override int GetHashCode()
+        {
+            return GetType().GetProperties().Aggregate(0, (prev, prop) => (prev*397) ^ prop.GetHashCode());
+        }
+    }
+}
\ No newline at end of file