/
WeakDictionaryExtensionTests.cs
175 lines (131 loc) · 5.09 KB
/
WeakDictionaryExtensionTests.cs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
namespace ExtensionCandidates
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Xml.Linq;
using NUnit.Framework;
[TestFixture]
public class WeakDictionaryExtensionTests : AssertionHelper
{
[Test]
public void get_null_string_from_hashtable_for_missing_value()
{
var table = new Hashtable { { "app id", "abc123" } };
var value = table.Get<string>("cat id");
Expect(value, Is.Null);
}
[Test]
public void can_get_string_from_hashtable()
{
var table = new Hashtable { { "app id", "abc123" } };
var value = table.Get<string>("app id");
Expect(value, Is.EqualTo("abc123"));
}
[Test]
public void get_string_from_hashtable_when_default_is_provided()
{
var table = new Hashtable { { "app id", "abc123" } };
var value = table.GetOrDefault("app id", "abracadabra");
Expect(value, Is.EqualTo("abc123"));
}
[Test]
public void can_get_default_string_from_hashtable()
{
var table = new Hashtable { { "app id", "abc123" } };
var value = table.GetOrDefault("cat id", "Frank");
Expect(value, Is.EqualTo("Frank"));
}
[Test]
public void can_get_bool_from_hashtable()
{
var table = new Hashtable { { "app id", "true" } };
var value = table.Get<bool>("app id");
Expect(value, Is.True);
}
[Test]
public void get_bool_from_hashtable_when_default_is_provided()
{
var table = new Hashtable { { "app id", "true" } };
var value = table.GetOrDefault("app id", false);
Expect(value, Is.True);
}
[Test]
public void can_get_default_bool_from_hashtable()
{
var value = true;
var table = new Hashtable { { "app id", "abc123" } };
value = table.GetOrDefault("Allow Windows Live Writer", value);
Expect(value, Is.True);
}
[Test]
public void get_false_from_hashtable_for_missing_value()
{
var table = new Hashtable { { "app id", "abc123" } };
var value = table.Get<bool>("Allow Windows Live Writer");
Expect(value, Is.False);
}
[Test]
public void get_bool_with_custom_converter_from_hashtable()
{
var table = new Hashtable { { "allow", "on" } };
var value = table.Get(
"allow",
v =>
{
bool allowed;
if (!bool.TryParse(v, out allowed))
{
allowed = v.Equals("on", StringComparison.Ordinal);
}
return allowed;
});
Expect(value, Is.True);
}
[Test]
public void get_int_from_statebag()
{
var collection = new StateBag { { "appId", "123" } };
var value = collection.Get<int>("appId");
Expect(value, Is.EqualTo(123));
}
[Test]
public void get_datetime_from_xmlnode()
{
var sampleDateTime = new DateTime(2013, 5, 30, 5, 6, 7);
var node = new XElement(
"parent",
new XElement("startDate", sampleDateTime));
var nodeDictionary = node.Descendants().ToDictionary(element => element.Name.ToString(), element => element.Value);
var value = nodeDictionary.Get<DateTime>("startDate");
Expect(value, Is.EqualTo(sampleDateTime));
}
[Test]
public void can_get_timespan_with_custom_converter()
{
var collection = new Hashtable { { "length", "1:10:10" } };
var value = collection.Get("length", TimeSpan.Parse);
Expect(value, Is.EqualTo(TimeSpan.FromSeconds(4210)));
}
[Test]
public void throws_argumentnullexception_when_dictionary_is_null()
{
IDictionary dictionary = null;
Expect(() => dictionary.Get<int>("value ID"), Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("dictionary"));
}
[Test]
public void throws_invalidcastexception_when_type_is_not_supported()
{
var dictionary = new Dictionary<string, string> { { "length", "1:10:10" } };
Expect(() => dictionary.Get<TimeSpan>("length"), Throws.TypeOf<InvalidCastException>());
}
[Test]
public void throws_formatexception_when_value_cannot_be_parsed()
{
var dictionary = new Dictionary<string, string> { { "ID", "abc123" } };
Expect(() => dictionary.Get<int>("ID"), Throws.TypeOf<FormatException>());
}
}
}