-
Notifications
You must be signed in to change notification settings - Fork 8
/
wit_generator_test.dart
240 lines (216 loc) · 7.05 KB
/
wit_generator_test.dart
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import 'dart:convert';
import 'dart:io';
import 'package:dart_style/dart_style.dart';
import 'package:test/test.dart';
import 'package:wasm_wit_component/generator.dart';
// ignore: implementation_imports
import 'package:wasm_wit_component/src/generate_cli.dart';
import 'package:wasm_wit_component/wasm_wit_component.dart';
import 'package:wasm_wit_component_example/host_wit_generation.dart';
import 'package:wasm_wit_component_example/test_utils.dart';
final _formatter = DartFormatter();
void witDartGeneratorTests({Future<Directory> Function()? getDirectory}) {
group('wit generator', () {
test('generate cli', testOn: 'windows || mac-os || linux', () async {
if (Platform.isAndroid || Platform.isIOS) return;
final root = getRootDirectory();
final base = root.uri.resolve('packages/dart_wit_component/');
final output = File.fromUri(
base.resolve('wasm_wit_component/test/temp/generator.dart'),
);
try {
// 'dart run wasm_wit_component/bin/generate.dart wit/dart-wit-generator.wit wasm_wit_component/lib/src/generator.dart'
await generateCli([
base.resolve('wit/dart-wit-generator.wit').toFilePath(),
output.path,
]);
final content = await output.readAsString();
final formatted = _formatter.format(content);
final expected = await File.fromUri(
base.resolve('wasm_wit_component/lib/src/generator.dart'),
).readAsString();
expect(formatted, expected.replaceAll('\r\n', '\n'));
} finally {
if (output.existsSync()) {
output.deleteSync();
}
}
});
group('cli args', () {
test('--no-default', () {
const pathToWit = 'wit/file.wit';
const pathToDartFile = 'lib/file.dart';
final args = GeneratorCLIArgs.fromArgs(
[pathToWit, pathToDartFile, '--no-default', '--json-serialization'],
);
expect(
args,
const GeneratorCLIArgs(
dartFilePath: pathToDartFile,
watch: false,
witInputPath: pathToWit,
config: WitGeneratorConfig(
inputs: FileSystemPaths(inputPath: pathToWit),
jsonSerialization: true,
copyWith_: false,
equalityAndHashCode: false,
toString_: false,
fileHeader: null,
generateDocs: false,
int64Type: Int64TypeConfig.bigInt,
useNullForOption: true,
requiredOption: false,
typedNumberLists: true,
asyncWorker: false,
sameClassUnion: true,
),
),
);
});
test('--no-copy-with --json-serialization=false', () {
const pathToWit = 'wit/file.wit';
final args = GeneratorCLIArgs.fromArgs(
[
pathToWit,
'--no-copy-with',
'--watch',
'--json-serialization=false',
],
);
expect(
args,
const GeneratorCLIArgs(
dartFilePath: null,
watch: true,
witInputPath: pathToWit,
config: WitGeneratorConfig(
inputs: FileSystemPaths(inputPath: pathToWit),
jsonSerialization: false,
copyWith_: false,
equalityAndHashCode: true,
toString_: true,
fileHeader: null,
generateDocs: true,
int64Type: Int64TypeConfig.bigInt,
useNullForOption: true,
requiredOption: false,
typedNumberLists: true,
asyncWorker: false,
sameClassUnion: true,
),
),
);
});
test('error messages', () {
void hasError(List<String> args, String error) {
expect(
() => GeneratorCLIArgs.fromArgs(args),
throwsA(predicate((e) => e.toString().contains(error))),
);
}
hasError(
['wit/file.wit', '-no-copy-with'],
'Invalid argument (1, -no-copy-with). Should be --<name>',
);
hasError(
['file.wit', '--no-copy-with', '--copy-with'],
'Duplicate argument (2, --copy-with).',
);
hasError(
['/wit/file.wit', '--watch', '--copy-with=no'],
'Invalid argument (2, --copy-with=no). Should be true or false.',
);
hasError(
['--copy-with=true'],
'Missing positional argument `witInputPath`.',
);
});
});
const hostWitContents = '''
package host-namespace:host-pkg
world host {
import print: func(msg: string)
record record-test {
a: u32,
b: string,
c: float64,
}
export run: func()
export get: func() -> record-test
export map: func(rec: record-test) -> record-test
export map-i: func(rec: record-test, i: float32) -> record-test
export receive-i: func(rec: record-test, i: float32)
}
''';
test('file system input', () async {
const isWeb = identical(0, 0.0);
final String witPath;
if (!isWeb && getDirectory != null) {
final dir = await getDirectory();
final file = File(
dir.uri.resolve('host.wit').toFilePath(windows: Platform.isWindows),
)
..createSync(recursive: true)
..writeAsStringSync(hostWitContents);
addTearDown(file.deleteSync);
witPath = file.path;
} else {
witPath = isWeb
? 'host/host.wit'
: getRootDirectory()
.uri
.resolve(
'packages/dart_wit_component/wasm_wit_component/example/lib/host.wit',
)
.toFilePath();
}
final wasiConfig = wasiConfigFromPath(
witPath,
webBrowserFileSystem: {
if (isWeb)
'host': WasiDirectory({
'host.wit': WasiFile(
const Utf8Encoder().convert(hostWitContents),
)
}),
},
);
final g = await createDartWitGenerator(wasiConfig: wasiConfig);
final inputs = FileSystemPaths(
inputPath: isWeb
? witPath
: '${wasiConfig.preopenedDirs.first.wasmGuestPath}host.wit',
);
_validateHostResult(g, inputs);
});
test('in memory input', () async {
final g = await createDartWitGenerator(
wasiConfig: const WasiConfig(
preopenedDirs: [],
webBrowserFileSystem: {},
),
);
const inputs = InMemoryFiles(
worldFile: WitFile(
path: 'host.wit',
contents: hostWitContents,
),
pkgFiles: [],
);
_validateHostResult(g, inputs);
});
});
}
void _validateHostResult(DartWitGeneratorWorld g, WitGeneratorInput inputs) {
final result = g.generate(
config: defaultGeneratorConfig(inputs: inputs),
);
switch (result) {
case Err(:final error):
throw Exception(error);
case Ok(ok: final file):
final formatted = _formatter.format(file.contents);
expect(formatted, hostWitDartOutput);
expect(file.path, endsWith('host.wit'));
}
}