diff --git a/src/Humanizer.Tests/HeadingTests.cs b/src/Humanizer.Tests.Shared/HeadingTests.cs similarity index 90% rename from src/Humanizer.Tests/HeadingTests.cs rename to src/Humanizer.Tests.Shared/HeadingTests.cs index 19234c219..45b241eb3 100644 --- a/src/Humanizer.Tests/HeadingTests.cs +++ b/src/Humanizer.Tests.Shared/HeadingTests.cs @@ -54,8 +54,10 @@ public class HeadingTests [InlineData(348.7, "NNW")] [InlineData(348.8, "N")] [InlineData(720, "N")] - public void ToHeadingShort(double heading, string expected) { - Assert.Equal(heading.ToHeading(true), expected); + [Theory] + public void ToHeadingAbbreviated(double heading, string expected) + { + Assert.Equal(expected, heading.ToHeading()); } [InlineData(0, "north")] @@ -75,8 +77,10 @@ public void ToHeadingShort(double heading, string expected) { [InlineData(315, "northwest")] [InlineData(337.5, "north-northwest")] [InlineData(720, "north")] - public void ToHeading(double heading, string expected) { - Assert.Equal(heading.ToHeading(), expected); + [Theory] + public void ToHeading(double heading, string expected) + { + Assert.Equal(expected, heading.ToHeading(HeadingStyle.Full)); } [InlineData("N" , 0)] @@ -95,8 +99,10 @@ public void ToHeading(double heading, string expected) { [InlineData("WNW", 292.5)] [InlineData("NW" , 315)] [InlineData("NNW", 337.5)] - public void FromShortHeading(string heading, double expected) { - Assert.Equal(heading.FromShortHeading(), expected); + [Theory] + public void FromShortHeading(string heading, double expected) + { + Assert.Equal(expected, heading.FromAbbreviatedHeading()); } [InlineData(0, '↑')] @@ -147,8 +153,10 @@ public void FromShortHeading(string heading, double expected) { [InlineData(337.5, '↑')] [InlineData(348.7, '↑')] [InlineData(348.8, '↑')] - public void ToHeadignArrow(double heading, char expected) { - Assert.Equal(heading.ToHeadingArrow(), expected); + [Theory] + public void ToHeadignArrow(double heading, char expected) + { + Assert.Equal(expected, heading.ToHeadingArrow()); } } } diff --git a/src/Humanizer.Tests.Shared/Humanizer.Tests.Shared.projitems b/src/Humanizer.Tests.Shared/Humanizer.Tests.Shared.projitems index d2be62186..3ce14e990 100644 --- a/src/Humanizer.Tests.Shared/Humanizer.Tests.Shared.projitems +++ b/src/Humanizer.Tests.Shared/Humanizer.Tests.Shared.projitems @@ -31,6 +31,7 @@ + diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index d38570ab1..324b993d3 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -166,10 +166,15 @@ namespace Humanizer } public class static HeadingExtensions { - public static double FromShortHeading(this string heading) { } - public static string ToHeading(this double heading, bool shortResult = False, System.Globalization.CultureInfo culture = null) { } + public static double FromAbbreviatedHeading(this string heading) { } + public static string ToHeading(this double heading, Humanizer.HeadingStyle style = 0, System.Globalization.CultureInfo culture = null) { } public static char ToHeadingArrow(this double heading) { } } + public enum HeadingStyle + { + Abbreviated = 0, + Full = 1, + } public class In { public In() { } diff --git a/src/Humanizer/HeadingExtensions.cs b/src/Humanizer/HeadingExtensions.cs index 7ff211ca0..6ee03cbd3 100644 --- a/src/Humanizer/HeadingExtensions.cs +++ b/src/Humanizer/HeadingExtensions.cs @@ -4,6 +4,22 @@ namespace Humanizer { + /// + /// Style for the cardinal direction humanization + /// + public enum HeadingStyle + { + /// + /// Returns an abbreviated format + /// + Abbreviated, + + /// + /// Returns the full format + /// + Full + } + /// /// Contains extensions to transform a number indicating a heading into the /// textual representation of the heading. @@ -21,13 +37,14 @@ public static class HeadingExtensions /// /// A textual representation of the heading /// The culture to return the textual representation in - /// Whether to return a short result or not - public static string ToHeading(this double heading, bool shortResult = false, CultureInfo culture = null) { + /// Whether to return a short result or not. + public static string ToHeading(this double heading, HeadingStyle style = HeadingStyle.Abbreviated, CultureInfo culture = null) { int val = (int)((heading / 22.5) + .5); string result = headings[val % 16]; - if (shortResult) return Resources.GetResource($"{result}_Short", culture); + if (style == HeadingStyle.Abbreviated) + return Resources.GetResource($"{result}_Short", culture); result = Resources.GetResource(result, culture); return result; @@ -49,7 +66,7 @@ public static char ToHeadingArrow(this double heading) { /// Returns a heading based on the short textual representation of the heading. /// /// The heading. -1 if the heading could not be parsed. - public static double FromShortHeading(this string heading) { + public static double FromAbbreviatedHeading(this string heading) { var index = Array.IndexOf(headings, heading.ToUpperInvariant()); if (index == -1) return -1; diff --git a/src/Humanizer/Properties/Resources.resx b/src/Humanizer/Properties/Resources.resx index 53ac5b5de..678f0d5cf 100644 --- a/src/Humanizer/Properties/Resources.resx +++ b/src/Humanizer/Properties/Resources.resx @@ -628,51 +628,51 @@ north-northwest - n + N - nne + NNE - ne + NE - ene + ENE - e + E - ese + ESE - se + SE - sse + SSE - s + S - ssw + SSW - sw + SW - wsw + WSW - w + W - wnw + WNW - nw + NW - nnw + NNW - + \ No newline at end of file