|
From: <fli...@us...> - 2010-02-10 01:10:36
|
Revision: 346
http://structuremap.svn.sourceforge.net/structuremap/?rev=346&view=rev
Author: flimflan
Date: 2010-02-10 00:14:42 +0000 (Wed, 10 Feb 2010)
Log Message:
-----------
Fix bug in Registry equality.
Two different instances of Registry are not considered equal.
Two different instances of the same Registry derived type are considered equal.
Modified Paths:
--------------
trunk/Source/StructureMap/Configuration/DSL/Registry.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs
Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2010-02-05 03:06:42 UTC (rev 345)
+++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2010-02-10 00:14:42 UTC (rev 346)
@@ -417,31 +417,27 @@
return (type.GetConstructor(new Type[0]) != null);
}
- public bool Equals(Registry obj)
+ public bool Equals(Registry other)
{
- if (ReferenceEquals(null, obj)) return false;
- return GetType().Equals(obj.GetType());
+ if (ReferenceEquals(null, other)) return false;
+ if (ReferenceEquals(this, other)) return true;
+ if(other.GetType() == typeof(Registry) && GetType() == typeof(Registry)) return false;
+ return Equals(other.GetType(), GetType());
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
-
- if (obj is Registry) return false;
-
-
- if (obj.GetType() != typeof (Registry)) return false;
+ if (!typeof (Registry).IsAssignableFrom(obj.GetType())) return false;
return Equals((Registry) obj);
}
public override int GetHashCode()
{
- return 0;
+ return GetType().GetHashCode();
}
- #region Nested type: BuildWithExpression
-
/// <summary>
/// Define the constructor and setter arguments for the default T
/// </summary>
@@ -457,7 +453,5 @@
public SmartInstance<T> Configure { get { return _instance; } }
}
-
- #endregion
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2010-02-05 03:06:42 UTC (rev 345)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2010-02-10 00:14:42 UTC (rev 346)
@@ -1,4 +1,5 @@
using System;
+using System.Diagnostics;
using NUnit.Framework;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
@@ -63,20 +64,37 @@
}
[Test]
- public void Equals_check_true()
+ public void an_instance_of_the_base_registry_is_equal_to_itself()
{
+ var registry1 = new Registry();
+
+ registry1.Equals((object)registry1).ShouldBeTrue();
+ }
+
+ [Test]
+ public void two_instances_of_the_base_registry_type_are_not_considered_equal()
+ {
+ var registry1 = new Registry();
+ var registry2 = new Registry();
+
+ registry1.Equals((object)registry2).ShouldBeFalse();
+ }
+
+ [Test]
+ public void two_instances_of_a_derived_registry_type_are_considered_equal()
+ {
var registry1 = new TestRegistry();
var registry2 = new TestRegistry();
var registry3 = new TestRegistry2();
var registry4 = new TestRegistry2();
- registry1.Equals(registry1).ShouldBeTrue();
- registry1.Equals(registry2).ShouldBeTrue();
- registry2.Equals(registry1).ShouldBeTrue();
- registry3.Equals(registry4).ShouldBeTrue();
+ registry1.Equals((object)registry1).ShouldBeTrue();
+ registry1.Equals((object)registry2).ShouldBeTrue();
+ registry2.Equals((object)registry1).ShouldBeTrue();
+ registry3.Equals((object)registry4).ShouldBeTrue();
- registry1.Equals(registry3).ShouldBeFalse();
- registry3.Equals(registry1).ShouldBeFalse();
+ registry1.Equals((object)registry3).ShouldBeFalse();
+ registry3.Equals((object)registry1).ShouldBeFalse();
}
[Test]
@@ -94,7 +112,46 @@
container.GetAllInstances<IWidget>().Count.ShouldEqual(5);
}
+ public class MutatedWidget : IWidget
+ {
+ public void DoSomething() { }
+ }
+
+ public class MutatingRegistry : Registry
+ {
+ private static int count = 0;
+
+ public MutatingRegistry()
+ {
+ For<IWidget>().Use<AWidget>();
+
+ if(count++ >= 1)
+ {
+ For<IWidget>().Use<MutatedWidget>();
+ }
+ }
+ }
+
[Test]
+ public void include_an_existing_registry_should_not_reevaluate_the_registry()
+ {
+ var registry1 = new Registry();
+ registry1.IncludeRegistry<MutatingRegistry>();
+
+ var registry2 = new Registry();
+ registry2.IncludeRegistry<MutatingRegistry>();
+
+ var container = new Container(config =>
+ {
+ config.AddRegistry(registry1);
+ config.AddRegistry(registry2);
+ });
+
+ container.GetInstance<IWidget>().ShouldBeOfType<AWidget>();
+ }
+
+
+ [Test]
public void Latch_on_a_PluginGraph()
{
var registry2 = new TestRegistry2();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|