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
9 changes: 0 additions & 9 deletions src/TypeShim.Generator.Tests/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ public static void EqualOrDiff(
Assert.Fail(message);
}

// Convenience for multi-line sequences (e.g., split lines and compare)
public static void EqualLinesOrDiff(IEnumerable<string> expectedLines, IEnumerable<string> actualLines, string? userMessage = null, int unifiedContext = 2)
{
var expected = string.Join("\n", expectedLines ?? Enumerable.Empty<string>());
var actual = string.Join("\n", actualLines ?? Enumerable.Empty<string>());
EqualOrDiff(expected, actual, normalizeLineEndings: true, trimTrailingWhitespace: false, scrubber: null, unifiedContext: unifiedContext, userMessage: userMessage);
}

// Preprocess input to reduce noise in diffs
private static string Preprocess(string input, bool normalizeLineEndings, bool trimTrailingWhitespace, Func<string, string>? scrubber)
{
var s = input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,42 +915,24 @@ public partial class C1Interop
public static object ctor([JSMarshalAs<JSType.Object>] JSObject jsObject)
{
using var _ = jsObject;
TaskCompletionSource<MyClass> P1Tcs = new();
(jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => {
if (t.IsFaulted) P1Tcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) P1Tcs.SetCanceled();
else P1Tcs.SetResult(MyClassInterop.FromObject(t.Result));
}, TaskContinuationOptions.ExecuteSynchronously);
return new C1()
{
P1 = P1Tcs.Task,
P1 = (jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => MyClassInterop.FromObject(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously),
};
}
[JSExport]
[return: JSMarshalAs<JSType.Promise<JSType.Any>>]
public static Task<object> get_P1([JSMarshalAs<JSType.Any>] object instance)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<object> retValTcs = new();
(typed_instance.P1).ContinueWith(t => {
if (t.IsFaulted) retValTcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) retValTcs.SetCanceled();
else retValTcs.SetResult((object)t.Result);
}, TaskContinuationOptions.ExecuteSynchronously);
return retValTcs.Task;
return typed_instance.P1.ContinueWith(t => (object)t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
}
[JSExport]
[return: JSMarshalAs<JSType.Void>]
public static void set_P1([JSMarshalAs<JSType.Any>] object instance, [JSMarshalAs<JSType.Promise<JSType.Any>>] Task<object> value)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<MyClass> valueTcs = new();
(value).ContinueWith(t => {
if (t.IsFaulted) valueTcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) valueTcs.SetCanceled();
else valueTcs.SetResult(MyClassInterop.FromObject(t.Result));
}, TaskContinuationOptions.ExecuteSynchronously);
Task<MyClass> typed_value = valueTcs.Task;
Task<MyClass> typed_value = value.ContinueWith(t => MyClassInterop.FromObject(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
typed_instance.P1 = typed_value;
}
public static C1 FromObject(object obj)
Expand All @@ -965,15 +947,9 @@ public static C1 FromObject(object obj)
public static C1 FromJSObject(JSObject jsObject)
{
using var _ = jsObject;
TaskCompletionSource<MyClass> P1Tcs = new();
(jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => {
if (t.IsFaulted) P1Tcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) P1Tcs.SetCanceled();
else P1Tcs.SetResult(MyClassInterop.FromObject(t.Result));
}, TaskContinuationOptions.ExecuteSynchronously);
return new C1()
{
P1 = P1Tcs.Task,
P1 = (jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => MyClassInterop.FromObject(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously),
};
}
}
Expand Down Expand Up @@ -1032,42 +1008,24 @@ public partial class C1Interop
public static object ctor([JSMarshalAs<JSType.Object>] JSObject jsObject)
{
using var _ = jsObject;
TaskCompletionSource<MyClass?> P1Tcs = new();
(jsObject.GetPropertyAsObjectNullableTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => {
if (t.IsFaulted) P1Tcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) P1Tcs.SetCanceled();
else P1Tcs.SetResult(t.Result is { } P1TcsVal ? MyClassInterop.FromObject(P1TcsVal) : null);
}, TaskContinuationOptions.ExecuteSynchronously);
return new C1()
{
P1 = P1Tcs.Task,
P1 = (jsObject.GetPropertyAsObjectNullableTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously),
};
}
[JSExport]
[return: JSMarshalAs<JSType.Promise<JSType.Any>>]
public static Task<object?> get_P1([JSMarshalAs<JSType.Any>] object instance)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<object?> retValTcs = new();
(typed_instance.P1).ContinueWith(t => {
if (t.IsFaulted) retValTcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) retValTcs.SetCanceled();
else retValTcs.SetResult(t.Result is { } retValTcsVal ? (object)retValTcsVal : null);
}, TaskContinuationOptions.ExecuteSynchronously);
return retValTcs.Task;
return typed_instance.P1.ContinueWith(t => (object?)t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
}
[JSExport]
[return: JSMarshalAs<JSType.Void>]
public static void set_P1([JSMarshalAs<JSType.Any>] object instance, [JSMarshalAs<JSType.Promise<JSType.Any>>] Task<object?> value)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<MyClass?> valueTcs = new();
(value).ContinueWith(t => {
if (t.IsFaulted) valueTcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) valueTcs.SetCanceled();
else valueTcs.SetResult(t.Result is { } valueTcsVal ? MyClassInterop.FromObject(valueTcsVal) : null);
}, TaskContinuationOptions.ExecuteSynchronously);
Task<MyClass?> typed_value = valueTcs.Task;
Task<MyClass?> typed_value = value.ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
typed_instance.P1 = typed_value;
}
public static C1 FromObject(object obj)
Expand All @@ -1082,19 +1040,13 @@ public static C1 FromObject(object obj)
public static C1 FromJSObject(JSObject jsObject)
{
using var _ = jsObject;
TaskCompletionSource<MyClass?> P1Tcs = new();
(jsObject.GetPropertyAsObjectNullableTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => {
if (t.IsFaulted) P1Tcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) P1Tcs.SetCanceled();
else P1Tcs.SetResult(t.Result is { } P1TcsVal ? MyClassInterop.FromObject(P1TcsVal) : null);
}, TaskContinuationOptions.ExecuteSynchronously);
return new C1()
{
P1 = P1Tcs.Task,
P1 = (jsObject.GetPropertyAsObjectNullableTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously),
};
}
}

""");
}

Expand Down Expand Up @@ -1149,42 +1101,24 @@ public partial class C1Interop
public static object ctor([JSMarshalAs<JSType.Object>] JSObject jsObject)
{
using var _ = jsObject;
TaskCompletionSource<MyClass?>? P1Tcs = jsObject.GetPropertyAsObjectNullableTaskNullable("P1") != null ? new() : null;
jsObject.GetPropertyAsObjectNullableTaskNullable("P1")?.ContinueWith(t => {
if (t.IsFaulted) P1Tcs!.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) P1Tcs!.SetCanceled();
else P1Tcs!.SetResult(t.Result is { } P1TcsVal ? MyClassInterop.FromObject(P1TcsVal) : null);
}, TaskContinuationOptions.ExecuteSynchronously);
return new C1()
{
P1 = P1Tcs?.Task,
P1 = jsObject.GetPropertyAsObjectNullableTaskNullable("P1")?.ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously),
};
}
[JSExport]
[return: JSMarshalAs<JSType.Promise<JSType.Any>>]
public static Task<object?>? get_P1([JSMarshalAs<JSType.Any>] object instance)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<object?>? retValTcs = typed_instance.P1 != null ? new() : null;
typed_instance.P1?.ContinueWith(t => {
if (t.IsFaulted) retValTcs!.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) retValTcs!.SetCanceled();
else retValTcs!.SetResult(t.Result is { } retValTcsVal ? (object)retValTcsVal : null);
}, TaskContinuationOptions.ExecuteSynchronously);
return retValTcs?.Task;
return typed_instance.P1?.ContinueWith(t => (object?)t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
}
[JSExport]
[return: JSMarshalAs<JSType.Void>]
public static void set_P1([JSMarshalAs<JSType.Any>] object instance, [JSMarshalAs<JSType.Promise<JSType.Any>>] Task<object?>? value)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<MyClass?>? valueTcs = value != null ? new() : null;
value?.ContinueWith(t => {
if (t.IsFaulted) valueTcs!.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) valueTcs!.SetCanceled();
else valueTcs!.SetResult(t.Result is { } valueTcsVal ? MyClassInterop.FromObject(valueTcsVal) : null);
}, TaskContinuationOptions.ExecuteSynchronously);
Task<MyClass?>? typed_value = valueTcs?.Task;
Task<MyClass?>? typed_value = value?.ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
typed_instance.P1 = typed_value;
}
public static C1 FromObject(object obj)
Expand All @@ -1199,19 +1133,13 @@ public static C1 FromObject(object obj)
public static C1 FromJSObject(JSObject jsObject)
{
using var _ = jsObject;
TaskCompletionSource<MyClass?>? P1Tcs = jsObject.GetPropertyAsObjectNullableTaskNullable("P1") != null ? new() : null;
jsObject.GetPropertyAsObjectNullableTaskNullable("P1")?.ContinueWith(t => {
if (t.IsFaulted) P1Tcs!.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) P1Tcs!.SetCanceled();
else P1Tcs!.SetResult(t.Result is { } P1TcsVal ? MyClassInterop.FromObject(P1TcsVal) : null);
}, TaskContinuationOptions.ExecuteSynchronously);
return new C1()
{
P1 = P1Tcs?.Task,
P1 = jsObject.GetPropertyAsObjectNullableTaskNullable("P1")?.ContinueWith(t => t.Result is { } tVal ? MyClassInterop.FromObject(tVal) : null, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously),
};
}
}

""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,9 @@ public partial class C1Interop
public static object ctor([JSMarshalAs<JSType.Object>] JSObject jsObject)
{
using var _ = jsObject;
TaskCompletionSource<{{typeName}}> P1Tcs = new();
(jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => {
if (t.IsFaulted) P1Tcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) P1Tcs.SetCanceled();
else P1Tcs.SetResult(({{typeName}})t.Result);
}, TaskContinuationOptions.ExecuteSynchronously);
return new C1()
{
P1 = P1Tcs.Task,
P1 = (jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => ({{typeName}})t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously),
P2 = jsObject.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(jsObject)),
};
}
Expand All @@ -248,26 +242,14 @@ public static object ctor([JSMarshalAs<JSType.Object>] JSObject jsObject)
public static Task<object> get_P1([JSMarshalAs<JSType.Any>] object instance)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<object> retValTcs = new();
(typed_instance.P1).ContinueWith(t => {
if (t.IsFaulted) retValTcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) retValTcs.SetCanceled();
else retValTcs.SetResult((object)t.Result);
}, TaskContinuationOptions.ExecuteSynchronously);
return retValTcs.Task;
return typed_instance.P1.ContinueWith(t => (object)t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
}
[JSExport]
[return: JSMarshalAs<JSType.Void>]
public static void set_P1([JSMarshalAs<JSType.Any>] object instance, [JSMarshalAs<JSType.Promise<JSType.Any>>] Task<object> value)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<{{typeName}}> valueTcs = new();
(value).ContinueWith(t => {
if (t.IsFaulted) valueTcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) valueTcs.SetCanceled();
else valueTcs.SetResult(({{typeName}})t.Result);
}, TaskContinuationOptions.ExecuteSynchronously);
Task<{{typeName}}> typed_value = valueTcs.Task;
Task<{{typeName}}> typed_value = value.ContinueWith(t => ({{typeName}})t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
typed_instance.P1 = typed_value;
}
[JSExport]
Expand Down Expand Up @@ -296,15 +278,9 @@ public static C1 FromObject(object obj)
public static C1 FromJSObject(JSObject jsObject)
{
using var _ = jsObject;
TaskCompletionSource<{{typeName}}> P1Tcs = new();
(jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => {
if (t.IsFaulted) P1Tcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) P1Tcs.SetCanceled();
else P1Tcs.SetResult(({{typeName}})t.Result);
}, TaskContinuationOptions.ExecuteSynchronously);
return new C1()
{
P1 = P1Tcs.Task,
P1 = (jsObject.GetPropertyAsObjectTaskNullable("P1") ?? throw new ArgumentException("Non-nullable property 'P1' missing or of invalid type", nameof(jsObject))).ContinueWith(t => ({{typeName}})t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously),
P2 = jsObject.GetPropertyAsInt32Nullable("P2") ?? throw new ArgumentException("Non-nullable property 'P2' missing or of invalid type", nameof(jsObject)),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public Task<MyClass> M1()
RenderContext renderContext = new(classInfo, [classInfo], RenderOptions.CSharp);
string interopClass = new CSharpInteropClassRenderer(classInfo, renderContext, new JSObjectMethodResolver([])).Render();

Assert.That(interopClass, Is.EqualTo("""
AssertEx.EqualOrDiff(interopClass, """
#nullable enable
// TypeShim generated TypeScript interop definitions
using System;
Expand All @@ -196,13 +196,7 @@ public partial class C1Interop
public static Task<object> M1([JSMarshalAs<JSType.Any>] object instance)
{
C1 typed_instance = (C1)instance;
TaskCompletionSource<object> retValTcs = new();
(typed_instance.M1()).ContinueWith(t => {
if (t.IsFaulted) retValTcs.SetException(t.Exception.InnerExceptions);
else if (t.IsCanceled) retValTcs.SetCanceled();
else retValTcs.SetResult((object)t.Result);
}, TaskContinuationOptions.ExecuteSynchronously);
return retValTcs.Task;
return typed_instance.M1().ContinueWith(t => (object)t.Result, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously);
}
public static C1 FromObject(object obj)
{
Expand All @@ -214,7 +208,7 @@ public static C1 FromObject(object obj)
}
}

"""));
""");
}

[TestCase("Version", "new Version(1,2,3,4)")]
Expand Down
Loading
Loading