Skip to content

Commit

Permalink
fix enum idx (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
elringus authored Jan 5, 2024
1 parent d594dc1 commit 6de6283
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/cs/Bootsharp.Publish.Test/Pack/BindingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,20 @@ public void ExportedEnumsAreDeclaredInJS ()
};
""");
}

[Fact]
public void CustomEnumIndexesArePreservedInJS ()
{
AddAssembly(
WithClass("n", "public enum Foo { A = 1, B = 6 }"),
WithClass("n", "[JSInvokable] public static Foo GetFoo () => default;"));
Execute();
Contains(
"""
export const n = {
getFoo: () => deserialize(getExports().n_MockClass.GetFoo()),
Foo: { "1": "A", "6": "B", "A": 1, "B": 6 }
};
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ private void EmitEvent (MethodMeta method)

private void EmitEnum (Type @enum)
{
var values = Enum.GetNames(@enum);
var fields = string.Join(", ",
values.Select((v, i) => $"\"{i}\": \"{v}\"")
.Concat(values.Select((v, i) => $"\"{v}\": {i}")));
var values = Enum.GetValuesAsUnderlyingType(@enum).Cast<object>().ToArray();
var fields = string.Join(", ", values
.Select(v => $"\"{v}\": \"{Enum.GetName(@enum, v)}\"")
.Concat(values.Select(v => $"\"{Enum.GetName(@enum, v)}\": {v}")));
builder.Append($"{Comma()}\n{Pad(level + 1)}{@enum.Name}: {{ {fields} }}");
}

Expand Down
4 changes: 2 additions & 2 deletions src/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
},
"devDependencies": {
"typescript": "^5.3.3",
"vitest": "^1.1.1",
"@vitest/coverage-v8": "^1.1.1",
"vitest": "^1.1.3",
"@vitest/coverage-v8": "^1.1.3",
"@types/node": "^20.10.6",
"ws": "^8.16.0"
}
Expand Down
7 changes: 7 additions & 0 deletions src/js/test/cs/Test/IdxEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Test;

public enum IdxEnum
{
One = 1,
Two = 2
}
3 changes: 3 additions & 0 deletions src/js/test/cs/Test/Invokable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ public static async Task<string> JoinStringsAsync (string a, string b)

[JSInvokable]
public static string BytesToString (byte[] bytes) => Encoding.UTF8.GetString(bytes);

[JSInvokable]
public static IdxEnum GetIdxEnumOne () => IdxEnum.One;
}
4 changes: 4 additions & 0 deletions src/js/test/spec/interop.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,8 @@ describe("while bootsharp is booted", () => {
expect(TrackType[TrackType.Rubber]).toStrictEqual("Rubber");
expect(TrackType[TrackType.Chain]).toStrictEqual("Chain");
});
it("can compare indexed enums", () => {
expect(Test.getIdxEnumOne() === Test.IdxEnum.One).toBeTruthy();
expect(Test.getIdxEnumOne() === Test.IdxEnum.Two).not.toBeTruthy();
});
});

0 comments on commit 6de6283

Please sign in to comment.