-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathfunction.test.ts
More file actions
172 lines (140 loc) · 5.5 KB
/
function.test.ts
File metadata and controls
172 lines (140 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { attest } from '@ark/attest';
import { describe, expect, expectTypeOf, it } from 'vitest';
import type { InferIO, InheritArgNames, IOLayout } from '../src/core/function/fnTypes.ts';
import * as d from '../src/data/index.ts';
import { Void } from '../src/data/wgslTypes.ts';
import tgpu, { type TgpuFn, type TgpuFnShell } from 'typegpu';
import type { Prettify } from '../src/shared/utilityTypes.ts';
const empty = tgpu.fn([])`() {
// do nothing
}`;
describe('tgpu.fn', () => {
it('should inject function declaration', () => {
expect(tgpu.resolve([empty])).toMatchInlineSnapshot(`
"fn empty() {
// do nothing
}"
`);
});
it('should inject function declaration only once', () => {
const main = tgpu.fn([])`() { empty(); empty(); }`.$uses({ empty });
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
"fn empty() {
// do nothing
}
fn main() { empty(); empty(); }"
`);
});
it('should inject function declaration only once (calls are nested)', () => {
const nestedA = tgpu.fn([])`() { empty(); }`.$uses({ empty });
const nestedB = tgpu.fn([])`() { empty(); }`.$uses({ empty });
const main = tgpu.fn([])`() { nestedA(); nestedB(); }`.$uses({ nestedA, nestedB });
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
"fn empty() {
// do nothing
}
fn nestedA() { empty(); }
fn nestedB() { empty(); }
fn main() { nestedA(); nestedB(); }"
`);
});
it('creates typed shell from parameters', () => {
const proc = tgpu.fn([]);
const one = tgpu.fn([d.f32]);
const two = tgpu.fn([d.f32, d.u32]);
expectTypeOf(proc).toEqualTypeOf<TgpuFnShell<[], d.Void>>();
expectTypeOf<ReturnType<typeof proc>>().toEqualTypeOf<TgpuFn<() => d.Void>>();
expectTypeOf(one).toEqualTypeOf<TgpuFnShell<[d.F32], d.Void>>();
expectTypeOf<ReturnType<typeof one>>().toEqualTypeOf<TgpuFn<(arg_0: d.F32) => d.Void>>();
expectTypeOf(two).toEqualTypeOf<TgpuFnShell<[d.F32, d.U32], d.Void>>();
expectTypeOf<ReturnType<typeof two>>().toEqualTypeOf<
TgpuFn<(arg_0: d.F32, arg_1: d.U32) => d.Void>
>();
});
it('creates typed shell from parameters and return type', () => {
const proc = tgpu.fn([], d.bool);
const one = tgpu.fn([d.f32], d.bool);
const two = tgpu.fn([d.f32, d.u32], d.bool);
expectTypeOf(proc).toEqualTypeOf<TgpuFnShell<[], d.Bool>>();
expectTypeOf<ReturnType<typeof proc>>().toEqualTypeOf<TgpuFn<() => d.Bool>>();
expectTypeOf(one).toEqualTypeOf<TgpuFnShell<[d.F32], d.Bool>>();
expectTypeOf<ReturnType<typeof one>>().toEqualTypeOf<TgpuFn<(arg_0: d.F32) => d.Bool>>();
expectTypeOf(two).toEqualTypeOf<TgpuFnShell<[d.F32, d.U32], d.Bool>>();
expectTypeOf<ReturnType<typeof two>>().toEqualTypeOf<
TgpuFn<(arg_0: d.F32, arg_1: d.U32) => d.Bool>
>();
});
});
describe('tgpu.computeFn', () => {
it('does not create In struct when the are no arguments', () => {
const foo = tgpu.computeFn({ workgroupSize: [1] })(() => {
const x = 2;
});
expect(tgpu.resolve([foo])).not.toContain('struct');
expect(foo.shell.argTypes).toStrictEqual([]);
});
it('does not create In struct when there is empty object for arguments', () => {
const foo = tgpu.computeFn({ in: {}, workgroupSize: [1] })(() => {
const x = 2;
});
expect(tgpu.resolve([foo])).not.toContain('struct');
expect(foo.shell.argTypes).toStrictEqual([]);
});
});
describe('tgpu.vertexFn', () => {
it('does not create In struct when the are no arguments', () => {
const foo = tgpu.vertexFn({
out: { pos: d.builtin.position },
})(() => ({
pos: d.vec4f(),
}));
expect(tgpu.resolve([foo])).not.toContain('struct foo_In');
expect(tgpu.resolve([foo])).toContain('struct foo_Out');
expect(foo.shell.argTypes).toStrictEqual([]);
});
it('does not create In struct when there is empty object for arguments', () => {
const foo = tgpu.vertexFn({
in: {},
out: { pos: d.builtin.position },
})(() => {
return {
pos: d.vec4f(),
};
});
expect(tgpu.resolve([foo])).not.toContain('struct foo_In');
expect(tgpu.resolve([foo])).toContain('struct foo_Out');
expect(foo.shell.argTypes).toStrictEqual([]);
});
});
describe('tgpu.fragmentFn', () => {
it('does not create Out struct when the are no output parameters', () => {
const foo = tgpu.fragmentFn({ out: Void })(() => {});
expect(tgpu.resolve([foo])).not.toContain('struct foo_Out');
});
});
describe('InferIO', () => {
it('unwraps f32', () => {
const layout = d.f32 satisfies IOLayout;
expectTypeOf<InferIO<typeof layout>>().toEqualTypeOf<number>();
});
it('unwraps a record of numeric primitives', () => {
const layout = { a: d.f32, b: d.location(2, d.u32) } satisfies IOLayout;
expectTypeOf<InferIO<typeof layout>>().toEqualTypeOf<{
a: number;
b: number;
}>();
});
});
describe('InheritArgNames', () => {
it('should inherit argument names from one fn to another', () => {
const isEven = (x: number) => (x & 1) === 0;
const identity = (num: number) => num;
// Should have the same argument names as `identity`, but the signature of `isEven`
const isEvenWithNames = undefined as unknown as Prettify<
InheritArgNames<typeof isEven, typeof identity>
>['result'];
attest(isEven).type.toString.snap('(x: number) => boolean');
attest(identity).type.toString.snap('(num: number) => number');
attest(isEvenWithNames).type.toString.snap('(num: number) => boolean');
});
});