Skip to content

Commit c7d57c2

Browse files
committed
Validate parsed enums and update tests
Refactor TryGetCountry to a shared internal implementation and add enum validation: only return a country when Enum.TryParse AND Enum.IsDefined for Alpha2Code/Alpha3Code. Update unit tests to use TestMethod, add a negative DataRow ("0", false), and strengthen assertions/messages. Also set an explicit numeric value for Alpha2Code.ZW = 9087. - #61
1 parent 7dfebb6 commit c7d57c2

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/Nager.Country.UnitTest/CountryTest.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Nager.Country.UnitTest
88
[TestClass]
99
public class CountryTest
1010
{
11-
[DataTestMethod]
11+
[TestMethod]
1212
[DataRow("DE")]
1313
[DataRow("de")]
1414
[DataRow("De")]
@@ -26,7 +26,8 @@ public void GetCountry(string countryCode)
2626
}
2727
}
2828

29-
[DataTestMethod]
29+
[TestMethod]
30+
[DataRow("0", false)]
3031
[DataRow("DE", true)]
3132
[DataRow("de", true)]
3233
[DataRow("De", true)]
@@ -41,17 +42,19 @@ public void TryGetCountry(string countryCode, bool isSuccessful)
4142
var successful = countryProvider.TryGetCountry(countryCode, out var countryInfo);
4243
if (isSuccessful)
4344
{
44-
Assert.IsTrue(successful);
45+
Assert.IsTrue(successful, "The assignment should be possible.");
4546
Assert.IsNotNull(countryInfo);
47+
48+
Assert.AreEqual(Alpha2Code.DE, countryInfo.Alpha2Code);
4649
}
4750
else
4851
{
49-
Assert.IsFalse(successful);
52+
Assert.IsFalse(successful, "The assignment should not be possible.");
5053
Assert.IsNull(countryInfo);
5154
}
5255
}
5356

54-
[DataTestMethod]
57+
[TestMethod]
5558
[DataRow("Austria")]
5659
[DataRow("Republic of Austria")]
5760
public void GetCountryByName(string countryName)
@@ -62,7 +65,7 @@ public void GetCountryByName(string countryName)
6265
Assert.AreEqual(Alpha2Code.AT, countryInfo.Alpha2Code);
6366
}
6467

65-
[DataTestMethod]
68+
[TestMethod]
6669
[DataRow("Austria")]
6770
[DataRow("Republic of Austria")]
6871
[DataRow("austria")]

src/Nager.Country/Alpha2Code.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,6 @@ public enum Alpha2Code : int
10081008
/// <summary>
10091009
/// Zimbabwe
10101010
/// </summary>
1011-
ZW
1011+
ZW = 9087
10121012
}
10131013
}

src/Nager.Country/CountryProvider.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,7 @@ public ICountryInfo GetCountry(string alpha2or3Code)
313313
/// <inheritdoc/>
314314
public bool TryGetCountry(string alpha2or3Code, out ICountryInfo? countryInfo)
315315
{
316-
if (Enum.TryParse(alpha2or3Code, true, out Alpha2Code alpha2Code))
317-
{
318-
countryInfo = this.GetCountry(alpha2Code);
319-
return true;
320-
}
321-
322-
if (Enum.TryParse(alpha2or3Code, true, out Alpha3Code alpha3Code))
323-
{
324-
countryInfo = this.GetCountry(alpha3Code);
325-
return true;
326-
}
327-
328-
countryInfo = null;
329-
return false;
316+
return this.TryGetCountryInternal(alpha2or3Code, out countryInfo);
330317
}
331318

332319
#endif
@@ -335,25 +322,36 @@ public bool TryGetCountry(string alpha2or3Code, out ICountryInfo? countryInfo)
335322

336323
/// <inheritdoc/>
337324
public bool TryGetCountry(string alpha2or3Code, [NotNullWhen(true)] out ICountryInfo? countryInfo)
325+
{
326+
return this.TryGetCountryInternal(alpha2or3Code, out countryInfo);
327+
}
328+
329+
#endif
330+
331+
private bool TryGetCountryInternal(string alpha2or3Code, out ICountryInfo? countryInfo)
338332
{
339333
if (Enum.TryParse(alpha2or3Code, true, out Alpha2Code alpha2Code))
340334
{
341-
countryInfo = this.GetCountry(alpha2Code);
342-
return true;
335+
if (Enum.IsDefined(typeof(Alpha2Code), alpha2Code))
336+
{
337+
countryInfo = this.GetCountry(alpha2Code);
338+
return true;
339+
}
343340
}
344341

345342
if (Enum.TryParse(alpha2or3Code, true, out Alpha3Code alpha3Code))
346343
{
347-
countryInfo = this.GetCountry(alpha3Code);
348-
return true;
344+
if (Enum.IsDefined(typeof(Alpha3Code), alpha3Code))
345+
{
346+
countryInfo = this.GetCountry(alpha3Code);
347+
return true;
348+
}
349349
}
350350

351351
countryInfo = null;
352352
return false;
353353
}
354354

355-
#endif
356-
357355
/// <inheritdoc/>
358356
public ICountryInfo GetCountry(Alpha2Code alpha2Code)
359357
{

0 commit comments

Comments
 (0)