diff --git a/packages/toolkit/src/apiGenerate/__test__/parse.ts b/packages/toolkit/src/apiGenerate/__test__/parse.ts index a1a8314..6b12400 100644 --- a/packages/toolkit/src/apiGenerate/__test__/parse.ts +++ b/packages/toolkit/src/apiGenerate/__test__/parse.ts @@ -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"; @@ -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); + } }); }); diff --git a/packages/toolkit/src/apiGenerate/__test__/test.md b/packages/toolkit/src/apiGenerate/__test__/test.md index 9765aa4..6594641 100644 --- a/packages/toolkit/src/apiGenerate/__test__/test.md +++ b/packages/toolkit/src/apiGenerate/__test__/test.md @@ -84,7 +84,7 @@ POST } ``` -# acount +# account ## 用户