Skip to content

Commit 36361fb

Browse files
authored
Initializer parameter naming (#92)
`jsObject` was technically correct but didnt convey much intent to the user, this was especially apparent after including it in constructor comments. New name is `initializer` to convey what it does. Added some e2e tests for different parameterized constructors too
1 parent 4654f2e commit 36361fb

22 files changed

Lines changed: 537 additions & 277 deletions
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
3+
namespace TypeShim.E2E.Wasm;
4+
5+
[TSExport]
6+
public class IntConstructor(int i)
7+
{
8+
public int Value { get; } = i;
9+
}
10+
11+
[TSExport]
12+
public class StringConstructor(string s)
13+
{
14+
public string Value { get; } = s;
15+
}
16+
17+
[TSExport]
18+
public class MultipleConstructor(int i, string s)
19+
{
20+
public int IntValue { get; } = i;
21+
public string StringValue { get; } = s;
22+
}
23+
24+
[TSExport]
25+
public class ExportedClassConstructor(ExportedClass e)
26+
{
27+
public ExportedClass Value { get; } = e;
28+
}
29+
30+
[TSExport]
31+
public class ExportedClassMultipleConstructor(ExportedClass e, ExportedClass f)
32+
{
33+
public ExportedClass Value { get; } = e;
34+
public ExportedClass Value2 { get; } = f;
35+
}
36+
37+
[TSExport]
38+
public class ExportedClassArrayConstructor(ExportedClass[] e)
39+
{
40+
public ExportedClass[] Value { get; } = e;
41+
}
42+
43+
[TSExport]
44+
public class ExportedClassActionConstructor(Action<ExportedClass> e)
45+
{
46+
public Action<ExportedClass> Value { get; } = e;
47+
}
48+
49+
[TSExport]
50+
public class IntStringMixedConstructor(int i)
51+
{
52+
public int Value { get; } = i;
53+
public required string StringValue { get; set; }
54+
}

src/TypeShim.E2E/vitest/package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { describe, test, expect, beforeEach } from 'vitest';
2+
import {
3+
IntConstructor,
4+
StringConstructor,
5+
MultipleConstructor,
6+
ExportedClass,
7+
ExportedClassConstructor,
8+
ExportedClassMultipleConstructor,
9+
ExportedClassArrayConstructor,
10+
ExportedClassActionConstructor,
11+
IntStringMixedConstructor
12+
} from '@typeshim/e2e-wasm-lib';
13+
14+
describe('Constructors Test', () => {
15+
test('IntConstructor with int parameter', async () => {
16+
const instance = new IntConstructor(100);
17+
expect(instance.Value).toBe(100);
18+
});
19+
20+
test('IntConstructor with different values', async () => {
21+
const instance1 = new IntConstructor(0);
22+
expect(instance1.Value).toBe(0);
23+
24+
const instance2 = new IntConstructor(-50);
25+
expect(instance2.Value).toBe(-50);
26+
27+
const instance3 = new IntConstructor(2147483647);
28+
expect(instance3.Value).toBe(2147483647);
29+
});
30+
31+
test('StringConstructor with string parameter', async () => {
32+
const instance = new StringConstructor('hello');
33+
expect(instance.Value).toBe('hello');
34+
});
35+
36+
test('StringConstructor with different strings', async () => {
37+
const instance1 = new StringConstructor('');
38+
expect(instance1.Value).toBe('');
39+
40+
const instance2 = new StringConstructor('test string');
41+
expect(instance2.Value).toBe('test string');
42+
43+
const instance3 = new StringConstructor('special chars: !@#$%');
44+
expect(instance3.Value).toBe('special chars: !@#$%');
45+
});
46+
47+
test('MultipleConstructor with int and string parameters', async () => {
48+
const instance = new MultipleConstructor(42, 'test');
49+
expect(instance.IntValue).toBe(42);
50+
expect(instance.StringValue).toBe('test');
51+
});
52+
53+
test('MultipleConstructor with various values', async () => {
54+
const instance1 = new MultipleConstructor(0, '');
55+
expect(instance1.IntValue).toBe(0);
56+
expect(instance1.StringValue).toBe('');
57+
58+
const instance2 = new MultipleConstructor(-100, 'negative');
59+
expect(instance2.IntValue).toBe(-100);
60+
expect(instance2.StringValue).toBe('negative');
61+
});
62+
63+
test('ExportedClassConstructor with ExportedClass parameter', async () => {
64+
const exported = new ExportedClass({ Id: 1 });
65+
const instance = new ExportedClassConstructor(exported);
66+
expect(instance.Value).toBe(exported);
67+
});
68+
69+
test('ExportedClassMultipleConstructor with multiple ExportedClass parameters', async () => {
70+
const exported1 = new ExportedClass({ Id: 1 });
71+
const exported2 = new ExportedClass({ Id: 2 });
72+
const instance = new ExportedClassMultipleConstructor(exported1, exported2);
73+
expect(instance.Value).toBe(exported1);
74+
expect(instance.Value2).toBe(exported2);
75+
});
76+
77+
test('ExportedClassArrayConstructor with ExportedClass array parameter', async () => {
78+
const exported1 = new ExportedClass({ Id: 1 });
79+
const exported2 = new ExportedClass({ Id: 2 });
80+
const exported3 = new ExportedClass({ Id: 3 });
81+
const array = [exported1, exported2, exported3];
82+
const instance = new ExportedClassArrayConstructor(array);
83+
expect(instance.Value).toStrictEqual(array);
84+
expect(instance.Value.length).toBe(3);
85+
});
86+
87+
test('ExportedClassArrayConstructor with empty array', async () => {
88+
const array: ExportedClass[] = [];
89+
const instance = new ExportedClassArrayConstructor(array);
90+
expect(instance.Value).toStrictEqual(array);
91+
expect(instance.Value.length).toBe(0);
92+
});
93+
94+
test('ExportedClassActionConstructor with Action<ExportedClass> parameter', async () => {
95+
let callCount = 0;
96+
const action = (obj: ExportedClass) => {
97+
callCount++;
98+
};
99+
const instance = new ExportedClassActionConstructor(action);
100+
instance.Value(new ExportedClass({ Id: 1 }))
101+
expect(callCount).toBe(1);
102+
instance.Value(new ExportedClass({ Id: 2 }))
103+
expect(callCount).toBe(2);
104+
});
105+
106+
test('ExportedClassActionConstructor action is callable', async () => {
107+
let receivedValue: ExportedClass | null = null;
108+
const action = (obj: ExportedClass) => {
109+
receivedValue = obj;
110+
};
111+
const instance = new ExportedClassActionConstructor(action);
112+
const exported = new ExportedClass({ Id: 1 });
113+
instance.Value(exported);
114+
expect(receivedValue).toBe(exported);
115+
});
116+
117+
test('IntStringMixedConstructor with int and string parameters', () =>{
118+
const instance = new IntStringMixedConstructor(42, { StringValue: 'test' });
119+
expect(instance.Value).toBe(42);
120+
expect(instance.StringValue).toBe('test');
121+
})
122+
});

0 commit comments

Comments
 (0)