コード例 #1
0
        public void StringComparer()
        {
            var collection =
                new TestKeyedCollectionOfIKeyedItem <string, int>(
                    System.StringComparer.OrdinalIgnoreCase)
            {
                new KeyedItem <string, int>("foo", 0),
                new KeyedItem <string, int>("bar", 1)
            };

            AssertExtensions.Throws <ArgumentException>("key", null, () => collection.Add(new KeyedItem <string, int>("Foo", 0)));
            AssertExtensions.Throws <ArgumentException>("key", null, () => collection.Add(new KeyedItem <string, int>("fOo", 0)));
            AssertExtensions.Throws <ArgumentException>("key", null, () => collection.Add(new KeyedItem <string, int>("baR", 0)));
        }
コード例 #2
0
        public void TryGetValue(
            int collectionSize,
            Named <KeyedCollectionGetKeyedValue <TKey, TValue> >
            generateKeyedItem)
        {
            TKey[] keys;
            IKeyedItem <TKey, TValue>[] items;
            IKeyedItem <TKey, TValue>[] itemsWithKeys;
            var collection =
                new TestKeyedCollectionOfIKeyedItem <TKey, TValue>();

            collection.AddItems(
                generateKeyedItem.Value.Bind(
                    GenerateValue,
                    GetKeyForItem),
                ki => ki.Key,
                collectionSize,
                out keys,
                out items,
                out itemsWithKeys);
            IKeyedItem <TKey, TValue> itemNotIn =
                generateKeyedItem.Value(GenerateValue, GetKeyForItem);

            TKey keyNotIn = itemNotIn.Key;

            if (keyNotIn == null)
            {
                IKeyedItem <TKey, TValue> item;
                Assert.Throws <ArgumentNullException>(
                    () => collection.TryGetValue(keyNotIn, out item));
            }
            else
            {
                IKeyedItem <TKey, TValue> item;
                Assert.False(collection.TryGetValue(keyNotIn, out item));
            }
            foreach (TKey k in keys)
            {
                IKeyedItem <TKey, TValue> item;
                TKey key = k;
                if (key == null)
                {
                    Assert.Throws <ArgumentNullException>(
                        () => collection.TryGetValue(key, out item));
                    continue;
                }
                Assert.True(collection.TryGetValue(key, out item));
                Assert.Equal(item.Key, key);
            }
        }