forked from microsoft/vstest
/
CustomKeyValueConverterTests.cs
97 lines (74 loc) · 3.33 KB
/
CustomKeyValueConverterTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Globalization;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.TestPlatform.ObjectModel.UnitTests;
[TestClass]
public class CustomKeyValueConverterTests
{
private readonly CustomKeyValueConverter _customKeyValueConverter;
public CustomKeyValueConverterTests()
{
_customKeyValueConverter = new CustomKeyValueConverter();
}
[TestMethod]
public void CustomKeyValueConverterShouldDeserializeWellformedJson()
{
var json = "[{ \"Key\": \"key1\", \"Value\": \"val1\" }]";
var data = _customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];
Assert.IsNotNull(data);
Assert.AreEqual(1, data.Length);
Assert.AreEqual("key1", data[0].Key);
Assert.AreEqual("val1", data[0].Value);
}
[TestMethod]
public void CustomKeyValueConverterShouldDeserializeKeyValuePairArray()
{
var json = "[{ \"Key\": \"key1\", \"Value\": \"val1\" }, { \"Key\": \"key2\", \"Value\": \"val2\" }]";
var data = _customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];
Assert.IsNotNull(data);
Assert.AreEqual(2, data.Length);
Assert.AreEqual("key1", data[0].Key);
Assert.AreEqual("val1", data[0].Value);
Assert.AreEqual("key2", data[1].Key);
Assert.AreEqual("val2", data[1].Value);
}
[TestMethod]
public void CustomKeyValueConverterShouldDeserializeEmptyArray()
{
var json = "[]";
var data = _customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];
Assert.IsNotNull(data);
Assert.AreEqual(0, data.Length);
}
[TestMethod]
public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue()
{
var json = "[{ \"Key\": \"\", \"Value\": \"\" }]";
var data = _customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];
Assert.IsNotNull(data);
Assert.AreEqual(1, data.Length);
Assert.AreEqual(string.Empty, data[0].Key);
Assert.AreEqual(string.Empty, data[0].Value);
}
[TestMethod]
public void CustomKeyValueConverterShouldDeserializeDuplicateKeysKvps()
{
var json = "[{ \"Key\": \"key1\", \"Value\": \"val1\" }, { \"Key\": \"key1\", \"Value\": \"val2\" }]";
var data = _customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair<string, string>[];
Assert.IsNotNull(data);
Assert.AreEqual(2, data.Length);
Assert.AreEqual("key1", data[0].Key);
Assert.AreEqual("val1", data[0].Value);
Assert.AreEqual("key1", data[1].Key);
Assert.AreEqual("val2", data[1].Value);
}
[TestMethod]
public void CustomKeyValueConverterShouldDeserializeNullValue()
{
var data = _customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, null) as KeyValuePair<string, string>[];
Assert.IsNull(data);
}
}