Skip to content

Commit 29a9d8d

Browse files
authored
✅ Add tests for functions.ts (#38)
1 parent 15d57b1 commit 29a9d8d

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed

Diff for: src/functions.test.ts

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import { describe, test } from "vitest"
2+
import type { FunctionDef, ObjectProp, Prop } from "./functions.js"
3+
import {
4+
formatFunctionDefinitions,
5+
formatObjectProperties,
6+
formatType,
7+
isAnyOfProp,
8+
} from "./functions.js"
9+
10+
describe("isAnyOfProp", () => {
11+
test("true", ({ expect }) => {
12+
const result = isAnyOfProp({ anyOf: [] })
13+
expect(result).toBe(true)
14+
})
15+
16+
test("false", ({ expect }) => {
17+
const result = isAnyOfProp({ type: "null" })
18+
expect(result).toBe(false)
19+
})
20+
})
21+
22+
describe("formatFunctionDefinitions", () => {
23+
test("empty array", ({ expect }) => {
24+
const result = formatFunctionDefinitions([])
25+
expect(result).toBe(`namespace functions {
26+
27+
} // namespace functions`)
28+
})
29+
30+
test("single function with no parameters", ({ expect }) => {
31+
const functions: FunctionDef[] = [
32+
{
33+
name: "testFunction",
34+
description: "A test function",
35+
parameters: { type: "object" },
36+
},
37+
]
38+
39+
const result = formatFunctionDefinitions(functions)
40+
expect(result).toBe(`namespace functions {
41+
42+
// A test function
43+
type testFunction = () => any;
44+
45+
} // namespace functions`)
46+
})
47+
48+
test("single function with parameters", ({ expect }) => {
49+
const functions: FunctionDef[] = [
50+
{
51+
name: "testFunction",
52+
description: "A test function",
53+
parameters: {
54+
type: "object",
55+
properties: {
56+
param1: { type: "string" },
57+
param2: { type: "number" },
58+
},
59+
required: ["param1"],
60+
},
61+
},
62+
]
63+
64+
const result = formatFunctionDefinitions(functions)
65+
expect(result).toBe(`namespace functions {
66+
67+
// A test function
68+
type testFunction = (_: {
69+
param1: string,
70+
param2?: number,
71+
}) => any;
72+
73+
} // namespace functions`)
74+
})
75+
76+
test("multiple functions", ({ expect }) => {
77+
const functions: FunctionDef[] = [
78+
{
79+
name: "functionOne",
80+
description: "First function",
81+
parameters: { type: "object" },
82+
},
83+
{
84+
name: "functionTwo",
85+
description: "Second function",
86+
parameters: {
87+
type: "object",
88+
properties: { param1: { type: "boolean" } },
89+
},
90+
},
91+
]
92+
93+
const result = formatFunctionDefinitions(functions)
94+
expect(result).toBe(`namespace functions {
95+
96+
// First function
97+
type functionOne = () => any;
98+
99+
// Second function
100+
type functionTwo = (_: {
101+
param1?: boolean,
102+
}) => any;
103+
104+
} // namespace functions`)
105+
})
106+
})
107+
108+
describe("formatObjectProperties", () => {
109+
test("empty object", ({ expect }) => {
110+
const obj: ObjectProp = { type: "object" }
111+
const result = formatObjectProperties(obj, 0)
112+
expect(result).toBe("")
113+
})
114+
115+
test("single required property", ({ expect }) => {
116+
const obj: ObjectProp = {
117+
type: "object",
118+
properties: { prop1: { type: "string" } },
119+
required: ["prop1"],
120+
}
121+
122+
const result = formatObjectProperties(obj, 0)
123+
expect(result).toBe("prop1: string,")
124+
})
125+
126+
test("multiple properties with optional ones", ({ expect }) => {
127+
const obj: ObjectProp = {
128+
type: "object",
129+
properties: { prop1: { type: "string" }, prop2: { type: "number" } },
130+
required: ["prop1"],
131+
}
132+
133+
const result = formatObjectProperties(obj, 0)
134+
expect(result).toBe("prop1: string,\nprop2?: number,")
135+
})
136+
137+
test("nested objects", ({ expect }) => {
138+
const obj: ObjectProp = {
139+
type: "object",
140+
properties: {
141+
prop1: { type: "object", properties: { subProp: { type: "boolean" } } },
142+
},
143+
required: ["prop1"],
144+
}
145+
146+
const result = formatObjectProperties(obj, 0)
147+
expect(result).toBe("prop1: {\n subProp?: boolean,\n},")
148+
})
149+
})
150+
151+
describe("formatType", () => {
152+
test("string", ({ expect }) => {
153+
const param: Prop = { type: "string" }
154+
const result = formatType(param, 0)
155+
156+
expect(result).toBe("string")
157+
})
158+
159+
test("number", ({ expect }) => {
160+
const param: Prop = { type: "number" }
161+
const result = formatType(param, 0)
162+
163+
expect(result).toBe("number")
164+
})
165+
166+
test("integer", ({ expect }) => {
167+
const param: Prop = { type: "integer" }
168+
const result = formatType(param, 0)
169+
170+
expect(result).toBe("number")
171+
})
172+
173+
test("boolean", ({ expect }) => {
174+
const param: Prop = { type: "boolean" }
175+
const result = formatType(param, 0)
176+
177+
expect(result).toBe("boolean")
178+
})
179+
180+
test("null", ({ expect }) => {
181+
const param: Prop = { type: "null" }
182+
const result = formatType(param, 0)
183+
184+
expect(result).toBe("null")
185+
})
186+
187+
test("object", ({ expect }) => {
188+
const param: Prop = {
189+
type: "object",
190+
properties: { prop1: { type: "string" } },
191+
required: ["prop1"],
192+
}
193+
194+
const result = formatType(param, 0)
195+
expect(result).toBe("{\n prop1: string,\n}")
196+
})
197+
198+
test("array", ({ expect }) => {
199+
const param: Prop = { type: "array", items: { type: "string" } }
200+
const result = formatType(param, 0)
201+
202+
expect(result).toBe("string[]")
203+
})
204+
205+
test("anyOf", ({ expect }) => {
206+
const param: Prop = { anyOf: [{ type: "string" }, { type: "number" }] }
207+
208+
const result = formatType(param, 0)
209+
expect(result).toBe("string | number")
210+
})
211+
})

0 commit comments

Comments
 (0)