diff options
author | Jon Skeet <jonskeet@google.com> | 2015-08-08 07:17:58 +0100 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2015-08-08 08:59:53 +0100 |
commit | 3f45d7c11e200184cfc09fb92efe25a63df1eef1 (patch) | |
tree | c69a46147733f1bde5d70ecbf2c49f86e8924ee3 /csharp/src/Google.Protobuf.Test | |
parent | e58cdbd2146bc8e2e2f5bf4bc8a7576f82535ae6 (diff) | |
download | protobuf-3f45d7c11e200184cfc09fb92efe25a63df1eef1.tar.gz protobuf-3f45d7c11e200184cfc09fb92efe25a63df1eef1.tar.bz2 protobuf-3f45d7c11e200184cfc09fb92efe25a63df1eef1.zip |
Implement Keys and Values as views
Diffstat (limited to 'csharp/src/Google.Protobuf.Test')
-rw-r--r-- | csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs index c62ac046..08f1a19c 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs @@ -477,6 +477,77 @@ namespace Google.Protobuf.Collections Assert.IsTrue(new MapField<int, int?>(true).AllowsNullValues); } + [Test] + public void KeysReturnsLiveView() + { + var map = new MapField<string, string>(); + var keys = map.Keys; + CollectionAssert.AreEqual(new string[0], keys); + map["foo"] = "bar"; + map["x"] = "y"; + CollectionAssert.AreEqual(new[] { "foo", "x" }, keys); + } + + [Test] + public void ValuesReturnsLiveView() + { + var map = new MapField<string, string>(); + var values = map.Values; + CollectionAssert.AreEqual(new string[0], values); + map["foo"] = "bar"; + map["x"] = "y"; + CollectionAssert.AreEqual(new[] { "bar", "y" }, values); + } + + // Just test keys - we know the implementation is the same for values + [Test] + public void ViewsAreReadOnly() + { + var map = new MapField<string, string>(); + var keys = map.Keys; + Assert.IsTrue(keys.IsReadOnly); + Assert.Throws<NotSupportedException>(() => keys.Clear()); + Assert.Throws<NotSupportedException>(() => keys.Remove("a")); + Assert.Throws<NotSupportedException>(() => keys.Add("a")); + } + + // Just test keys - we know the implementation is the same for values + [Test] + public void ViewCopyTo() + { + var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } }; + var keys = map.Keys; + var array = new string[4]; + Assert.Throws<ArgumentException>(() => keys.CopyTo(array, 3)); + Assert.Throws<ArgumentOutOfRangeException>(() => keys.CopyTo(array, -1)); + keys.CopyTo(array, 1); + CollectionAssert.AreEqual(new[] { null, "foo", "x", null }, array); + } + + [Test] + public void KeysContains() + { + var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } }; + var keys = map.Keys; + Assert.IsTrue(keys.Contains("foo")); + Assert.IsFalse(keys.Contains("bar")); // It's a value! + Assert.IsFalse(keys.Contains("1")); + // Keys can't be null, so we should prevent contains check + Assert.Throws<ArgumentNullException>(() => keys.Contains(null)); + } + + [Test] + public void ValuesContains() + { + var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } }; + var values = map.Values; + Assert.IsTrue(values.Contains("bar")); + Assert.IsFalse(values.Contains("foo")); // It's a key! + Assert.IsFalse(values.Contains("1")); + // Values can be null, so this makes sense + Assert.IsFalse(values.Contains(null)); + } + private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(TKey key, TValue value) { return new KeyValuePair<TKey, TValue>(key, value); |