Skip to content

Commit 0c6b0c6

Browse files
committed
test: add shared test utilities
Add reusable test helpers to reduce duplication: - param-validation.mts: consolidated 4 duplicate param validation functions - test-helpers.mts: consolidated 5 duplicate freeze testing functions
1 parent d38aec8 commit 0c6b0c6

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

test/utils/param-validation.mts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @fileoverview Utilities for testing PackageURL parameter validation
3+
*/
4+
5+
import { expect } from 'vitest'
6+
7+
import { PackageURL } from '../../dist/package-url.js'
8+
9+
type ParamValue = unknown
10+
type CreateArgsFn = (
11+
_name: string,
12+
_value: ParamValue,
13+
) => [unknown, unknown, unknown, unknown, unknown, unknown]
14+
15+
/**
16+
* Test parameter validation with various test values.
17+
* @param paramName - Name of the parameter to test
18+
* @param paramMap - Map of parameter names to argument positions
19+
* @param createArgs - Function to create constructor arguments
20+
* @param testValues - Array of values to test
21+
* @param shouldThrow - Whether the values should cause errors
22+
*/
23+
export function testParam(
24+
paramName: string,
25+
paramMap: Record<string, number>,
26+
createArgs: CreateArgsFn,
27+
testValues: ParamValue[],
28+
shouldThrow: boolean,
29+
) {
30+
const paramIndex = paramMap[paramName]
31+
testValues.forEach(value => {
32+
const args = createArgs(paramName, value)
33+
const message = JSON.stringify(args[paramIndex])
34+
if (shouldThrow) {
35+
expect(() => new PackageURL(...args), message).toThrow()
36+
} else {
37+
expect(() => new PackageURL(...args), message).not.toThrow()
38+
}
39+
})
40+
}
41+
42+
/**
43+
* Test that required parameters are validated correctly.
44+
* Tests various invalid inputs that should all throw.
45+
*/
46+
export function testInvalidParam(
47+
paramName: string,
48+
paramMap: Record<string, number>,
49+
createArgs: CreateArgsFn,
50+
) {
51+
testParam(
52+
paramName,
53+
paramMap,
54+
createArgs,
55+
[0, false, 1, true, {}, null, undefined, ''],
56+
true,
57+
)
58+
}
59+
60+
/**
61+
* Test that optional string parameters accept valid string values.
62+
* Tests various valid inputs that should all succeed.
63+
*/
64+
export function testValidStringParam(
65+
paramName: string,
66+
paramMap: Record<string, number>,
67+
createArgs: CreateArgsFn,
68+
) {
69+
testParam(
70+
paramName,
71+
paramMap,
72+
createArgs,
73+
[paramName, null, undefined, ''],
74+
false,
75+
)
76+
}
77+
78+
/**
79+
* Test that string parameters reject non-string values.
80+
* Tests various invalid type inputs that should all throw.
81+
*/
82+
export function testInvalidStringParam(
83+
paramName: string,
84+
paramMap: Record<string, number>,
85+
createArgs: CreateArgsFn,
86+
) {
87+
testParam(paramName, paramMap, createArgs, [0, false, 1, true, {}], true)
88+
}
89+
90+
/**
91+
* Test that required parameters accept valid values.
92+
*/
93+
export function testValidParam(
94+
paramName: string,
95+
paramMap: Record<string, number>,
96+
createArgs: CreateArgsFn,
97+
) {
98+
testParam(paramName, paramMap, createArgs, [paramName], false)
99+
}

test/utils/test-helpers.mts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @fileoverview Test helper utilities for creating test functions and data
3+
*/
4+
5+
/**
6+
* Create a test function with optional return value.
7+
* @param returnValue - Optional value to return from the function
8+
*/
9+
export function createTestFunction(returnValue?: string): () => string | void {
10+
if (returnValue !== undefined) {
11+
return function () {
12+
return returnValue
13+
}
14+
}
15+
return function () {}
16+
}

0 commit comments

Comments
 (0)