You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** optional: test result output format, default "table" */
146
-
// mode: ["html", "json", "table"],
147
-
};
148
-
149
-
```
150
-
151
-
## Using Matchers
152
-
153
-
The simplest way to test a value is with exact equality.
154
-
155
-
```typescript
156
-
test('two plus two is four', () => {
157
-
expect(2+2).equal(4);
158
-
});
159
-
```
160
-
161
-
In this code, `expect(2+2)` returns an "Value" object. You typically won't do much with these objects except call matchers on them. In this code, `.equal(4)` is the matcher. When Jest runs, it tracks all the failing matchers so that it can print out nice error messages for you.
162
-
163
-
### Equal
164
-
165
-
In the most condition, `equal` is similar as `==`, you can use this matcher to compare `i32 | i64 | u32 | u64 | f32 | f64 | string` just like `==`. What's more, it can also be used to compare some inner type, such as `Array | Map | Set`.
166
-
167
-
However, **Class** and **Interface** cannot be compared directly now.
168
-
169
-
`notEqual` is the opposite of `equal`
170
-
171
-
### Numbers
172
-
173
-
Most ways of comparing numbers have matcher equivalents, like `equal`, `greaterThan`, `greaterThanOrEqual`, `lessThan`, `lessThanOrEqual`.
174
-
175
-
Specially, for float type, use `closeTo` instead of `equal` to avoid rounding error.
176
-
177
-
## Nullable
178
-
179
-
`isNull` and `notNull` matcher can be used to a nullable object.
180
-
Of cource, you can also use `equal` and `notEqual` to do same thing with explicit generic declartion `expect<T | null>()`
181
-
182
-
## Using Mock Function
183
-
184
-
Because Assemblyscript's grammar is not as flexible as javascript, mock function have a lot of limitation and API design is not similar as jest (a javascript test framework).
185
-
186
-
However, There is a way to do some mock function.
187
-
188
-
Imagine that we are testing a function which includes a system-interface:
189
-
190
-
```typescript
191
-
// source.ts
192
-
declarefunction sys_getTime():i32;
193
-
exportfunction getTime():bool {
194
-
let errno =sys_getTime();
195
-
if (errno<0) {
196
-
// error handle
197
-
returnfalse;
198
-
}
199
-
// do something
200
-
returntrue;
201
-
}
202
-
```
203
-
204
-
To test error handle part, we need to inject some code to `sys_getTime` and expect to return a errno.
205
-
206
-
```typescript
207
-
// source.test.ts
208
-
test("getTime error handle", () => {
209
-
const fn =mock(sys_getTime, () => {
210
-
return-1;
211
-
});
212
-
expect(getTime()).equal(false); // success
213
-
expect(fn.calls).equal(1); // success
214
-
});
215
-
endTest();
216
-
```
217
-
218
-
mock API can temporary change the behavior of function, effective scope is each test.
219
-
In this mock function, you can do every thing include expecting arguments, mock return values and so on.
220
-
221
-
Tips:
222
-
223
-
- Because Assemblyscript is a strongly typed language, you should keep the function signature aligned.
224
-
- AssemblyScript does not support closures. If a mock function needs to be called several times in one test, and you want it to return different values or match arguments to different values, using a global counter for this function is a good way to achieve this.
225
-
226
-
### Example for MockFn
227
-
228
-
1. expect arguments
229
-
230
-
```typescript
231
-
test("check argument", () => {
232
-
const fn =mock(add, (a:i32, b:i32) => {
233
-
expect(a).equal(1);
234
-
returna+b;
235
-
});
236
-
expect(fn.calls).greaterThanOrEqual(1);
237
-
});
238
-
```
239
-
240
-
2. return difference value
241
-
242
-
```typescript
243
-
const ret = [1,2,3,4,5,6,7,8]; // global variant
244
-
const calls =0; // global variant
245
-
test("check argument", () => {
246
-
const fn =mock(add, (a:i32, b:i32) => {
247
-
returnret[calls++];
248
-
});
249
-
expect(fn.calls).greaterThanOrEqual(1);
250
-
});
251
-
```
252
-
253
-
3. re-call origin
254
-
255
-
```typescript
256
-
test("call origin", () => {
257
-
mock(add, (a:i32, b:i32) => {
258
-
unmock(add);
259
-
const res =add(a,b);
260
-
remock(add);
261
-
returnres;
262
-
});
263
-
});
264
-
```
265
-
266
-
## Coverage Report
267
-
268
-
After testing, you can get a html / json / table format test coverage report include "Statements Coverage", "Branches Coverage", "Functions Coverage", "Lines Coverage"
269
-
270
24
## Architecture
271
25
272
26
-`assembly/` written in Assemblyscript, provides user-accessible testing APIs such as test, inspect, mock, etc.
0 commit comments