Skip to content

Commit

Permalink
feat: add @optional, close #6
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jun 18, 2021
1 parent 87f087c commit f3ccb92
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Read this in other languages: [中文](./README.zh-CN.md)
- [@some](#some)
- [@partial](#partial)
- [@type](#type)
- [@optional](#optional)
- [Run](#run)
- [Skip](#skip)
- [Delay](#delay)
Expand Down Expand Up @@ -785,6 +786,27 @@ Apitest supports nearly 40 mock functions. For a detailed list, see [fake-js](ht
}
```

### @optional

**Marker field is optional**
> scope: unit res block
```
{
test1: { @client("echo")
req: {
v1: 3,
// v2: 4, optional field
},
res: {
v1: 3,
v2: 4, @optional
}
}
}
```


## Run

In some scenarios, use cases may not need to be executed, or they may need to be executed repeatedly. It is necessary to add a `run` option to support this feature.
Expand Down
21 changes: 21 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Apitest 是一款使用类JSON的DSL编写测试用例的自动化测试工具
- [@some](#some)
- [@partial](#partial)
- [@type](#type)
- [@optional](#optional)
- [执行控制](#执行控制)
- [跳过](#跳过)
- [延时](#延时)
Expand Down Expand Up @@ -780,6 +781,26 @@ Apitest 支持近40个mock函数。详细清单见[fake-js](https://github.com/s
}
```

### @optional

- 功能: 标记字段可选
- 使用范围: 用例`res`数据块

```
{
test1: { @client("echo")
req: {
v1: 3,
// v2: 4, 可选字段
},
res: {
v1: 3,
v2: 4, @optional
}
}
}
```

## 执行控制

Apitest 允许测试用例或组通过 `run` 属性自定义执行逻辑。
Expand Down
5 changes: 4 additions & 1 deletion src/compareRes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ async function compareValue(paths: string[], ctx: VmContext, v1: JsonaValue, v2:
throw new RunCaseError(paths, "", `${v1Value}${v2}`);
}
if (v1.type === "Object") {
const optionalFields = v1.properties.filter(v => !!v.value.annotations.find(v => v.name === "optional")).map(v => v.key);
if (!existAnno(paths, v1, "partial", "object")) {
const v1Keys = v1.properties.map(v => v.key);
let v1Keys = v1.properties.map(v => v.key);
v1Keys = _.difference(v1Keys, optionalFields);
const v2Keys = Object.keys(v2);
if (v1Keys.length !== v2Keys.length) {
const v1x = _.difference(v1Keys, v2Keys);
Expand All @@ -88,6 +90,7 @@ async function compareValue(paths: string[], ctx: VmContext, v1: JsonaValue, v2:
}
}
for (const prop of v1.properties) {
if (optionalFields.indexOf(prop.key) > -1 && typeof v2[prop.key] === "undefined") continue;
await compareValue(paths.concat([prop.key]), ctx, prop.value, v2[prop.key]);
}
} else if (v1.type === "Array") {
Expand Down
5 changes: 5 additions & 0 deletions tests/__snapshots__/res.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ exports[`res every 1`] = `
"
`;

exports[`res optional 1`] = `
"not found main jsona file
"
`;

exports[`res partial 1`] = `
"main
@partial for object ✔
Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/res/optiona.jsona
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
@client({name:"default",kind:"echo"})
test1: { @describe("@optional")
req: {
v1: 3,
// v2: 4, optional field
},
res: {
v1: 3,
v2: 4, @optional
}
},
test2: { @describe("@optional with other anno")
req: {
v1: 3,
// v2: 4, optional filed
},
res: {
v1: 3,
v2: 0, @type @optional
}
}
}
5 changes: 5 additions & 0 deletions tests/res.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ describe("res", () => {
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
test("optional", async () => {
const { stdout, code } = await spwanTest("res/optional.jsona", ["--ci"]);
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
});

0 comments on commit f3ccb92

Please sign in to comment.