-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathDictionaryExtensionsTests.cs
More file actions
108 lines (94 loc) · 3.19 KB
/
DictionaryExtensionsTests.cs
File metadata and controls
108 lines (94 loc) · 3.19 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
using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Extensions;
using Xunit;
namespace Microsoft.OpenApi.Tests.Extensions
{
public class DictionaryExtensionsTests
{
[Fact]
public void ShouldSortStringIntDictionaryInAscendingOrder()
{
var dict = new Dictionary<string, int> { { "b", 2 }, { "a", 1 } };
var result = dict.Sort();
Assert.Equal(["a", "b"], result.Keys);
}
[Fact]
public void ShouldReturnEmptyDictionaryWhenSourceIsEmpty()
{
var dict = new Dictionary<string, int>();
var result = dict.Sort();
Assert.Empty(result);
}
[Fact]
public void ShouldKeepOrderWhenDictionaryIsAlreadySorted()
{
var dict = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
var result = dict.Sort();
Assert.Equal(["a", "b"], result.Keys);
}
[Fact]
public void ShouldSortNumericKeysNaturally()
{
var dict = new Dictionary<int, string> { { 10, "a" }, { 1, "b" } };
var result = dict.Sort();
Assert.Equal([1, 10], result.Keys);
}
[Fact]
public void ShouldSortDateTimeKeysInAscendingOrder()
{
var now = DateTime.Now;
var later = now.AddHours(1);
var dict = new Dictionary<DateTime, string>
{
[later] = "future",
[now] = "present"
};
var result = dict.Sort();
Assert.Equal([now, later], result.Keys);
}
[Fact]
public void ShouldSortWithCustomDescendingComparer()
{
var dict = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
var result = dict.Sort(Comparer<string>.Create((x, y) => y.CompareTo(x)));
Assert.Equal(["b", "a"], result.Keys);
}
[Fact]
public void ShouldSortDictionaryWithComplexValueTypes()
{
var dict = new Dictionary<string, ISet<string>>
{
{ "z", new HashSet<string> { "value1" } },
{ "a", new HashSet<string> { "value2" } }
};
var result = dict.Sort();
Assert.Equal(["a", "z"], result.Keys);
Assert.Equal(new HashSet<string> { "value2" }, result["a"]);
}
[Fact]
public void ShouldSortDictionaryWithNullValues()
{
var dict = new Dictionary<string, string>
{
{ "b", null },
{ "a", "value" }
};
var result = dict.Sort();
Assert.Equal(["a", "b"], result.Keys);
Assert.Null(result["b"]);
}
[Fact]
public void ShouldSortDictionaryOfDictionariesByOuterKey()
{
var dict = new Dictionary<string, Dictionary<string, string>>
{
["z"] = new Dictionary<string, string> { { "x", "1" } },
["a"] = new Dictionary<string, string> { { "y", "2" } }
};
var result = dict.Sort();
Assert.Equal(["a", "z"], result.Keys);
Assert.Equal("2", result["a"]["y"]);
}
}
}