Skip to content

Commit 814e593

Browse files
committed
merge main
2 parents b8d8777 + 3675aa6 commit 814e593

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3319
-565
lines changed

.github/workflows/gh-pages.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Simple workflow for deploying static content to GitHub Pages
2+
name: Deploy static content to Pages
3+
4+
on:
5+
# Runs on pushes targeting the default branch
6+
push:
7+
branches: ["main"]
8+
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
11+
12+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13+
permissions:
14+
contents: read
15+
pages: write
16+
id-token: write
17+
18+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
19+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
20+
concurrency:
21+
group: "pages"
22+
cancel-in-progress: false
23+
24+
jobs:
25+
# Single deploy job since we're just deploying
26+
deploy:
27+
environment:
28+
name: github-pages
29+
url: ${{ steps.deployment.outputs.page_url }}
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v4
33+
34+
- uses: actions/setup-node@v4
35+
with:
36+
node-version: 22
37+
cache: npm
38+
- run: npm ci
39+
- run: npm run docs:build
40+
41+
- uses: actions/configure-pages@v5
42+
- uses: actions/upload-pages-artifact@v3
43+
with:
44+
path: docs/.vitepress/dist
45+
- id: deployment
46+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
/coverage-ts
1313

1414
/tests/cpp/lit/build
15+
16+
/docs/.vitepress/dist
17+
/docs/.vitepress/cache

README.md

Lines changed: 16 additions & 245 deletions
Original file line numberDiff line numberDiff line change
@@ -1,256 +1,27 @@
11
# Assemblyscript Unittest Framework
22

3+
<div align="center">
4+
<a href="https://deepwiki.com/wasm-ecosystem/assemblyscript-unittest-framework">
5+
<img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki" />
6+
</a>
7+
<a href="https://www.npmjs.com/package/assemblyscript-unittest-framework">
8+
<img src="https://img.shields.io/npm/v/assemblyscript-unittest-framework.svg?color=007acc&logo=npm" alt="npm" />
9+
</a>
10+
<a href="https://discord.gg/assemblyscript">
11+
<img
12+
src="https://img.shields.io/discord/721472913886281818.svg?label=discord&logo=discord&logoColor=ffffff&color=7389D8"
13+
alt="Discord online"
14+
/>
15+
</a>
16+
</div>
17+
318
A comprehensive AssemblyScript testing solution, offering developers a robust suite of features to ensure their code performs as expected:
419

520
- Function Mocking
621
- Coverage statistics
722
- Expectations
823

9-
## Getting Started
10-
11-
Install Assemblyscript Unittest Framework using npm
12-
13-
```bash
14-
npm install --save-dev assemblyscript-unittest-framework
15-
```
16-
17-
Let's get started by writing a test for a simple function that add two numbers. Assume that there is already environment of assemblyscript.
18-
19-
First, create `source/sum.ts`:
20-
21-
```Typescript
22-
export function add(a: i32, b: i32): i32 {
23-
return a + b;
24-
}
25-
```
26-
27-
Then, create a file named `tests/sum.test.ts`. This will contain our actual test:
28-
29-
```Typescript
30-
import { test, expect, endTest } from "assemblyscript-unittest-framework/assembly";
31-
import { add } from "../source/sum";
32-
33-
test("sum", () => {
34-
expect(add(1, 2)).equal(3);
35-
expect(add(1, 1)).equal(3);
36-
});
37-
endTest(); // Don't forget it!
38-
```
39-
40-
**Don't forget `endTest()` at the end of `*.test.ts` files**
41-
42-
Create a config file in project root `as-test.config.js`:
43-
44-
for cjs:
45-
46-
```javascript
47-
module.exports = {
48-
include: ["source", "tests"],
49-
};
50-
```
51-
52-
for mjs:
53-
54-
```javascript
55-
export default {
56-
include: ["source", "tests"],
57-
};
58-
```
59-
60-
Add the following section to your `package.json`
61-
62-
```json
63-
{
64-
"scripts": {
65-
"test": "as-test"
66-
}
67-
}
68-
```
69-
70-
Finally, run `npm run test` and as-test will print this message:
71-
72-
```
73-
transform source/sum.ts => build/source/sum.ts.cov
74-
transform build/source/sum.ts.cov => build/source/sum.ts
75-
transform tests/sum.test.ts => build/tests/sum.test.ts
76-
(node:489815) ExperimentalWarning: WASI is an experimental feature. This feature could change at any time
77-
78-
test case: 1/2 (success/total)
79-
80-
Error Message:
81-
sum:
82-
tests/sum.test.ts:6:3 (6:3, 6:29)
83-
```
84-
85-
You can also use `npx as-test -h` for more information to control detail configurations
86-
87-
## Configuration
88-
89-
This is the template of `as-test.config.js`:
90-
91-
```javascript
92-
module.exports = {
93-
// test related code folder
94-
include: ["source", "tests"],
95-
exclude: [],
96-
97-
/** optional: assemblyscript compile flag, default is --exportStart _start -O0 */
98-
flags: "",
99-
100-
/**
101-
* optional: import functions
102-
* @param {ImportsArgument} runtime
103-
* @returns
104-
*/
105-
imports(runtime) {
106-
return {
107-
env: {
108-
logInfo(ptr, len) {
109-
if (runtime.exports) {
110-
let arrbuf = runtime.exports.__getArrayBuffer(ptr);
111-
let str = Buffer.from(arrbuf).toString("utf8");
112-
console.log(str);
113-
}
114-
},
115-
},
116-
builtin: {
117-
getU8FromLinkedMemory(a) {
118-
return 1;
119-
},
120-
},
121-
};
122-
},
123-
124-
/** optional: template file path, default "coverage" */
125-
// temp: "coverage",
126-
127-
/** optional: report file path, default "coverage" */
128-
// output: "coverage",
129-
130-
/** optional: test result output format, default "table" */
131-
// mode: ["html", "json", "table"],
132-
};
133-
134-
```
135-
136-
## Using Matchers
137-
138-
The simplest way to test a value is with exact equality.
139-
140-
```typescript
141-
test('two plus two is four', () => {
142-
expect(2 + 2).equal(4);
143-
});
144-
```
145-
146-
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.
147-
148-
### Equal
149-
150-
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`.
151-
152-
However, **Class** and **Interface** cannot be compared directly now.
153-
154-
`notEqual` is the opposite of `equal`
155-
156-
### Numbers
157-
158-
Most ways of comparing numbers have matcher equivalents, like `equal`, `greaterThan`, `greaterThanOrEqual`, `lessThan`, `lessThanOrEqual`.
159-
160-
Specially, for float type, use `closeTo` instead of `equal` to avoid rounding error.
161-
162-
## Nullable
163-
164-
`isNull` and `notNull` matcher can be used to a nullable object.
165-
Of cource, you can also use `equal` and `notEqual` to do same thing with explicit generic declartion `expect<T | null>()`
166-
167-
## Using Mock Function
168-
169-
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).
170-
171-
However, There is a way to do some mock function.
172-
173-
Imagine that we are testing a function which includes a system-interface:
174-
175-
```typescript
176-
// source.ts
177-
declare function sys_getTime(): i32;
178-
export function getTime(): bool {
179-
let errno = sys_getTime();
180-
if (errno < 0) {
181-
// error handle
182-
return false;
183-
}
184-
// do something
185-
return true;
186-
}
187-
```
188-
189-
To test error handle part, we need to inject some code to `sys_getTime` and expect to return a errno.
190-
191-
```typescript
192-
// source.test.ts
193-
test("getTime error handle", () => {
194-
const fn = mock(sys_getTime, () => {
195-
return -1;
196-
});
197-
expect(getTime()).equal(false); // success
198-
expect(fn.calls).equal(1); // success
199-
});
200-
endTest();
201-
```
202-
203-
mock API can temporary change the behavior of function, effective scope is each test.
204-
In this mock function, you can do every thing include expecting arguments, mock return values and so on.
205-
206-
Tips:
207-
208-
- Because Assemblyscript is a strongly typed language, you should keep the function signature aligned.
209-
- 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.
210-
211-
### Example for MockFn
212-
213-
1. expect arguments
214-
215-
```typescript
216-
test("check argument", () => {
217-
const fn = mock(add, (a: i32, b: i32) => {
218-
expect(a).equal(1);
219-
return a + b;
220-
});
221-
expect(fn.calls).greaterThanOrEqual(1);
222-
});
223-
```
224-
225-
2. return difference value
226-
227-
```typescript
228-
const ret = [1,2,3,4,5,6,7,8]; // global variant
229-
const calls = 0; // global variant
230-
test("check argument", () => {
231-
const fn = mock(add, (a: i32, b: i32) => {
232-
return ret[calls++];
233-
});
234-
expect(fn.calls).greaterThanOrEqual(1);
235-
});
236-
```
237-
238-
3. re-call origin
239-
240-
```typescript
241-
test("call origin", () => {
242-
mock(add, (a: i32, b: i32) => {
243-
unmock(add);
244-
const res = add(a,b);
245-
remock(add);
246-
return res;
247-
});
248-
});
249-
```
250-
251-
## Coverage Report
252-
253-
After testing, you can get a html / json / table format test coverage report include "Statements Coverage", "Branches Coverage", "Functions Coverage", "Lines Coverage"
24+
Documentation: https://wasm-ecosystem.github.io/assemblyscript-unittest-framework/
25425

25526
## Architecture
25627

assembly/env.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export namespace assertResult {
1212
export declare function registerTestFunction(index: u32): void;
1313

1414

15+
@external("__unittest_framework_env","finishTestFunction")
16+
export declare function finishTestFunction(): void;
17+
18+
1519
@external("__unittest_framework_env","collectCheckResult")
1620
export declare function collectCheckResult(
1721
result: bool,

assembly/implement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export function describeImpl(
1212
export function testImpl(description: string, testFunction: () => void): void {
1313
assertResult.addDescription(description);
1414
assertResult.registerTestFunction(testFunction.index);
15+
assertResult.finishTestFunction();
1516
assertResult.removeDescription();
16-
mockFunctionStatus.clear();
1717
}
1818

1919
export function mockImpl<T extends Function>(

docs/.vitepress/config.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { defineConfig } from "vitepress";
2+
3+
// https://vitepress.dev/reference/site-config
4+
export default defineConfig({
5+
title: "assemblyscript unittest framework",
6+
description: "documents of assemblyscript unittest framework",
7+
base: "/assemblyscript-unittest-framework/",
8+
themeConfig: {
9+
// https://vitepress.dev/reference/default-theme-config
10+
search: {
11+
provider: "local",
12+
},
13+
nav: [{ text: "Home", link: "/" }],
14+
sidebar: [
15+
{
16+
text: "Document",
17+
items: [
18+
{ text: "Quick Start", link: "/quick-start.md" },
19+
{
20+
text: "API documents",
21+
link: "/api-documents",
22+
items: [
23+
{ text: "Configuration", link: "/api-documents/configuration" },
24+
{ text: "Options", link: "/api-documents/options" },
25+
{ text: "Matchers", link: "/api-documents/matchers" },
26+
{ text: "Mock Function", link: "/api-documents/mock-function" },
27+
{ text: "Report", link: "/api-documents/coverage-report" },
28+
],
29+
},
30+
{
31+
text: "Examples",
32+
link: "/examples",
33+
items: [{ text: "Mock Method", link: "/examples/mock-method" }],
34+
},
35+
{
36+
text: "Technical Details",
37+
items: [
38+
{
39+
text: "code debug info between C++ and TS",
40+
link: "/technical-details/code-debug-info",
41+
},
42+
],
43+
},
44+
],
45+
},
46+
],
47+
socialLinks: [{ icon: "github", link: "https://github.com/wasm-ecosystem/assemblyscript-unittest-framework" }],
48+
},
49+
});

0 commit comments

Comments
 (0)