-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAlpha2CountryCodeTests.cs
More file actions
164 lines (141 loc) · 4.17 KB
/
Alpha2CountryCodeTests.cs
File metadata and controls
164 lines (141 loc) · 4.17 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using OpenShock.Common.Geo;
using TUnit.Assertions.AssertConditions.Throws;
namespace OpenShock.Common.Tests.Geo;
public class Alpha2CountryCodeTests
{
[Test]
[Arguments("US", 'U', 'S')]
[Arguments("DE", 'D', 'E')]
public async Task ValidCode_ShouldParse(string str, char char1, char char2)
{
// Act
Alpha2CountryCode c = str;
// Assert
await Assert.That(c.Char1).IsEqualTo(char1);
await Assert.That(c.Char2).IsEqualTo(char2);
}
[Test]
[Arguments("E")]
[Arguments("INVALID")]
public async Task InvalidCharCount_ShouldThrow_InvalidLength(string str)
{
// Act & Assert
await Assert.That(() =>
{
Alpha2CountryCode c = str;
})
.ThrowsExactly<ArgumentOutOfRangeException>()
.WithMessage("Country code must be exactly 2 characters long (Parameter 'str')");
}
[Test]
[Arguments("us")]
[Arguments("Us")]
[Arguments("uS")]
[Arguments("12")]
[Arguments("U1")]
[Arguments("1U")]
[Arguments("ÆØ")]
[Arguments(":D")]
public async Task InvalidCharTypes_ShouldThrow(string str)
{
// Act & Assert
await Assert.That(() =>
{
Alpha2CountryCode c = str;
})
.ThrowsExactly<ArgumentOutOfRangeException>()
.WithMessage("Country code must be uppercase ASCII characters only (Parameter 'str')");
}
[Test]
[Arguments("US", 'U', 'S')]
[Arguments("DE", 'D', 'E')]
public async Task TryParseAndValidate_ValidCode_ShouldReturnTrue(string str, char char1, char char2)
{
// Act
var result = Alpha2CountryCode.TryParseAndValidate(str, out var c);
// Assert
await Assert.That(result).IsTrue();
await Assert.That(c.Char1).IsEqualTo(char1);
await Assert.That(c.Char2).IsEqualTo(char2);
}
[Test]
[Arguments("E")]
[Arguments("INVALID")]
[Arguments("us")]
[Arguments("Us")]
[Arguments("uS")]
[Arguments("12")]
[Arguments("U1")]
[Arguments("1U")]
[Arguments("ÆØ")]
[Arguments(":D")]
[Arguments("")]
[Arguments(" ")]
[Arguments(" ")]
[Arguments(" ")]
public async Task TryParseAndValidate_InvalidCode_ShouldReturnFalse(string str)
{
// Act
var result = Alpha2CountryCode.TryParseAndValidate(str, out var c);
// Assert
await Assert.That(result).IsFalse();
await Assert.That(c == Alpha2CountryCode.UnknownCountry).IsTrue();
}
[Test]
[Arguments("US", "US", 0x5553_5553)]
[Arguments("US", "DE", 0x4445_5553)]
[Arguments("DE", "US", 0x4445_5553)]
public async Task GetCombinedHashCode_ShouldReturnCombined(string str1, string str2, int expected)
{
// Act
var result = Alpha2CountryCode.GetCombinedHashCode(str1, str2);
// Assert
await Assert.That(result).IsEqualTo(expected);
}
[Test]
[Arguments("US", 0x5553)]
[Arguments("DE", 0x4445)]
[Arguments("NO", 0x4E4F)]
public async Task GetHashcode_ShouldReturnHash(string str, int expected)
{
// Arrange
Alpha2CountryCode code = str;
// Act
var result = code.GetHashCode();
// Assert
await Assert.That(result).IsEqualTo(expected); // "US"
}
[Test]
public async Task IsUnknown_ShouldReturnTrue()
{
// Arrange
Alpha2CountryCode code = Alpha2CountryCode.UnknownCountry;
// Act
var result = code.IsUnknown();
// Assert
await Assert.That(result).IsTrue();
}
[Test]
public async Task UnknownCountry_IsEqualTo_XX()
{
// Arrange
Alpha2CountryCode code = "XX";
// Act
var result = code == Alpha2CountryCode.UnknownCountry;
// Assert
await Assert.That(result).IsTrue();
}
[Test]
[Arguments("US")]
[Arguments("DE")]
[Arguments("NO")]
public async Task IsUnknown_Known_ShouldReturnFalse(string str)
{
// Arrange
Alpha2CountryCode code = str;
// Act
var result = code.IsUnknown();
// Assert
await Assert.That(result).IsFalse();
}
}