Skip to content

Commit

Permalink
feat: add @nullable (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jul 6, 2022
1 parent bd1d7bd commit 4718a7a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
20 changes: 20 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)
- [@partial](#partial)
- [@type](#type)
- [@optional](#optional)
- [@nullable](#nullable)
- [Run](#run)
- [Skip](#skip)
- [Delay](#delay)
Expand Down Expand Up @@ -808,6 +809,25 @@ Apitest supports nearly 40 mock functions. For a detailed list, see [fake-js](ht
}
```

### @nullable

**Field maybe null**
> scope: unit res block
```
{
test1: { @client("echo")
req: {
v1: null,
// v1: 3,
},
res: {
v1: 3, @nullable
}
}
}
```


## Run

Expand Down
3 changes: 3 additions & 0 deletions src/compareRes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ async function compareValue(paths: string[], ctx: VmContext, v1: JsonaValue, v2:
v2 = evalValue(paths, ctx, transAnno.value, "trans");
_.set(ctx.state, "$", null);
}

if (existAnno(paths, v1, "nullable", "any") && v2 == null) return;

if (existAnno(paths, v1, "eval", "string")) {
ctx.state.$ = v2;
const value = evalValue(paths, ctx, (v1 as JsonaString).value);
Expand Down
6 changes: 6 additions & 0 deletions tests/__snapshots__/res.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ exports[`res every 1`] = `
"
`;

exports[`res nullable 1`] = `
"main
@nullable ✔
"
`;

exports[`res optional 1`] = `
"not found main jsona file
"
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/res/nullable.jsona
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
@client({name:"default",kind:"echo"})
test1: { @describe("@nullable")
req: {
v1: null,
},
res: {
v1: 3, @nullable
}
},
}
5 changes: 5 additions & 0 deletions tests/res.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ describe("res", () => {
expect(code).toEqual(1);
expect(stdout).toMatchSnapshot();
});
test("nullable", async () => {
const { stdout, code } = await spwanTest("res/nullable.jsona", ["--ci"]);
expect(code).toEqual(0);
expect(stdout).toMatchSnapshot();
});
});

0 comments on commit 4718a7a

Please sign in to comment.