public void TryGetValue_NullKey_ThrowsArgumentNullException() { var collection = new StringKeyedCollection <string>(); string item = "item"; AssertExtensions.Throws <ArgumentNullException>("key", () => collection.TryGetValue(null, out item)); Assert.Equal("item", item); }
public void TryGetValue_Invoke_ReturnsExpected(IEqualityComparer <string> comparer, string key, bool expected, string expectedItem) { var collection = new StringKeyedCollection <string>(comparer, 3); collection.GetKeyForItemHandler = i => i + "_key"; // Without dictionary. collection.InsertItem(0, "first"); Assert.Equal(expected, collection.TryGetValue(key, out string item)); Assert.Equal(expectedItem, item); // With dictionary. collection.InsertItem(0, "second"); collection.InsertItem(0, "third"); collection.InsertItem(0, "fourth"); Assert.Equal(expected, collection.TryGetValue(key, out item)); Assert.Equal(expectedItem, item); }
public void TryGetValue_NullKeyForItemResult_Success(int dictionaryCreationThreshold, bool expected, int expectedItem) { var collection = new StringKeyedCollection <int>(null, dictionaryCreationThreshold); collection.GetKeyForItemHandler = i => i.ToString(); collection.Add(1); collection.Add(2); collection.Add(3); collection.Add(4); // Don't get even numbers. collection.GetKeyForItemHandler = i => i % 2 == 0 ? null : i.ToString(); // Get null key. Assert.Equal(expected, collection.TryGetValue("2", out int item)); Assert.Equal(expectedItem, item); // Get non null key. Assert.True(collection.TryGetValue("1", out item)); Assert.Equal(1, item); }
public void TryGetValue_DifferentKeyForItemResult_Success(int dictionaryCreationThreshold, bool expected, int expectedItem) { var collection = new StringKeyedCollection <int>(null, dictionaryCreationThreshold); collection.GetKeyForItemHandler = i => i.ToString(); collection.Add(1); collection.Add(2); collection.Add(3); collection.Add(4); collection.GetKeyForItemHandler = i => (i * 2).ToString(); Assert.Equal(expected, collection.TryGetValue("6", out int item)); Assert.Equal(expectedItem, item); }
private void PushOperations(XElement element) { foreach (Operation operation in CreateOperationsFromElement(element)) { _propertyOperations = _propertyOperations ?? new StringKeyedCollection <PropertyOperationCollection>(); if (!_propertyOperations.TryGetValue(operation.PropertyName, out PropertyOperationCollection propertyOperations)) { propertyOperations = new PropertyOperationCollection(operation.PropertyDefinition); _propertyOperations.Add(propertyOperations); } propertyOperations.Add(operation); } }