| OLD | NEW |
| 1 #region Copyright notice and license | 1 #region Copyright notice and license |
| 2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
| 3 // Copyright 2015 Google Inc. All rights reserved. | 3 // Copyright 2015 Google Inc. All rights reserved. |
| 4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
| 5 // | 5 // |
| 6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
| 8 // met: | 8 // met: |
| 9 // | 9 // |
| 10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 public void Clone_ClonesMessages() | 49 public void Clone_ClonesMessages() |
| 50 { | 50 { |
| 51 var message = new ForeignMessage { C = 20 }; | 51 var message = new ForeignMessage { C = 20 }; |
| 52 var map = new MapField<string, ForeignMessage> { { "x", message } }; | 52 var map = new MapField<string, ForeignMessage> { { "x", message } }; |
| 53 var clone = map.Clone(); | 53 var clone = map.Clone(); |
| 54 map["x"].C = 30; | 54 map["x"].C = 30; |
| 55 Assert.AreEqual(20, clone["x"].C); | 55 Assert.AreEqual(20, clone["x"].C); |
| 56 } | 56 } |
| 57 | 57 |
| 58 [Test] | 58 [Test] |
| 59 public void NullValues() | 59 public void NullValuesProhibited() |
| 60 { | 60 { |
| 61 TestNullValues<int?>(0); | 61 TestNullValues<int?>(0); |
| 62 TestNullValues(""); | 62 TestNullValues(""); |
| 63 TestNullValues(new TestAllTypes()); | 63 TestNullValues(new TestAllTypes()); |
| 64 } | 64 } |
| 65 | 65 |
| 66 private void TestNullValues<T>(T nonNullValue) | 66 private void TestNullValues<T>(T nonNullValue) |
| 67 { | 67 { |
| 68 var map = new MapField<int, T>(false); | 68 var map = new MapField<int, T>(); |
| 69 var nullValue = (T) (object) null; | 69 var nullValue = (T) (object) null; |
| 70 Assert.Throws<ArgumentNullException>(() => map.Add(0, nullValue)); | 70 Assert.Throws<ArgumentNullException>(() => map.Add(0, nullValue)); |
| 71 Assert.Throws<ArgumentNullException>(() => map[0] = nullValue); | 71 Assert.Throws<ArgumentNullException>(() => map[0] = nullValue); |
| 72 map.Add(1, nonNullValue); | 72 map.Add(1, nonNullValue); |
| 73 map[1] = nonNullValue; | 73 map[1] = nonNullValue; |
| 74 | |
| 75 // Doesn't throw... | |
| 76 map = new MapField<int, T>(true); | |
| 77 map.Add(0, nullValue); | |
| 78 map[0] = nullValue; | |
| 79 map.Add(1, nonNullValue); | |
| 80 map[1] = nonNullValue; | |
| 81 } | 74 } |
| 82 | 75 |
| 83 [Test] | 76 [Test] |
| 84 public void Add_ForbidsNullKeys() | 77 public void Add_ForbidsNullKeys() |
| 85 { | 78 { |
| 86 var map = new MapField<string, ForeignMessage>(); | 79 var map = new MapField<string, ForeignMessage>(); |
| 87 Assert.Throws<ArgumentNullException>(() => map.Add(null, new Foreign
Message())); | 80 Assert.Throws<ArgumentNullException>(() => map.Add(null, new Foreign
Message())); |
| 88 } | 81 } |
| 89 | 82 |
| 90 [Test] | 83 [Test] |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 map1.Add("b", "second value"); | 147 map1.Add("b", "second value"); |
| 155 | 148 |
| 156 var map2 = new MapField<string, string>(); | 149 var map2 = new MapField<string, string>(); |
| 157 map2.Add("a", "third value"); | 150 map2.Add("a", "third value"); |
| 158 map2.Add("b", "fourth value"); | 151 map2.Add("b", "fourth value"); |
| 159 | 152 |
| 160 EqualityTester.AssertInequality(map1, map2); | 153 EqualityTester.AssertInequality(map1, map2); |
| 161 } | 154 } |
| 162 | 155 |
| 163 [Test] | 156 [Test] |
| 164 public void EqualityHandlesNullValues() | |
| 165 { | |
| 166 var map1 = new MapField<string, ForeignMessage>(); | |
| 167 map1.Add("a", new ForeignMessage { C = 10 }); | |
| 168 map1.Add("b", null); | |
| 169 | |
| 170 var map2 = new MapField<string, ForeignMessage>(); | |
| 171 map2.Add("a", new ForeignMessage { C = 10 }); | |
| 172 map2.Add("b", null); | |
| 173 | |
| 174 EqualityTester.AssertEquality(map1, map2); | |
| 175 // Check the null value isn't ignored entirely... | |
| 176 Assert.IsTrue(map1.Remove("b")); | |
| 177 EqualityTester.AssertInequality(map1, map2); | |
| 178 map1.Add("b", new ForeignMessage()); | |
| 179 EqualityTester.AssertInequality(map1, map2); | |
| 180 map1["b"] = null; | |
| 181 EqualityTester.AssertEquality(map1, map2); | |
| 182 } | |
| 183 | |
| 184 [Test] | |
| 185 public void Add_Dictionary() | 157 public void Add_Dictionary() |
| 186 { | 158 { |
| 187 var map1 = new MapField<string, string> | 159 var map1 = new MapField<string, string> |
| 188 { | 160 { |
| 189 { "x", "y" }, | 161 { "x", "y" }, |
| 190 { "a", "b" } | 162 { "a", "b" } |
| 191 }; | 163 }; |
| 192 var map2 = new MapField<string, string> | 164 var map2 = new MapField<string, string> |
| 193 { | 165 { |
| 194 { "before", "" }, | 166 { "before", "" }, |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 Assert.AreEqual("b", map["a"]); | 419 Assert.AreEqual("b", map["a"]); |
| 448 map["a"] = "c"; | 420 map["a"] = "c"; |
| 449 Assert.AreEqual("c", map["a"]); | 421 Assert.AreEqual("c", map["a"]); |
| 450 Assert.Throws<InvalidCastException>(() => dictionary[5] = "x"); | 422 Assert.Throws<InvalidCastException>(() => dictionary[5] = "x"); |
| 451 Assert.Throws<InvalidCastException>(() => dictionary["x"] = 5); | 423 Assert.Throws<InvalidCastException>(() => dictionary["x"] = 5); |
| 452 Assert.Throws<ArgumentNullException>(() => dictionary[null] = "z"); | 424 Assert.Throws<ArgumentNullException>(() => dictionary[null] = "z"); |
| 453 Assert.Throws<ArgumentNullException>(() => dictionary["x"] = null); | 425 Assert.Throws<ArgumentNullException>(() => dictionary["x"] = null); |
| 454 } | 426 } |
| 455 | 427 |
| 456 [Test] | 428 [Test] |
| 457 public void AllowNullValues_Property() | |
| 458 { | |
| 459 // Non-message reference type values are non-nullable by default, bu
t can be overridden | |
| 460 Assert.IsFalse(new MapField<int, string>().AllowsNullValues); | |
| 461 Assert.IsFalse(new MapField<int, string>(false).AllowsNullValues); | |
| 462 Assert.IsTrue(new MapField<int, string>(true).AllowsNullValues); | |
| 463 | |
| 464 // Non-nullable value type values are never nullable | |
| 465 Assert.IsFalse(new MapField<int, int>().AllowsNullValues); | |
| 466 Assert.IsFalse(new MapField<int, int>(false).AllowsNullValues); | |
| 467 Assert.Throws<ArgumentException>(() => new MapField<int, int>(true))
; | |
| 468 | |
| 469 // Message type values are nullable by default, but can be overridde
n | |
| 470 Assert.IsTrue(new MapField<int, TestAllTypes>().AllowsNullValues); | |
| 471 Assert.IsFalse(new MapField<int, TestAllTypes>(false).AllowsNullValu
es); | |
| 472 Assert.IsTrue(new MapField<int, TestAllTypes>(true).AllowsNullValues
); | |
| 473 | |
| 474 // Nullable value type values are nullable by default, but can be ov
erridden | |
| 475 Assert.IsTrue(new MapField<int, int?>().AllowsNullValues); | |
| 476 Assert.IsFalse(new MapField<int, int?>(false).AllowsNullValues); | |
| 477 Assert.IsTrue(new MapField<int, int?>(true).AllowsNullValues); | |
| 478 } | |
| 479 | |
| 480 [Test] | |
| 481 public void KeysReturnsLiveView() | 429 public void KeysReturnsLiveView() |
| 482 { | 430 { |
| 483 var map = new MapField<string, string>(); | 431 var map = new MapField<string, string>(); |
| 484 var keys = map.Keys; | 432 var keys = map.Keys; |
| 485 CollectionAssert.AreEqual(new string[0], keys); | 433 CollectionAssert.AreEqual(new string[0], keys); |
| 486 map["foo"] = "bar"; | 434 map["foo"] = "bar"; |
| 487 map["x"] = "y"; | 435 map["x"] = "y"; |
| 488 CollectionAssert.AreEqual(new[] { "foo", "x" }, keys); | 436 CollectionAssert.AreEqual(new[] { "foo", "x" }, keys); |
| 489 } | 437 } |
| 490 | 438 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 var map = new MapField<byte, string> { { 10, "foo" } }; | 523 var map = new MapField<byte, string> { { 10, "foo" } }; |
| 576 Assert.Throws<ArgumentException>(() => map.ToString()); | 524 Assert.Throws<ArgumentException>(() => map.ToString()); |
| 577 } | 525 } |
| 578 | 526 |
| 579 private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(
TKey key, TValue value) | 527 private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(
TKey key, TValue value) |
| 580 { | 528 { |
| 581 return new KeyValuePair<TKey, TValue>(key, value); | 529 return new KeyValuePair<TKey, TValue>(key, value); |
| 582 } | 530 } |
| 583 } | 531 } |
| 584 } | 532 } |
| OLD | NEW |