[C] throw meaningful exception on duplicate RD key (#716)
authorStephane Delcroix <stephane@delcroix.org>
Wed, 1 Feb 2017 20:17:15 +0000 (21:17 +0100)
committerKangho Hur <kangho.hur@samsung.com>
Fri, 24 Mar 2017 04:17:13 +0000 (13:17 +0900)
Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs
Xamarin.Forms.Core/ResourceDictionary.cs

index e5b8b60..9a2c6f0 100644 (file)
@@ -292,5 +292,19 @@ namespace Xamarin.Forms.Core.UnitTests
                        Assert.True(rd0.TryGetMergedValue("foo", out _));
                        Assert.AreEqual("Foo", _);
                }
+
+               [Test]
+               public void ThrowOnDuplicateKey()
+               {
+                       var rd0 = new ResourceDictionary();
+                       rd0.Add("foo", "Foo");
+                       try {
+                               rd0.Add("foo", "Bar");
+                       } catch (ArgumentException ae) {
+                               Assert.AreEqual("A resource with the key 'foo' is already present in the ResourceDictionary.", ae.Message);
+                               Assert.Pass();
+                       }
+                       Assert.Fail();
+               }
        }
 }
\ No newline at end of file
index ed3ea7f..a213dee 100644 (file)
@@ -72,6 +72,8 @@ namespace Xamarin.Forms
 
                public void Add(string key, object value)
                {
+                       if (ContainsKey(key))
+                               throw new ArgumentException($"A resource with the key '{key}' is already present in the ResourceDictionary.");
                        _innerDictionary.Add(key, value);
                        OnValueChanged(key, value);
                }