diff options
author | Jon Skeet <jonskeet@google.com> | 2016-01-04 14:03:01 +0000 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2016-01-11 09:34:43 +0000 |
commit | 9e4f354f14775061ed098c896170d3a2d01a3895 (patch) | |
tree | e7e6850910cbd00970c70c4514246c6b7ea93ac1 /csharp/src/Google.Protobuf.Test/Collections | |
parent | 5700a1054b081f425964b0b0e1535438da4b2ea1 (diff) | |
download | protobuf-9e4f354f14775061ed098c896170d3a2d01a3895.tar.gz protobuf-9e4f354f14775061ed098c896170d3a2d01a3895.tar.bz2 protobuf-9e4f354f14775061ed098c896170d3a2d01a3895.zip |
Prohibit null values in map fields
On deserialization, missing values for message types
are replaced with a "default" message.
Diffstat (limited to 'csharp/src/Google.Protobuf.Test/Collections')
-rw-r--r-- | csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs | 56 |
1 files changed, 2 insertions, 54 deletions
diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs index ba82c0e8..9c845907 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs @@ -56,7 +56,7 @@ namespace Google.Protobuf.Collections } [Test] - public void NullValues() + public void NullValuesProhibited() { TestNullValues<int?>(0); TestNullValues(""); @@ -65,19 +65,12 @@ namespace Google.Protobuf.Collections private void TestNullValues<T>(T nonNullValue) { - var map = new MapField<int, T>(false); + var map = new MapField<int, T>(); 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; - - // Doesn't throw... - map = new MapField<int, T>(true); - map.Add(0, nullValue); - map[0] = nullValue; - map.Add(1, nonNullValue); - map[1] = nonNullValue; } [Test] @@ -161,27 +154,6 @@ namespace Google.Protobuf.Collections } [Test] - public void EqualityHandlesNullValues() - { - var map1 = new MapField<string, ForeignMessage>(); - map1.Add("a", new ForeignMessage { C = 10 }); - map1.Add("b", null); - - var map2 = new MapField<string, ForeignMessage>(); - map2.Add("a", new ForeignMessage { C = 10 }); - map2.Add("b", null); - - EqualityTester.AssertEquality(map1, map2); - // Check the null value isn't ignored entirely... - Assert.IsTrue(map1.Remove("b")); - EqualityTester.AssertInequality(map1, map2); - map1.Add("b", new ForeignMessage()); - EqualityTester.AssertInequality(map1, map2); - map1["b"] = null; - EqualityTester.AssertEquality(map1, map2); - } - - [Test] public void Add_Dictionary() { var map1 = new MapField<string, string> @@ -454,30 +426,6 @@ namespace Google.Protobuf.Collections } [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); - } - - [Test] public void KeysReturnsLiveView() { var map = new MapField<string, string>(); |