Skip to content

Commit

Permalink
Validations around lat/long values
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Oct 11, 2012
1 parent c66c3eb commit df75782
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
39 changes: 39 additions & 0 deletions SolrNet.Tests/LocationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,45 @@ public static IEnumerable<Test> Tests() {
Assert.AreEqual(x.location.Longitude, parsedLocation.Longitude);
});
}

var invalidLatitudes = new[] {-100, 120};
foreach (var x in invalidLatitudes) {
var latitude = x;

yield return new TestCase("Latitude " + latitude + " is invalid",
() => Assert.IsFalse(Location.IsValidLatitude(latitude)));

yield return new TestCase("Invalid latitude throws: " + latitude,
() => Assert.Throws<ArgumentOutOfRangeException>(() => new Location(latitude, 0)));
}

var invalidLongitudes = new[] {-200, 999};
foreach (var x in invalidLongitudes) {
var longitude = x;

yield return new TestCase("Longitude " + longitude + " is invalid",
() => Assert.IsFalse(Location.IsValidLongitude(longitude)));

yield return new TestCase("Invalid longitude throws: " + longitude,
() => Assert.Throws<ArgumentOutOfRangeException>(() => new Location(0, longitude)));
}

yield return new TestCase("TryCreate returns null with invalid lat/long", () => {
foreach (var lat in invalidLatitudes)
foreach (var lng in invalidLongitudes) {
var loc = Location.TryCreate(lat, lng);
Assert.IsNull(loc);
}
});

yield return new TestCase("TryCreate returns non-null with valid lat/long", () => {
foreach (var l in locations) {
var loc = l.location;
var loc2 = Location.TryCreate(loc.Latitude, loc.Longitude);
Assert.IsNotNull(loc2);
Assert.AreEqual(loc, loc2);
}
});
}
}
}
40 changes: 38 additions & 2 deletions SolrNet/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,49 @@ public class Location : IEquatable<Location>, IFormattable {
/// <summary>
/// Represents a Latitude/Longitude as a 2 dimensional point.
/// </summary>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
/// <param name="latitude">Value between -90 and 90</param>
/// <param name="longitude">Value between -180 and 180</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="latitude"/> or <paramref name="longitude"/> are invalid</exception>
public Location(double latitude, double longitude) {
if (!IsValidLatitude(latitude))
throw new ArgumentOutOfRangeException(string.Format(CultureInfo.InvariantCulture, "Invalid latitude '{0}'. Valid values are between -90 and 90", latitude));
if (!IsValidLongitude(longitude))
throw new ArgumentOutOfRangeException(string.Format(CultureInfo.InvariantCulture, "Invalid longitude '{0}'. Valid values are between -180 and 180", longitude));
Latitude = latitude;
Longitude = longitude;
}

/// <summary>
/// True if <paramref name="latitude"/> is a valid latitude. Otherwise false.
/// </summary>
/// <param name="latitude"></param>
/// <returns></returns>
public static bool IsValidLatitude(double latitude) {
return latitude >= -90 && latitude <= 90;
}

/// <summary>
/// True if <paramref name="longitude"/> is a valid longitude. Otherwise false.
/// </summary>
/// <param name="longitude"></param>
/// <returns></returns>
public static bool IsValidLongitude(double longitude) {
return longitude >= -180 && longitude <= 180;
}

/// <summary>
/// Try to create a <see cref="Location"/>.
/// Return <value>null</value> if either <paramref name="latitude"/> or <paramref name="longitude"/> are invalid.
/// </summary>
/// <param name="latitude">Value between -90 and 90</param>
/// <param name="longitude">Value between -180 and 180</param>
/// <returns></returns>
public static Location TryCreate(double latitude, double longitude) {
if (IsValidLatitude(latitude) && IsValidLongitude(longitude))
return new Location(latitude, longitude);
return null;
}

public override string ToString() {
return string.Format(CultureInfo.InvariantCulture, "{0},{1}", Latitude, Longitude);
}
Expand Down

0 comments on commit df75782

Please sign in to comment.