Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for JP range #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/PostalCodes.UnitTests/PostalCodeRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ private static PostalCodeRange MakeRangeUS(string start, string end)
var endPc = end != null ? new USPostalCode(end) : null;
return new PostalCodeRange(startPc, endPc);
}


private static PostalCodeRange MakeRangeJP(string start, string end)
{
var startPc = start != null ? new JPPostalCode(start) : null;
var endPc = end != null ? new JPPostalCode(end) : null;
return new PostalCodeRange(startPc, endPc);
}

public static IEnumerable<ITestCaseData> CoincidesWithTestCases
{
get
Expand Down Expand Up @@ -231,6 +238,11 @@ public static IEnumerable<ITestCaseData> Contains_WithPostalCode_TestCases
yield return new TestCaseData(rangeUS2, new USPostalCode("28000 1235")).Returns(true);
yield return new TestCaseData(rangeUS3, new USPostalCode("28000")).Returns(true);
yield return new TestCaseData(rangeUS3, new USPostalCode("28000 1235")).Returns(true);

var rangeJP = MakeRangeJP("9000001", "9000006");
yield return new TestCaseData(rangeJP, new JPPostalCode("9000005")).Returns(true);
yield return new TestCaseData(rangeJP, new JPPostalCode("9000-005")).Returns(true);
yield return new TestCaseData(rangeJP, new JPPostalCode("9000007")).Returns(false);
}
}

Expand Down
18 changes: 4 additions & 14 deletions src/PostalCodes/PostalCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ internal PostalCode(PostalCodeFormat[] formats, string redundantCharacters, stri
/// </summary>
/// <param name="other">The other.</param>
/// <returns><c>true</c> if [is adjacent to] [the specified other]; otherwise, <c>false</c>.</returns>
public bool IsAdjacentTo(PostalCode other)
private bool IsAdjacentTo(PostalCode other)
{
return other != null && Predecessor == other || Successor == other;
}
Expand All @@ -152,19 +152,13 @@ public bool IsAdjacentTo(PostalCode other)
/// Gets the predecessor.
/// </summary>
/// <value>The predecessor.</value>
public PostalCode Predecessor
{
get { return PredecessorImpl; }
}
public PostalCode Predecessor => PredecessorImpl;

/// <summary>
/// Gets the successor.
/// </summary>
/// <value>The successor.</value>
public PostalCode Successor
{
get { return SuccessorImpl; }
}
public PostalCode Successor => SuccessorImpl;

/// <summary>
/// Gets the predecessor implementation.
Expand Down Expand Up @@ -474,11 +468,7 @@ public PostalCode ExpandPostalCodeAsHighestInRange()
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
public static bool AreAdjacent(PostalCode left, PostalCode right)
{
if (left == null || right == null)
{
return false;
}
return left.IsAdjacentTo(right) && right.IsAdjacentTo(left);
return left != null && right != null && left.IsAdjacentTo(right) && right.IsAdjacentTo(left);
}

/// <summary>
Expand Down
22 changes: 6 additions & 16 deletions src/PostalCodes/PostalCodeRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@ namespace PostalCodes
/// </summary>
public class PostalCodeRange : IEquatable<PostalCodeRange>, IComparable<PostalCodeRange>, IComparable
{
/// <summary>
/// The lazy default
/// </summary>
private static readonly Lazy<PostalCodeRange> LazyDefault = new Lazy<PostalCodeRange>(() => new PostalCodeRange(null, null));

private PostalCode _start;
private PostalCode _end;

/// <summary>
/// Gets the default.
/// </summary>
/// <value>The default.</value>
public static PostalCodeRange Default
{
get { return LazyDefault.Value; }
}
public static readonly PostalCodeRange Default = new PostalCodeRange(null, null);

/// <summary>
/// Initializes a new instance of the <see cref="PostalCodeRange"/> class.
Expand All @@ -35,19 +27,17 @@ public PostalCodeRange(PostalCode start, PostalCode end)
{
if (start != null && end != null && start.GetType() != end.GetType())
{
throw new ArgumentException(String.Format(
"The start and the end of the range are from incompatible types ('{0}' & '{1}')",
start.GetType(), end.GetType()));
throw new ArgumentException(
$"The start and the end of the range are from incompatible types ('{start.GetType()}' & '{end.GetType()}')");
}

if (end != null && start != null && start > end)
{
throw new ArgumentException(String.Format(
"PostalCodeRange end ({0}) can't be before start ({1})", end, start));
throw new ArgumentException($"PostalCodeRange end ({end}) can't be before start ({start})");
}

_start = start != null ? start.ExpandPostalCodeAsLowestInRange() : null;
_end = end != null ? end.ExpandPostalCodeAsHighestInRange() : null;
_start = start?.ExpandPostalCodeAsLowestInRange();
_end = end?.ExpandPostalCodeAsHighestInRange();
}

/// <summary>
Expand Down