Skip to content

Commit

Permalink
feat: add annotation @trans
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jun 6, 2021
1 parent 6beb21c commit aaf1e5b
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 9 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Read this in other languages: [中文](./README.zh-CN.md)
- [@group](#group)
- [@eval](#eval)
- [@mock](#mock)
- [@trans](#trans)
- [@every](#every)
- [@some](#some)
- [@partial](#partial)
Expand Down Expand Up @@ -599,6 +600,37 @@ The test cases in the group will inherit the group's `@client` and `@mixin`. The

Apitest supports nearly 40 mock functions. For a detailed list, see [sigodne/fake.js](https://github.com/sigoden/fake-js#doc)

### @trans

**Transform data**

```
{
test1: { @client("echo")
req: {
v1: { @trans(`JSON.stringify($)`)
v1: 1,
v2: 2,
}
},
res: {
v1: `{"v1":1,"v2":2}`,
}
},
test2: { @client("echo")
req: {
v1: `{"v1":1,"v2":2}`,
},
res: {
v2: { @trans(`JSON.parse($)`)
v1: 1,
v2: 2,
}
}
}
}
```

### @every

**A set of assertions are passed before the test passes**
Expand Down
32 changes: 32 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Apitest 是一款使用类JSON的DSL编写测试用例的自动化测试工具
- [@group](#group)
- [@eval](#eval)
- [@mock](#mock)
- [@trans](#trans)
- [@every](#every)
- [@some](#some)
- [@partial](#partial)
Expand Down Expand Up @@ -592,6 +593,37 @@ main

Apitest 支持近40个mock函数。详细清单见[sigodne/fake.js](https://github.com/sigoden/fake-js#doc)

### @trans

功能: 变换数据

```
{
test1: { @client("echo")
req: {
v1: { @trans(`JSON.stringify($)`)
v1: 1,
v2: 2,
}
},
res: {
v1: `{"v1":1,"v2":2}`,
}
},
test2: { @client("echo")
req: {
v1: `{"v1":1,"v2":2}`,
},
res: {
v2: { @trans(`JSON.parse($)`)
v1: 1,
v2: 2,
}
}
}
}
```

### @every

功能: 一组断言全部通过才测试通过
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sigodenjs/apitest",
"description": "declartive api testing tool",
"version": "0.5.1",
"version": "0.6.0",
"bin": {
"apitest": "dist/bin.js"
},
Expand Down
6 changes: 6 additions & 0 deletions src/compareRes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export default function compareRes(unit: Unit, ctx: VmContext, res: any) {
}

function compareValue(paths: string[], ctx: VmContext, v1: JsonaValue, v2: any) {
if (existAnno(paths, v1, "trans", "any")) {
const transAnno = v1.annotations.find(v => v.name === "trans");
_.set(ctx.state, "$", v2);
v2 = evalValue(paths, ctx, transAnno.value, "trans");
_.set(ctx.state, "$", null);
}
if (existAnno(paths, v1, "eval", "string")) {
ctx.state.$ = v2;
const value = evalValue(paths, ctx, (v1 as JsonaString).value);
Expand Down
26 changes: 18 additions & 8 deletions src/createReq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ export default function createReq(unit: Unit, ctx: VmContext): any {
}

function createValue(paths: string[], ctx: VmContext, jsa: JsonaValue) {
let result: any;
if (existAnno(paths, jsa, "eval", "string")) {
const value = evalValue(paths, ctx, (jsa as JsonaString).value);
_.set(ctx.state, paths, value);
return value;
result = value;
} else if(existAnno(paths, jsa, "mock", "string")) {
const value = (jsa as JsonaString).value;
try {
const mockValue = fake(value);
_.set(ctx.state, paths, mockValue);
return mockValue;
result = mockValue;
} catch(err) {
throw new RunCaseError(paths, "mock", `bad mock '${value}'`);
}
Expand All @@ -33,23 +34,32 @@ function createValue(paths: string[], ctx: VmContext, jsa: JsonaValue) {
for (const [i, ele] of jsa.elements.entries()) {
output.push(createValue(paths.concat([String(i)]), ctx, ele));
}
return output;
result = output;
} else if (jsa.type === "Object") {
_.set(ctx.state, paths, _.get(ctx.state, paths, {}));
const output = _.get(ctx.state, paths);
for (const prop of jsa.properties) {
output[prop.key] = createValue(paths.concat([prop.key]), ctx, prop.value);
}
return output;
result = output;
} else if (jsa.type === "Null") {
return null;
result = null;
} else {
return jsa.value;
result = jsa.value;
}
}
if (existAnno(paths, jsa, "trans", "any")) {
const transAnno = jsa.annotations.find(v => v.name === "trans");
_.set(ctx.state, "$", result);
const value = evalValue(paths, ctx, transAnno.value, "trans");
_.set(ctx.state, "$", null);
_.set(ctx.state, paths, value);
result = value;
}
return result;
}

export function evalValue(paths: string[], ctx: VmContext, code: string): any {
export function evalValue(paths: string[], ctx: VmContext, code: string, anno = "eval"): any {
if (!code) return null;
const expressions = _.trimEnd(code.trim(), ";").split(";").map(v => v.trim());
const lastIdx = expressions.length - 1;
Expand All @@ -65,7 +75,7 @@ export function evalValue(paths: string[], ctx: VmContext, code: string): any {
script.runInNewContext(ctx.state);
return ctx.state[EXPORT_KEY];
} catch (err) {
throw new RunCaseError(paths, "eval", `throw err, ${err.message}`);
throw new RunCaseError(paths, anno, `throw err, ${err.message}`);
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/req/trans.jsona
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
test1: { @client("echo")
req: {
v1: { @trans(`JSON.stringify($)`)
v1: 1,
v2: 2,
}
},
res: {
v1: `{"v1":1,"v2":2}`,
}
},
}
13 changes: 13 additions & 0 deletions tests/fixtures/res/trans.jsona
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
test1: { @client("echo")
req: {
v1: `{"v1":1,"v2":2}`,
},
res: {
v1: { @trans(`JSON.parse($)`)
v1: 1,
v2: 2,
}
}
}
}
4 changes: 4 additions & 0 deletions tests/req.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ describe("req", () => {
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
test("trans", async () => {
const { code } = await spwanTest("req/trans.jsona", ["--ci"]);
expect(code).toEqual(0);
});
});
4 changes: 4 additions & 0 deletions tests/res.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe("res", () => {
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
test("trans", async () => {
const { code } = await spwanTest("res/trans.jsona", ["--ci"]);
expect(code).toEqual(0);
});
test("type", async () => {
const { stdout, code } = await spwanTest("res/type.jsona", ["--ci"]);
expect(code).toEqual(1);
Expand Down

0 comments on commit aaf1e5b

Please sign in to comment.