Skip to content

Commit ec96308

Browse files
authored
chore: move doc to github.io with vitepress (#45)
1 parent 978b7c9 commit ec96308

File tree

14 files changed

+2751
-323
lines changed

14 files changed

+2751
-323
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
@@ -11,3 +11,6 @@
1111
/coverage-ts
1212

1313
/tests/cpp/lit/build
14+
15+
/docs/.vitepress/dist
16+
/docs/.vitepress/cache

README.md

Lines changed: 0 additions & 246 deletions
Original file line numberDiff line numberDiff line change
@@ -21,252 +21,6 @@ A comprehensive AssemblyScript testing solution, offering developers a robust su
2121
- Coverage statistics
2222
- Expectations
2323

24-
## Getting Started
25-
26-
Install Assemblyscript Unittest Framework using npm
27-
28-
```bash
29-
npm install --save-dev assemblyscript-unittest-framework
30-
```
31-
32-
Let's get started by writing a test for a simple function that add two numbers. Assume that there is already environment of assemblyscript.
33-
34-
First, create `source/sum.ts`:
35-
36-
```Typescript
37-
export function add(a: i32, b: i32): i32 {
38-
return a + b;
39-
}
40-
```
41-
42-
Then, create a file named `tests/sum.test.ts`. This will contain our actual test:
43-
44-
```Typescript
45-
import { test, expect, endTest } from "assemblyscript-unittest-framework/assembly";
46-
import { add } from "../source/sum";
47-
48-
test("sum", () => {
49-
expect(add(1, 2)).equal(3);
50-
expect(add(1, 1)).equal(3);
51-
});
52-
endTest(); // Don't forget it!
53-
```
54-
55-
**Don't forget `endTest()` at the end of `*.test.ts` files**
56-
57-
Create a config file in project root `as-test.config.js`:
58-
59-
for cjs:
60-
61-
```javascript
62-
module.exports = {
63-
include: ["source", "tests"],
64-
};
65-
```
66-
67-
for mjs:
68-
69-
```javascript
70-
export default {
71-
include: ["source", "tests"],
72-
};
73-
```
74-
75-
Add the following section to your `package.json`
76-
77-
```json
78-
{
79-
"scripts": {
80-
"test": "as-test"
81-
}
82-
}
83-
```
84-
85-
Finally, run `npm run test` and as-test will print this message:
86-
87-
```
88-
transform source/sum.ts => build/source/sum.ts.cov
89-
transform build/source/sum.ts.cov => build/source/sum.ts
90-
transform tests/sum.test.ts => build/tests/sum.test.ts
91-
(node:489815) ExperimentalWarning: WASI is an experimental feature. This feature could change at any time
92-
93-
test case: 1/2 (success/total)
94-
95-
Error Message:
96-
sum:
97-
tests/sum.test.ts:6:3 (6:3, 6:29)
98-
```
99-
100-
You can also use `npx as-test -h` for more information to control detail configurations
101-
102-
## Configuration
103-
104-
This is the template of `as-test.config.js`:
105-
106-
```javascript
107-
module.exports = {
108-
// test related code folder
109-
include: ["source", "tests"],
110-
exclude: [],
111-
112-
/** optional: assemblyscript compile flag, default is --exportStart _start -O0 */
113-
flags: "",
114-
115-
/**
116-
* optional: import functions
117-
* @param {ImportsArgument} runtime
118-
* @returns
119-
*/
120-
imports(runtime) {
121-
return {
122-
env: {
123-
logInfo(ptr, len) {
124-
if (runtime.exports) {
125-
let arrbuf = runtime.exports.__getArrayBuffer(ptr);
126-
let str = Buffer.from(arrbuf).toString("utf8");
127-
console.log(str);
128-
}
129-
},
130-
},
131-
builtin: {
132-
getU8FromLinkedMemory(a) {
133-
return 1;
134-
},
135-
},
136-
};
137-
},
138-
139-
/** optional: template file path, default "coverage" */
140-
// temp: "coverage",
141-
142-
/** optional: report file path, default "coverage" */
143-
// output: "coverage",
144-
145-
/** 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-
declare function sys_getTime(): i32;
193-
export function getTime(): bool {
194-
let errno = sys_getTime();
195-
if (errno < 0) {
196-
// error handle
197-
return false;
198-
}
199-
// do something
200-
return true;
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-
return a + 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-
return ret[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-
return res;
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-
27024
## Architecture
27125

27226
- `assembly/` written in Assemblyscript, provides user-accessible testing APIs such as test, inspect, mock, etc.

docs/.vitepress/config.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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/index",
22+
items: [
23+
{ text: "Configuration", link: "/api-documents/configuration" },
24+
{ text: "Matchers", link: "/api-documents/matchers" },
25+
{ text: "Mock Function", link: "/api-documents/mock-function" },
26+
{ text: "Report", link: "/api-documents/coverage-report" },
27+
],
28+
},
29+
{
30+
text: "Technical Details",
31+
items: [
32+
{
33+
text: "code debug info between C++ and TS",
34+
link: "/technical-details/code-debug-info",
35+
},
36+
],
37+
},
38+
],
39+
},
40+
],
41+
socialLinks: [{ icon: "github", link: "https://github.com/wasm-ecosystem/assemblyscript-unittest-framework" }],
42+
},
43+
});

docs/api-documents/configuration.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Configuration
2+
3+
This is the template of `as-test.config.js`:
4+
5+
```javascript
6+
module.exports = {
7+
// test related code folder
8+
include: ["source", "tests"],
9+
exclude: [],
10+
11+
/** optional: assemblyscript compile flag, default is --exportStart _start -O0 */
12+
flags: "",
13+
14+
/**
15+
* optional: import functions
16+
* @param {ImportsArgument} runtime
17+
* @returns
18+
*/
19+
imports(runtime) {
20+
return {
21+
env: {
22+
logInfo(ptr, len) {
23+
if (runtime.exports) {
24+
let arrbuf = runtime.exports.__getArrayBuffer(ptr);
25+
let str = Buffer.from(arrbuf).toString("utf8");
26+
console.log(str);
27+
}
28+
},
29+
},
30+
builtin: {
31+
getU8FromLinkedMemory(a) {
32+
return 1;
33+
},
34+
},
35+
};
36+
},
37+
38+
/** optional: template file path, default "coverage" */
39+
// temp: "coverage",
40+
41+
/** optional: report file path, default "coverage" */
42+
// output: "coverage",
43+
44+
/** optional: test result output format, default "table" */
45+
// mode: ["html", "json", "table"],
46+
};
47+
48+
```

0 commit comments

Comments
 (0)