Skip to content

Commit 8b7d1c8

Browse files
authored
feat: add GetSelectedCountries method to CountryProvider class (#107)
* feat: add GetSelectedCountries method to CountryProvider class * add unit tests for selected countries * test implementation in program class
1 parent d9c829b commit 8b7d1c8

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

src/World.Net.Console/Program.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
Console.WriteLine($"Error: {ex.Message}");
2222
}
2323

24+
PrintSeparator("Selected Countries");
25+
var indentifiers = new List<CountryIdentifier>
26+
{
27+
CountryIdentifier.Afghanistan,
28+
CountryIdentifier.Armenia,
29+
CountryIdentifier.Niger,
30+
CountryIdentifier.Nigeria,
31+
CountryIdentifier.Brazil
32+
};
33+
34+
var selectedCountries = CountryProvider.GetCountries(indentifiers);
35+
foreach (var country in selectedCountries)
36+
{
37+
Console.WriteLine($"Name: {country.Name,-20} | Capital: {country.Capital}");
38+
}
39+
40+
Console.WriteLine();
41+
2442
Console.WriteLine();
2543
Console.WriteLine("Press any key to exit...");
2644
Console.ReadKey();

src/World.Net.UnitTests/CountryProviderTest.cs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using World.Net.Helpers;
2-
3-
namespace World.Net.UnitTests;
1+
namespace World.Net.UnitTests;
42

53
public sealed class CountryProviderTest
64
{
@@ -57,4 +55,59 @@ public void GetCountry_ShouldThrow_CountryNotFoundException_WhenCountryDoesNotEx
5755
var exception = Assert.Throws<CountryNotFoundException>(() => CountryProvider.GetCountry(nonExistingCountryId));
5856
Assert.Equal($"Country with id {nonExistingCountryId} was not found.", exception.Message);
5957
}
58+
59+
[Fact]
60+
public void GetCountries_ValidIdentifiers_ReturnsCorrectCountries()
61+
{
62+
// Arrange
63+
var indentifiers = new List<CountryIdentifier>
64+
{
65+
CountryIdentifier.Afghanistan,
66+
CountryIdentifier.Brazil
67+
};
68+
69+
// Act
70+
var result = CountryProvider.GetCountries(indentifiers);
71+
72+
// Assert
73+
Assert.NotNull(result);
74+
Assert.NotEmpty(result);
75+
Assert.Equal(indentifiers.Count, result.Count);
76+
}
77+
78+
[Fact]
79+
public void GetCountries_EmptyIdentifiers_ReturnsEmptyList()
80+
{
81+
// Arrange
82+
var emptyIdentifiers = new List<CountryIdentifier>();
83+
84+
// Act
85+
var result = CountryProvider.GetCountries(emptyIdentifiers);
86+
87+
// Assert
88+
Assert.Empty(result);
89+
Assert.Equal(emptyIdentifiers.Count, result.Count);
90+
}
91+
92+
[Fact]
93+
public void GetCountries_WhenIdentifiersContainDuplicates_ReturnsDistinctCountries()
94+
{
95+
// Arrange
96+
var indentifiers = new List<CountryIdentifier>
97+
{
98+
CountryIdentifier.Afghanistan,
99+
CountryIdentifier.Afghanistan,
100+
CountryIdentifier.Argentina,
101+
CountryIdentifier.Brazil,
102+
CountryIdentifier.Brazil
103+
};
104+
105+
// Act
106+
var result = CountryProvider.GetCountries(indentifiers);
107+
108+
// Assert
109+
Assert.NotNull(result);
110+
Assert.NotEmpty(result);
111+
Assert.Equal(3, result.Count);
112+
}
60113
}

src/World.Net/CountryProvider.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,18 @@ public static ICountry GetCountry(CountryIdentifier countryIdentifier)
5555

5656
throw new CountryNotFoundException($"Country with id {countryIdentifier} was not found.");
5757
}
58+
59+
/// <summary>
60+
/// Retrieves multiple countries by their unique identifiers.
61+
/// </summary>
62+
/// <param name="countryIdentifiers">A collection of unique identifiers of the countries to retrieve.</param>
63+
/// <returns>
64+
/// A collection of <see cref="ICountry"/> instances corresponding to the specified <paramref name="countryIdentifiers"/>.
65+
/// </returns>
66+
public static IList<ICountry> GetCountries(IEnumerable<CountryIdentifier> countryIdentifiers)
67+
{
68+
var countries = _countries.Value.Where(x => countryIdentifiers.Contains(x.Key)).Select(x => x.Value);
69+
70+
return countries.ToList();
71+
}
5872
}

0 commit comments

Comments
 (0)