Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 46 additions & 72 deletions packages/toolkit/src/apiGenerate/__test__/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, test } from "vitest";

import exp from "node:constants";
import fs from "node:fs";
import path from "node:path";
import { ApiParser } from "../paser";
Expand All @@ -18,77 +19,50 @@ test("parse", () => {
console.error("读取文件失败:", err);
return;
}
expect(showMDASR(data)).toStrictEqual({
login: {
用户注册: {
method: "POST",
url: "/user/register",
req:
"{\r\n" +
'"username": "12154545",\r\n' +
'"name": "吴系挂",\r\n' +
'"password": "1436864169"\r\n' +
"}",
res:
"{\r\n" +
'"error_code": 0,\r\n' +
'"data": {\r\n' +
'"uid": "1",\r\n' +
'"username": "12154545",\r\n' +
'"name": "吴系挂",\r\n' +
'"groupid": 2 ,\r\n' +
'"reg_time": "1436864169",\r\n' +
'"last_login_time": "0",\r\n' +
"}\r\n" +
"}",
},
退出: {
method: "POST",
url: "/user/register",
req:
"{\r\n" +
'"username": "12154545",\r\n' +
'"name": "吴系挂",\r\n' +
'"password": "1436864169"\r\n' +
"}",
res:
"{\r\n" +
'"error_code": 0,\r\n' +
'"data": {\r\n' +
'"uid": "1",\r\n' +
'"username": "12154545",\r\n' +
'"name": "吴系挂",\r\n' +
'"groupid": 2 ,\r\n' +
'"reg_time": "1436864169",\r\n' +
'"last_login_time": "0",\r\n' +
"}\r\n" +
"}",
},
},
acount: {
用户: {
method: "POST",
url: "/user/register",
req:
"{\r\n" +
'"username": "12154545",\r\n' +
'"name": "吴系挂",\r\n' +
'"password": "1436864169"\r\n' +
"}",
res:
"{\r\n" +
'"error_code": 0,\r\n' +
'"data": {\r\n' +
'"uid": "1",\r\n' +
'"username": "12154545",\r\n' +
'"name": "吴系挂",\r\n' +
'"groupid": 2 ,\r\n' +
'"reg_time": "1436864169",\r\n' +
'"last_login_time": "0",\r\n' +
"}\r\n" +
"}",
},
},
});

const target = showMDASR(data);

function stringToRegex(str: string) {
// 用 \s+ 替换掉所有空格
const escapedStr = str.replace(/\s+/g, "\\s+");
// 构建正则对象,使用 new RegExp
return new RegExp(escapedStr);
}
const testCases = [
{ path: "login" },
{ path: "account" },
{ path: "login.用户注册" },
{ path: "login.退出" },
{ path: "account.用户" },
{ path: "login.用户注册.method", value: "POST" },
{ path: "login.用户注册.url", value: "/user/register" },
{ path: "login.退出.method", value: "POST" },
{ path: "login.退出.url", value: "/user/register" },
{ path: "account.用户.method", value: "POST" },
{ path: "account.用户.url", value: "/user/register" },
];

const checkProperty = (path: string, value?: string) => {
if (value) {
expect(target).toHaveProperty(path, value);
} else {
expect(target).toHaveProperty(path);
}
};

const checkJsonMatch = (path: string) => {
const reqPath = target[path.split(".")[0]][path.split(".")[1]].req;
expect(reqPath).toMatch(stringToRegex(reqPath));
const resPath = target[path.split(".")[0]][path.split(".")[1]].res;
expect(resPath).toMatch(stringToRegex(resPath));
};

for (const { path, value } of testCases) {
checkProperty(path, value);
}

for (const item of ["login.用户注册", "login.退出", "account.用户"]) {
checkJsonMatch(item);
}
});
});
2 changes: 1 addition & 1 deletion packages/toolkit/src/apiGenerate/__test__/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ POST
}
```

# acount
# account

## 用户

Expand Down