diff options
author | Jon Skeet <jonskeet@google.com> | 2015-07-16 17:03:06 +0100 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2015-07-16 17:03:06 +0100 |
commit | 8a0312b20156aa4df092cb6b3c665ef48cb6fa54 (patch) | |
tree | 8a5864fa5e8481ed8b04a25404cf51bc1818a37e /csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs | |
parent | 8d47ec4f3e3368c5f4e7ac195f20978abc8a692f (diff) | |
download | protobuf-8a0312b20156aa4df092cb6b3c665ef48cb6fa54.tar.gz protobuf-8a0312b20156aa4df092cb6b3c665ef48cb6fa54.tar.bz2 protobuf-8a0312b20156aa4df092cb6b3c665ef48cb6fa54.zip |
First pass at wrapper types.
- We do still generate the message types, as otherwise reflection breaks, even though it doesn't actually use those types.
- JSON handling hasn't been implemented yet
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs | 85 |
1 files changed, 45 insertions, 40 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs b/csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs index d43bed3e..46d3bd9a 100644 --- a/csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs +++ b/csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs @@ -103,32 +103,35 @@ namespace Google.Protobuf.Collections } [Test] - public void Add_ForbidsNullKeys() + public void NullValues() { - var map = new MapField<string, ForeignMessage>(); - Assert.Throws<ArgumentNullException>(() => map.Add(null, new ForeignMessage())); + TestNullValues<int?>(0); + TestNullValues(""); + TestNullValues(new TestAllTypes()); } - [Test] - public void Add_AcceptsNullMessageValues() + private void TestNullValues<T>(T nonNullValue) { - var map = new MapField<string, ForeignMessage>(); - map.Add("missing", null); - Assert.IsNull(map["missing"]); - } + var map = new MapField<int, T>(false); + var nullValue = (T) (object) null; + Assert.Throws<ArgumentNullException>(() => map.Add(0, nullValue)); + Assert.Throws<ArgumentNullException>(() => map[0] = nullValue); + map.Add(1, nonNullValue); + map[1] = nonNullValue; - [Test] - public void Add_ForbidsNullStringValues() - { - var map = new MapField<string, string>(); - Assert.Throws<ArgumentNullException>(() => map.Add("missing", null)); + // Doesn't throw... + map = new MapField<int, T>(true); + map.Add(0, nullValue); + map[0] = nullValue; + map.Add(1, nonNullValue); + map[1] = nonNullValue; } [Test] - public void Add_ForbidsNullByteStringValues() + public void Add_ForbidsNullKeys() { - var map = new MapField<string, ByteString>(); - Assert.Throws<ArgumentNullException>(() => map.Add("missing", null)); + var map = new MapField<string, ForeignMessage>(); + Assert.Throws<ArgumentNullException>(() => map.Add(null, new ForeignMessage())); } [Test] @@ -137,29 +140,7 @@ namespace Google.Protobuf.Collections var map = new MapField<string, ForeignMessage>(); Assert.Throws<ArgumentNullException>(() => map[null] = new ForeignMessage()); } - - [Test] - public void Indexer_AcceptsNullMessageValues() - { - var map = new MapField<string, ForeignMessage>(); - map["missing"] = null; - Assert.IsNull(map["missing"]); - } - - [Test] - public void Indexer_ForbidsNullStringValues() - { - var map = new MapField<string, string>(); - Assert.Throws<ArgumentNullException>(() => map["missing"] = null); - } - - [Test] - public void Indexer_ForbidsNullByteStringValues() - { - var map = new MapField<string, ByteString>(); - Assert.Throws<ArgumentNullException>(() => map["missing"] = null); - } - + [Test] public void AddPreservesInsertionOrder() { @@ -528,6 +509,30 @@ namespace Google.Protobuf.Collections Assert.Throws<NotSupportedException>(() => dictionary["a"] = "c"); } + [Test] + public void AllowNullValues_Property() + { + // Non-message reference type values are non-nullable by default, but can be overridden + Assert.IsFalse(new MapField<int, string>().AllowsNullValues); + Assert.IsFalse(new MapField<int, string>(false).AllowsNullValues); + Assert.IsTrue(new MapField<int, string>(true).AllowsNullValues); + + // Non-nullable value type values are never nullable + Assert.IsFalse(new MapField<int, int>().AllowsNullValues); + Assert.IsFalse(new MapField<int, int>(false).AllowsNullValues); + Assert.Throws<ArgumentException>(() => new MapField<int, int>(true)); + + // Message type values are nullable by default, but can be overridden + Assert.IsTrue(new MapField<int, TestAllTypes>().AllowsNullValues); + Assert.IsFalse(new MapField<int, TestAllTypes>(false).AllowsNullValues); + Assert.IsTrue(new MapField<int, TestAllTypes>(true).AllowsNullValues); + + // Nullable value type values are nullable by default, but can be overridden + Assert.IsTrue(new MapField<int, int?>().AllowsNullValues); + Assert.IsFalse(new MapField<int, int?>(false).AllowsNullValues); + Assert.IsTrue(new MapField<int, int?>(true).AllowsNullValues); + } + private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(TKey key, TValue value) { return new KeyValuePair<TKey, TValue>(key, value); |