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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
//-----------------------------------------------------------------------
// <copyright file="MessageDictionaryTests.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test.Messaging.Reflection {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.Messaging.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class MessageDictionaryTests : MessagingTestBase {
private Mocks.TestMessage message;
[TestInitialize]
public override void SetUp() {
base.SetUp();
this.message = new Mocks.TestMessage();
}
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void CtorNull() {
new MessageDictionary(null);
}
/// <summary>
/// A test for System.Collections.Generic.IDictionary<System.String,System.String>.Values
/// </summary>
[TestMethod]
public void Values() {
IDictionary<string, string> target = new MessageDictionary(this.message);
Collection<string> expected = new Collection<string> {
this.message.Age.ToString(),
XmlConvert.ToString(DateTime.SpecifyKind(this.message.Timestamp, DateTimeKind.Utc), XmlDateTimeSerializationMode.Utc),
};
CollectionAssert<string>.AreEquivalent(expected, target.Values);
this.message.Age = 15;
this.message.Location = new Uri("http://localtest");
this.message.Name = "Andrew";
target["extra"] = "a";
expected = new Collection<string> {
this.message.Age.ToString(),
this.message.Location.AbsoluteUri,
this.message.Name,
XmlConvert.ToString(DateTime.SpecifyKind(this.message.Timestamp, DateTimeKind.Utc), XmlDateTimeSerializationMode.Utc),
"a",
};
CollectionAssert<string>.AreEquivalent(expected, target.Values);
}
/// <summary>
/// A test for System.Collections.Generic.IDictionary<System.String,System.String>.Keys
/// </summary>
[TestMethod]
public void Keys() {
// We expect that non-nullable value type fields will automatically have keys
// in the dictionary for them.
IDictionary<string, string> target = new MessageDictionary(this.message);
Collection<string> expected = new Collection<string> {
"age",
"Timestamp",
};
CollectionAssert<string>.AreEquivalent(expected, target.Keys);
this.message.Name = "Andrew";
expected.Add("Name");
target["extraField"] = string.Empty;
expected.Add("extraField");
CollectionAssert<string>.AreEquivalent(expected, target.Keys);
}
/// <summary>
/// A test for System.Collections.Generic.IDictionary<System.String,System.String>.Item
/// </summary>
[TestMethod]
public void Item() {
IDictionary<string, string> target = new MessageDictionary(this.message);
// Test setting of declared message properties.
this.message.Age = 15;
Assert.AreEqual("15", target["age"]);
target["age"] = "13";
Assert.AreEqual(13, this.message.Age);
// Test setting extra fields
target["extra"] = "fun";
Assert.AreEqual("fun", target["extra"]);
Assert.AreEqual("fun", ((IProtocolMessage)this.message).ExtraData["extra"]);
// Test clearing extra fields
target["extra"] = null;
Assert.IsFalse(target.ContainsKey("extra"));
}
/// <summary>
/// A test for System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.String,System.String<<.IsReadOnly
/// </summary>
[TestMethod]
public void IsReadOnly() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
Assert.IsFalse(target.IsReadOnly);
}
/// <summary>
/// A test for System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.String,System.String<<.Count
/// </summary>
[TestMethod]
public void Count() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetDictionary = (IDictionary<string, string>)target;
Assert.AreEqual(targetDictionary.Keys.Count, target.Count);
targetDictionary["extraField"] = "hi";
Assert.AreEqual(targetDictionary.Keys.Count, target.Count);
}
/// <summary>
/// A test for System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.String,System.String<<.GetEnumerator
/// </summary>
[TestMethod]
public void GetEnumerator() {
IEnumerable<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetDictionary = (IDictionary<string, string>)target;
var keys = targetDictionary.Keys.GetEnumerator();
var values = targetDictionary.Values.GetEnumerator();
IEnumerator<KeyValuePair<string, string>> actual = target.GetEnumerator();
bool keysLast = true, valuesLast = true, actualLast = true;
while (true) {
keysLast = keys.MoveNext();
valuesLast = values.MoveNext();
actualLast = actual.MoveNext();
if (!keysLast || !valuesLast || !actualLast) {
break;
}
Assert.AreEqual(keys.Current, actual.Current.Key);
Assert.AreEqual(values.Current, actual.Current.Value);
}
Assert.IsTrue(keysLast == valuesLast && keysLast == actualLast);
}
[TestMethod]
public void GetEnumeratorUntyped() {
IEnumerable target = new MessageDictionary(this.message);
IDictionary<string, string> targetDictionary = (IDictionary<string, string>)target;
var keys = targetDictionary.Keys.GetEnumerator();
var values = targetDictionary.Values.GetEnumerator();
IEnumerator actual = target.GetEnumerator();
bool keysLast = true, valuesLast = true, actualLast = true;
while (true) {
keysLast = keys.MoveNext();
valuesLast = values.MoveNext();
actualLast = actual.MoveNext();
if (!keysLast || !valuesLast || !actualLast) {
break;
}
KeyValuePair<string, string> current = (KeyValuePair<string, string>)actual.Current;
Assert.AreEqual(keys.Current, current.Key);
Assert.AreEqual(values.Current, current.Value);
}
Assert.IsTrue(keysLast == valuesLast && keysLast == actualLast);
}
/// <summary>
/// A test for System.Collections.Generic.IDictionary<System.String,System.String>.TryGetValue
/// </summary>
[TestMethod]
public void TryGetValue() {
IDictionary<string, string> target = new MessageDictionary(this.message);
this.message.Name = "andrew";
string name;
Assert.IsTrue(target.TryGetValue("Name", out name));
Assert.AreEqual(this.message.Name, name);
Assert.IsFalse(target.TryGetValue("name", out name));
Assert.IsNull(name);
target["extra"] = "value";
string extra;
Assert.IsTrue(target.TryGetValue("extra", out extra));
Assert.AreEqual("value", extra);
}
/// <summary>
/// A test for System.Collections.Generic.IDictionary<System.String,System.String>.Remove
/// </summary>
[TestMethod]
public void RemoveTest1() {
IDictionary<string, string> target = new MessageDictionary(this.message);
this.message.Name = "andrew";
Assert.IsTrue(target.Remove("Name"));
Assert.IsNull(this.message.Name);
Assert.IsFalse(target.Remove("Name"));
Assert.IsFalse(target.Remove("extra"));
target["extra"] = "value";
Assert.IsTrue(target.Remove("extra"));
Assert.IsFalse(target.ContainsKey("extra"));
}
/// <summary>
/// A test for System.Collections.Generic.IDictionary<System.String,System.String>.ContainsKey
/// </summary>
[TestMethod]
public void ContainsKey() {
IDictionary<string, string> target = new MessageDictionary(this.message);
Assert.IsTrue(target.ContainsKey("age"), "Value type declared element should have a key.");
Assert.IsFalse(target.ContainsKey("Name"), "Null declared element should NOT have a key.");
Assert.IsFalse(target.ContainsKey("extra"));
target["extra"] = "value";
Assert.IsTrue(target.ContainsKey("extra"));
}
/// <summary>
/// A test for System.Collections.Generic.IDictionary<System.String,System.String>.Add
/// </summary>
[TestMethod]
public void AddByKeyAndValue() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add("extra", "value");
Assert.IsTrue(target.Contains(new KeyValuePair<string, string>("extra", "value")));
target.Add("Name", "Andrew");
Assert.AreEqual("Andrew", this.message.Name);
}
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void AddNullValue() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add("extra", null);
}
/// <summary>
/// A test for System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.String,System.String<<.Add
/// </summary>
[TestMethod]
public void AddByKeyValuePair() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add(new KeyValuePair<string, string>("extra", "value"));
Assert.IsTrue(target.Contains(new KeyValuePair<string, string>("extra", "value")));
}
[TestMethod, ExpectedException(typeof(ArgumentException))]
public void AddExtraFieldThatAlreadyExists() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add("extra", "value");
target.Add("extra", "value");
}
[TestMethod, ExpectedException(typeof(ArgumentException))]
public void AddDeclaredValueThatAlreadyExists() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add("Name", "andrew");
target.Add("Name", "andrew");
}
[TestMethod]
public void DefaultReferenceTypeDeclaredPropertyHasNoKey() {
IDictionary<string, string> target = new MessageDictionary(this.message);
Assert.IsFalse(target.ContainsKey("Name"), "A null value should result in no key.");
Assert.IsFalse(target.Keys.Contains("Name"), "A null value should result in no key.");
}
[TestMethod]
public void RemoveStructDeclaredProperty() {
IDictionary<string, string> target = new MessageDictionary(this.message);
this.message.Age = 5;
Assert.IsTrue(target.ContainsKey("age"));
target.Remove("age");
Assert.IsTrue(target.ContainsKey("age"));
Assert.AreEqual(0, this.message.Age);
}
/// <summary>
/// A test for System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.String,System.String<<.Remove
/// </summary>
[TestMethod]
public void RemoveByKeyValuePair() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
this.message.Name = "Andrew";
Assert.IsFalse(target.Remove(new KeyValuePair<string, string>("Name", "andrew")));
Assert.AreEqual("Andrew", this.message.Name);
Assert.IsTrue(target.Remove(new KeyValuePair<string, string>("Name", "Andrew")));
Assert.IsNull(this.message.Name);
}
/// <summary>
/// A test for System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.String,System.String<<.CopyTo
/// </summary>
[TestMethod]
public void CopyTo() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetAsDictionary = ((IDictionary<string, string>)target);
KeyValuePair<string, string>[] array = new KeyValuePair<string, string>[target.Count + 1];
int arrayIndex = 1;
target.CopyTo(array, arrayIndex);
Assert.AreEqual(new KeyValuePair<string, string>(), array[0]);
for (int i = 1; i < array.Length; i++) {
Assert.IsNotNull(array[i].Key);
Assert.AreEqual(targetAsDictionary[array[i].Key], array[i].Value);
}
}
/// <summary>
/// A test for System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.String,System.String<<.Contains
/// </summary>
[TestMethod]
public void ContainsKeyValuePair() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetAsDictionary = ((IDictionary<string, string>)target);
Assert.IsFalse(target.Contains(new KeyValuePair<string, string>("age", "1")));
Assert.IsTrue(target.Contains(new KeyValuePair<string, string>("age", "0")));
targetAsDictionary["extra"] = "value";
Assert.IsFalse(target.Contains(new KeyValuePair<string, string>("extra", "Value")));
Assert.IsTrue(target.Contains(new KeyValuePair<string, string>("extra", "value")));
Assert.IsFalse(target.Contains(new KeyValuePair<string, string>("wayoff", "value")));
}
/// <summary>
/// A test for System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<System.String,System.String<<.Clear
/// </summary>
[TestMethod]
public void Clear() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetAsDictionary = ((IDictionary<string, string>)target);
this.message.Name = "Andrew";
this.message.Age = 15;
targetAsDictionary["extra"] = "value";
target.Clear();
Assert.AreEqual(2, target.Count, "Clearing should remove all keys except for declared non-nullable structs.");
Assert.IsFalse(targetAsDictionary.ContainsKey("extra"));
Assert.IsNull(this.message.Name);
Assert.AreEqual(0, this.message.Age);
}
}
}
|