diff --git a/src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs b/src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs index 8a13b89..9f60888 100644 --- a/src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs +++ b/src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; + using Ramstack.HtmxToolkit.Configuration; namespace Ramstack.HtmxToolkit.Internal; @@ -15,8 +17,12 @@ internal static class EnumHelper /// /// blob or arraybuffer. /// - public static string GetWsBinaryTypeValue(this HtmxBinaryType value) => - value == HtmxBinaryType.Blob ? "blob" : "arraybuffer"; + public static string GetWsBinaryTypeValue(this HtmxBinaryType value) + { + Debug.Assert(value is HtmxBinaryType.Blob or HtmxBinaryType.ArrayBuffer); + + return value == HtmxBinaryType.Blob ? "blob" : "arraybuffer"; + } /// /// Converts a value @@ -28,6 +34,11 @@ public static string GetWsBinaryTypeValue(this HtmxBinaryType value) => /// public static string GetScrollBehaviorValue(this HtmxScrollBehavior value) { + Debug.Assert(value + is HtmxScrollBehavior.Auto + or HtmxScrollBehavior.Smooth + or HtmxScrollBehavior.Instant); + return value switch { HtmxScrollBehavior.Auto => "auto", @@ -59,6 +70,20 @@ public static string GetScrollBehaviorValue(this HtmxScrollBehavior value) /// public static string GetSwapValue(this HtmxSwap value) { + Debug.Assert(value + is HtmxSwap.InnerHtml + or HtmxSwap.OuterHtml + or HtmxSwap.InnerMorph + or HtmxSwap.OuterMorph + or HtmxSwap.OuterSync + or HtmxSwap.TextContent + or HtmxSwap.BeforeBegin + or HtmxSwap.AfterBegin + or HtmxSwap.BeforeEnd + or HtmxSwap.AfterEnd + or HtmxSwap.Delete + or HtmxSwap.None); + return value switch { HtmxSwap.InnerHtml => "innerHTML", @@ -85,6 +110,17 @@ public static string GetSwapValue(this HtmxSwap value) /// public static string GetHttpVerbValue(this HttpVerb value) { + Debug.Assert(value + is HttpVerb.Get + or HttpVerb.Head + or HttpVerb.Post + or HttpVerb.Put + or HttpVerb.Delete + or HttpVerb.Connect + or HttpVerb.Options + or HttpVerb.Trace + or HttpVerb.Patch); + return value switch { HttpVerb.Get => "get", @@ -109,6 +145,11 @@ public static string GetHttpVerbValue(this HttpVerb value) /// public static string GetFetchModeValue(this HtmxFetchMode value) { + Debug.Assert(value + is HtmxFetchMode.SameOrigin + or HtmxFetchMode.Cors + or HtmxFetchMode.NoCors); + return value switch { HtmxFetchMode.SameOrigin => "same-origin", diff --git a/src/Ramstack.HtmxToolkit/Serialization/HtmxHistoryModeJsonConverter.cs b/src/Ramstack.HtmxToolkit/Serialization/HtmxHistoryModeJsonConverter.cs index 7321292..85463ae 100644 --- a/src/Ramstack.HtmxToolkit/Serialization/HtmxHistoryModeJsonConverter.cs +++ b/src/Ramstack.HtmxToolkit/Serialization/HtmxHistoryModeJsonConverter.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Text.Json; using System.Text.Json.Serialization; @@ -23,6 +24,11 @@ internal sealed class HtmxHistoryModeJsonConverter : JsonConverter public override void Write(Utf8JsonWriter writer, HtmxHistoryMode? value, JsonSerializerOptions options) { + Debug.Assert(value is null + or HtmxHistoryMode.Enabled + or HtmxHistoryMode.Disabled + or HtmxHistoryMode.Reload); + // NOTE: value is never null here: null-valued properties are omitted // by JsonIgnoreCondition.WhenWritingNull before this converter is invoked. switch (value.GetValueOrDefault()) diff --git a/tests/Ramstack.HtmxToolkit.Tests/EnumHelperTests.cs b/tests/Ramstack.HtmxToolkit.Tests/EnumHelperTests.cs index 486dcc5..c54f231 100644 --- a/tests/Ramstack.HtmxToolkit.Tests/EnumHelperTests.cs +++ b/tests/Ramstack.HtmxToolkit.Tests/EnumHelperTests.cs @@ -86,24 +86,4 @@ public void GetScrollBehaviorValue_ReturnsExpectedString(HtmxScrollBehavior valu [TestCase(HtmxFetchMode.NoCors, "no-cors")] public void GetFetchModeValue_ReturnsExpectedString(HtmxFetchMode value, string expected) => Assert.That(value.GetFetchModeValue(), Is.EqualTo(expected)); - - [Test] - public void GetSwapValue_ReturnsNone_ForUndefinedValue() => - Assert.That(((HtmxSwap)999).GetSwapValue(), Is.EqualTo("none")); - - [Test] - public void GetHttpVerbValue_ReturnsPatch_ForUndefinedValue() => - Assert.That(((HttpVerb)999).GetHttpVerbValue(), Is.EqualTo("patch")); - - [Test] - public void GetScrollBehaviorValue_ReturnsInstant_ForUndefinedValue() => - Assert.That(((HtmxScrollBehavior)999).GetScrollBehaviorValue(), Is.EqualTo("instant")); - - [Test] - public void GetWsBinaryTypeValue_ReturnsArrayBuffer_ForUndefinedValue() => - Assert.That(((HtmxBinaryType)999).GetWsBinaryTypeValue(), Is.EqualTo("arraybuffer")); - - [Test] - public void GetFetchModeValue_ReturnsNoCors_ForUndefinedValue() => - Assert.That(((HtmxFetchMode)999).GetFetchModeValue(), Is.EqualTo("no-cors")); }