Skip to content
Merged
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
45 changes: 43 additions & 2 deletions src/Ramstack.HtmxToolkit/Internal/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics;

using Ramstack.HtmxToolkit.Configuration;

namespace Ramstack.HtmxToolkit.Internal;
Expand All @@ -15,8 +17,12 @@ internal static class EnumHelper
/// <returns>
/// <c>blob</c> or <c>arraybuffer</c>.
/// </returns>
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";
}

/// <summary>
/// Converts a <see cref="HtmxScrollBehavior" /> value
Expand All @@ -28,6 +34,11 @@ public static string GetWsBinaryTypeValue(this HtmxBinaryType value) =>
/// </returns>
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",
Expand Down Expand Up @@ -59,6 +70,20 @@ public static string GetScrollBehaviorValue(this HtmxScrollBehavior value)
/// </returns>
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",
Expand All @@ -85,6 +110,17 @@ public static string GetSwapValue(this HtmxSwap value)
/// </returns>
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",
Expand All @@ -109,6 +145,11 @@ public static string GetHttpVerbValue(this HttpVerb value)
/// </returns>
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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -23,6 +24,11 @@ internal sealed class HtmxHistoryModeJsonConverter : JsonConverter<HtmxHistoryMo
/// <inheritdoc />
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())
Expand Down
20 changes: 0 additions & 20 deletions tests/Ramstack.HtmxToolkit.Tests/EnumHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}