forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionExtensionsTests.cs
More file actions
143 lines (126 loc) · 6.05 KB
/
CollectionExtensionsTests.cs
File metadata and controls
143 lines (126 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xunit;
namespace System.Collections.Tests
{
public class CollectionExtensionsTests
{
[Fact]
public void GetValueOrDefault_KeyExistsInIReadOnlyDictionary_ReturnsValue()
{
IReadOnlyDictionary<string, string> dictionary = new SortedDictionary<string, string>() { { "key", "value" } };
Assert.Equal("value", dictionary.GetValueOrDefault("key"));
Assert.Equal("value", dictionary.GetValueOrDefault("key", null));
}
[Fact]
public void GetValueOrDefault_KeyDoesntExistInIReadOnlyDictionary_ReturnsDefaultValue()
{
IReadOnlyDictionary<string, string> dictionary = new SortedDictionary<string, string>() { { "key", "value" } };
Assert.Null(dictionary.GetValueOrDefault("anotherKey"));
Assert.Equal("anotherValue", dictionary.GetValueOrDefault("anotherKey", "anotherValue"));
}
[Fact]
public void GetValueOrDefault_NullKeyIReadOnlyDictionary_ThrowsArgumentNullException()
{
IReadOnlyDictionary<string, string> dictionary = new SortedDictionary<string, string>() { { "key", "value" } };
AssertExtensions.Throws<ArgumentNullException>("key", () => dictionary.GetValueOrDefault(null));
AssertExtensions.Throws<ArgumentNullException>("key", () => dictionary.GetValueOrDefault(null, "anotherValue"));
}
[Fact]
public void GetValueOrDefault_NullIReadOnlyDictionary_ThrowsArgumentNullException()
{
IReadOnlyDictionary<string, string> dictionary = null;
AssertExtensions.Throws<ArgumentNullException>("dictionary", () => dictionary.GetValueOrDefault("key"));
AssertExtensions.Throws<ArgumentNullException>("dictionary", () => dictionary.GetValueOrDefault("key", "value"));
}
[Fact]
public void TryAdd_NullIDictionary_ThrowsArgumentNullException()
{
IDictionary<string, string> dictionary = null;
AssertExtensions.Throws<ArgumentNullException>("dictionary", () => dictionary.TryAdd("key", "value"));
}
[Fact]
public void TryAdd_NullKeyIDictionary_ThrowsArgumentNullException()
{
IDictionary<string, string> dictionary = new SortedDictionary<string, string>();
AssertExtensions.Throws<ArgumentNullException>("key", () => dictionary.TryAdd(null, "value"));
}
[Fact]
public void TryAdd_KeyDoesntExistInIDictionary_ReturnsTrue()
{
IDictionary<string, string> dictionary = new SortedDictionary<string, string>();
Assert.True(dictionary.TryAdd("key", "value"));
Assert.Equal("value", dictionary["key"]);
}
[Fact]
public void TryAdd_KeyExistsInIDictionary_ReturnsFalse()
{
IDictionary<string, string> dictionary = new SortedDictionary<string, string>() { ["key"] = "value" };
Assert.False(dictionary.TryAdd("key", "value2"));
Assert.Equal("value", dictionary["key"]);
}
[Fact]
public void Remove_NullIDictionary_ThrowsArgumentNullException()
{
IDictionary<string, string> dictionary = null;
string value = null;
AssertExtensions.Throws<ArgumentNullException>("dictionary", () => dictionary.Remove("key", out value));
Assert.Null(value);
}
[Fact]
public void Remove_NullKeyIDictionary_ThrowsArgumentNullException()
{
IDictionary<string, string> dictionary = new SortedDictionary<string, string>();
string value = null;
AssertExtensions.Throws<ArgumentNullException>("key", () => dictionary.Remove(null, out value));
Assert.Null(value);
}
[Fact]
public void Remove_KeyExistsInIDictionary_ReturnsTrue()
{
IDictionary<string, string> dictionary = new SortedDictionary<string, string>() { ["key"] = "value" };
Assert.True(dictionary.Remove("key", out var value));
Assert.Equal("value", value);
Assert.Throws<KeyNotFoundException>(() => dictionary["key"]);
}
[Fact]
public void Remove_KeyDoesntExistInIDictionary_ReturnsFalse()
{
IDictionary<string, string> dictionary = new SortedDictionary<string, string>();
Assert.False(dictionary.Remove("key", out var value));
Assert.Equal(default(string), value);
}
[Fact]
public void AsReadOnly_TurnsIListIntoReadOnlyCollection()
{
IList<string> list = new List<string> { "A", "B" };
ReadOnlyCollection<string> readOnlyCollection = list.AsReadOnly();
Assert.NotNull(readOnlyCollection);
CollectionAsserts.Equal(list, readOnlyCollection);
}
[Fact]
public void AsReadOnly_TurnsIDictionaryIntoReadOnlyDictionary()
{
IDictionary<string, string> dictionary = new Dictionary<string, string> { ["key1"] = "value1", ["key2"] = "value2" };
ReadOnlyDictionary<string, string> readOnlyDictionary = dictionary.AsReadOnly();
Assert.NotNull(readOnlyDictionary);
Assert.Equal(dictionary["key1"], readOnlyDictionary["key1"]);
Assert.Equal(dictionary["key2"], readOnlyDictionary["key2"]);
Assert.Equal(dictionary.Count, readOnlyDictionary.Count);
}
[Fact]
public void AsReadOnly_NullIList_ThrowsArgumentNullException()
{
IList<string> list = null;
Assert.Throws<ArgumentNullException>("list", () => list.AsReadOnly());
}
[Fact]
public void AsReadOnly_NullIDictionary_ThrowsArgumentNullException()
{
IDictionary<string, string> dictionary = null;
Assert.Throws<ArgumentNullException>("dictionary", () => dictionary.AsReadOnly());
}
}
}