-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GetCountryCode method and enhance API and docs
- Added `GetCountryCode(string countryName)` to `CountryData.Standard` to return ISO 3166-1 alpha-2 codes. - Updated `README.md` with documentation for `GetCountryCode`. - Modified `Program.cs` in `CountryData.Sample.CountryConsoleProject` to call and print the result of `GetCountryCode`. - Added `GetCountryCodeByName` API endpoint in `CountryController` within `CountryData.Sample.Web.API.Controllers`. - Updated `CountryData.Sample.Web.API.csproj` to generate XML docs and suppress warning 1591. - Enhanced Swagger configuration in `Program.cs` to include XML comments. - Made `GetCountryData` method in `CountryHelper` virtual for potential overrides. - Added `CountryExtensions` class in `CountryData.Standard` to manage `CountryHelper` and provide country-related methods. - Implemented unit tests for `CountryExtensions` in `CountryExtensionsTests.cs`. - Minor formatting changes in `Currency.cs` and `Regions.cs` to add missing braces. - Changed `app.Run()` to `await app.RunAsync()` in `Program.cs` for async execution.
- Loading branch information
1 parent
e8b183a
commit 64f0d45
Showing
11 changed files
with
263 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace CountryData.Standard | ||
{ | ||
/// <summary> | ||
/// Manages the CountryHelper instance and provides methods for retrieving country-related information. | ||
/// </summary> | ||
public class CountryExtensions : IDisposable | ||
{ | ||
private CountryHelper _countryHelper; | ||
private bool _disposed = false; | ||
|
||
/// <summary> | ||
/// Constructor to initialize the CountryHelper instance. | ||
/// </summary> | ||
public CountryExtensions() | ||
{ | ||
_countryHelper = new CountryHelper(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the country code for the specified country name. | ||
/// </summary> | ||
/// <param name="countryName">The name of the country.</param> | ||
/// <returns>The country code if found; otherwise, null.</returns> | ||
public string GetCountryCode(string countryName) | ||
{ | ||
if (countryName == null) | ||
{ | ||
throw new ArgumentNullException(nameof(countryName)); | ||
} | ||
|
||
var country = _countryHelper.GetCountryData() | ||
.FirstOrDefault(c => c.CountryName.Equals(countryName, StringComparison.OrdinalIgnoreCase)); | ||
|
||
return country?.CountryShortCode; | ||
} | ||
|
||
/// <summary> | ||
/// Dispose method to clean up resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Protected method to dispose resources. | ||
/// </summary> | ||
/// <param name="disposing">Indicates whether the method is called from Dispose or finalizer.</param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (disposing && _countryHelper != null) | ||
{ | ||
// Dispose managed resources. | ||
_countryHelper = null; | ||
} | ||
|
||
// Dispose unmanaged resources. | ||
|
||
_disposed = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Finalizer to ensure resources are cleaned up. | ||
/// </summary> | ||
~CountryExtensions() | ||
{ | ||
Dispose(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ public class Currency | |
{ | ||
public string Code { get; set; } | ||
public string Name { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,7 @@ public class Regions | |
{ | ||
public String Name { get; set; } | ||
public String ShortCode { get; set; } | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using CountryData.Standard; | ||
using FluentAssertions; | ||
using System; | ||
using Xunit; | ||
|
||
namespace CountryData.UnitTests | ||
{ | ||
public class CountryHelperManagerTests | ||
{ | ||
/// <summary> | ||
/// Tests that GetCountryCode returns the correct country code for a valid country name. | ||
/// </summary> | ||
[Fact] | ||
public void GetCountryCode_ValidCountryName_ReturnsCountryCode() | ||
{ | ||
// Arrange | ||
using var manager = new CountryExtensions(); | ||
var countryName = "United States"; | ||
|
||
// Act | ||
var result = manager.GetCountryCode(countryName); | ||
|
||
// Assert | ||
result.Should().Be("US"); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that GetCountryCode returns null for an invalid country name. | ||
/// </summary> | ||
[Fact] | ||
public void GetCountryCode_InvalidCountryName_ReturnsNull() | ||
{ | ||
// Arrange | ||
using var manager = new CountryExtensions(); | ||
var countryName = "Invalid Country"; | ||
|
||
// Act | ||
var result = manager.GetCountryCode(countryName); | ||
|
||
// Assert | ||
result.Should().BeNull(); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that GetCountryCode throws an ArgumentNullException for a null country name. | ||
/// </summary> | ||
[Fact] | ||
public void GetCountryCode_NullCountryName_ThrowsArgumentNullException() | ||
{ | ||
// Arrange | ||
using var manager = new CountryExtensions(); | ||
string? countryName = null; | ||
|
||
// Act | ||
Action act = () => manager.GetCountryCode(countryName!); | ||
|
||
// Assert | ||
act.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that Dispose method sets the _disposed flag to true. | ||
/// </summary> | ||
[Fact] | ||
public void Dispose_SetsDisposedFlagToTrue() | ||
{ | ||
// Arrange | ||
var manager = new CountryExtensions(); | ||
|
||
// Act | ||
manager.Dispose(); | ||
|
||
// Assert | ||
var disposedField = typeof(CountryExtensions).GetField("_disposed", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
var disposedValue = (bool)disposedField.GetValue(manager); | ||
Check warning on line 75 in test/CountryData.UnitTests/CountryExtensionsTests.cs GitHub Actions / build
|
||
disposedValue.Should().BeTrue(); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that Dispose method sets the _countryHelper to null. | ||
/// </summary> | ||
[Fact] | ||
public void Dispose_SetsCountryHelperToNull() | ||
{ | ||
// Arrange | ||
var manager = new CountryExtensions(); | ||
|
||
// Act | ||
manager.Dispose(); | ||
|
||
// Assert | ||
var countryHelperField = typeof(CountryExtensions).GetField("_countryHelper", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
var countryHelperValue = countryHelperField.GetValue(manager); | ||
countryHelperValue.Should().BeNull(); | ||
} | ||
} | ||
} |