Skip to content

Commit 28c5f97

Browse files
committed
Update tests to use options pattern for validation functions
- Convert test calls from boolean params to { throws } options - Maintain test coverage for all validation functions
1 parent 57a402b commit 28c5f97

File tree

1 file changed

+91
-61
lines changed

1 file changed

+91
-61
lines changed

test/package-url.test.mts

Lines changed: 91 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,29 +1748,33 @@ describe('PackageURL', () => {
17481748
// Test validation functions with throws parameter
17491749
it('should handle validation errors with throws parameter', () => {
17501750
// validateRequired
1751-
expect(() => validateRequired('field', null, true)).toThrow(
1751+
expect(() => validateRequired('field', null, { throws: true })).toThrow(
17521752
'"field" is a required component',
17531753
)
1754-
expect(() => validateRequired('field', '', true)).toThrow(
1754+
expect(() => validateRequired('field', '', { throws: true })).toThrow(
17551755
'"field" is a required component',
17561756
)
1757-
expect(validateRequired('field', null, false)).toBe(false)
1758-
expect(validateRequired('field', '', false)).toBe(false)
1757+
expect(validateRequired('field', null, { throws: false })).toBe(false)
1758+
expect(validateRequired('field', '', { throws: false })).toBe(false)
17591759

17601760
// validateRequiredByType
1761-
expect(() => validateRequiredByType('npm', 'name', null, true)).toThrow(
1762-
'npm requires a "name" component',
1763-
)
1764-
expect(() => validateRequiredByType('npm', 'name', '', true)).toThrow(
1765-
'npm requires a "name" component',
1766-
)
1767-
expect(validateRequiredByType('npm', 'name', null, false)).toBe(false)
1761+
expect(() =>
1762+
validateRequiredByType('npm', 'name', null, { throws: true }),
1763+
).toThrow('npm requires a "name" component')
1764+
expect(() =>
1765+
validateRequiredByType('npm', 'name', '', { throws: true }),
1766+
).toThrow('npm requires a "name" component')
1767+
expect(
1768+
validateRequiredByType('npm', 'name', null, { throws: false }),
1769+
).toBe(false)
17681770

17691771
// validateStartsWithoutNumber
17701772
expect(() =>
1771-
validateStartsWithoutNumber('field', '1test', true),
1773+
validateStartsWithoutNumber('field', '1test', { throws: true }),
17721774
).toThrow('field "1test" cannot start with a number')
1773-
expect(validateStartsWithoutNumber('field', '1test', false)).toBe(false)
1775+
expect(
1776+
validateStartsWithoutNumber('field', '1test', { throws: false }),
1777+
).toBe(false)
17741778
})
17751779

17761780
// Test index.js exports
@@ -1947,12 +1951,16 @@ describe('PackageURL', () => {
19471951
it('should validate empty component edge cases', () => {
19481952
// Test line 12 - return false without throwing
19491953
expect(
1950-
validateEmptyByType('swift', 'namespace', 'not-empty', false),
1954+
validateEmptyByType('swift', 'namespace', 'not-empty', {
1955+
throws: false,
1956+
}),
19511957
).toBe(false)
19521958

19531959
// Test with throws=true
19541960
expect(() =>
1955-
validateEmptyByType('swift', 'namespace', 'not-empty', true),
1961+
validateEmptyByType('swift', 'namespace', 'not-empty', {
1962+
throws: true,
1963+
}),
19561964
).toThrow(/swift "namespace" component must be empty/)
19571965
})
19581966

@@ -1961,26 +1969,30 @@ describe('PackageURL', () => {
19611969
// Import already at top of file
19621970

19631971
// Test lines 33-36 - qualifiers must be a plain object
1964-
expect(() => validateQualifiers('string-value', true)).toThrow(
1965-
/"qualifiers" must be a plain object/,
1966-
)
1972+
expect(() =>
1973+
validateQualifiers('string-value', { throws: true }),
1974+
).toThrow(/"qualifiers" must be a plain object/)
19671975

1968-
expect(validateQualifiers('string-value', false)).toBe(false)
1976+
expect(validateQualifiers('string-value', { throws: false })).toBe(
1977+
false,
1978+
)
19691979
})
19701980

19711981
// Test validateQualifierKey with invalid key
19721982
it('should validate qualifier key format', () => {
19731983
// Import already at top of file
19741984

19751985
// Test line 46 - return false
1976-
expect(validateQualifierKey('1invalid', false)).toBe(false)
1986+
expect(validateQualifierKey('1invalid', { throws: false })).toBe(false)
19771987

19781988
// Test lines 73-76 - illegal character in key
1979-
expect(() => validateQualifierKey('key!invalid', true)).toThrow(
1980-
/qualifier "key!invalid" contains an illegal character/,
1981-
)
1989+
expect(() =>
1990+
validateQualifierKey('key!invalid', { throws: true }),
1991+
).toThrow(/qualifier "key!invalid" contains an illegal character/)
19821992

1983-
expect(validateQualifierKey('key!invalid', false)).toBe(false)
1993+
expect(validateQualifierKey('key!invalid', { throws: false })).toBe(
1994+
false,
1995+
)
19841996
})
19851997

19861998
// Test encode.js branch coverage
@@ -2178,10 +2190,16 @@ describe('PackageURL', () => {
21782190
// Import already at top of file
21792191

21802192
// Test returning false without throwing
2181-
expect(validateQualifierKey('1startsWithNumber', false)).toBe(false)
2182-
expect(validateQualifierKey('has-dashes', false)).toBe(true)
2183-
expect(validateQualifierKey('has_underscores', false)).toBe(true)
2184-
expect(validateQualifierKey('has.periods', false)).toBe(true)
2193+
expect(
2194+
validateQualifierKey('1startsWithNumber', { throws: false }),
2195+
).toBe(false)
2196+
expect(validateQualifierKey('has-dashes', { throws: false })).toBe(true)
2197+
expect(validateQualifierKey('has_underscores', { throws: false })).toBe(
2198+
true,
2199+
)
2200+
expect(validateQualifierKey('has.periods', { throws: false })).toBe(
2201+
true,
2202+
)
21852203
})
21862204

21872205
// Test purl-component.js line 36
@@ -2404,12 +2422,14 @@ describe('PackageURL', () => {
24042422
// Import already at top of file
24052423

24062424
// Test line 46 - returns false when validateStartsWithoutNumber fails
2407-
expect(validateQualifierKey('1start', false)).toBe(false)
2408-
expect(validateQualifierKey('9number', false)).toBe(false)
2425+
expect(validateQualifierKey('1start', { throws: false })).toBe(false)
2426+
expect(validateQualifierKey('9number', { throws: false })).toBe(false)
24092427

24102428
// Valid keys
2411-
expect(validateQualifierKey('valid_key', false)).toBe(true)
2412-
expect(validateQualifierKey('another.valid-key', false)).toBe(true)
2429+
expect(validateQualifierKey('valid_key', { throws: false })).toBe(true)
2430+
expect(
2431+
validateQualifierKey('another.valid-key', { throws: false }),
2432+
).toBe(true)
24132433
})
24142434

24152435
// Test objects.js line 33 - check for recursiveFreeze edge case
@@ -2552,7 +2572,7 @@ describe('PackageURL', () => {
25522572
// Import already at top of file
25532573

25542574
const qualifiers = { '9key': 'value' }
2555-
const result = validateQualifiers(qualifiers, false)
2575+
const result = validateQualifiers(qualifiers, { throws: false })
25562576
expect(result).toBe(false)
25572577
})
25582578

@@ -2672,7 +2692,7 @@ describe('PackageURL', () => {
26722692

26732693
const params = new URLSearchParams()
26742694
params.append('valid_key', 'value')
2675-
const result = validateQualifiers(params, false)
2695+
const result = validateQualifiers(params, { throws: false })
26762696
expect(result).toBe(true)
26772697
})
26782698

@@ -2681,21 +2701,23 @@ describe('PackageURL', () => {
26812701
// Import already at top of file
26822702

26832703
// Test line 121 - validateStartsWithoutNumber
2684-
expect(validateStartsWithoutNumber('test', '0start', false)).toBe(false)
2704+
expect(
2705+
validateStartsWithoutNumber('test', '0start', { throws: false }),
2706+
).toBe(false)
26852707
expect(() =>
2686-
validateStartsWithoutNumber('test', '0start', true),
2708+
validateStartsWithoutNumber('test', '0start', { throws: true }),
26872709
).toThrow(/test "0start" cannot start with a number/)
26882710

26892711
// Test line 135 - validateSubpath empty check
2690-
expect(validateSubpath('', false)).toBe(true)
2691-
expect(validateSubpath(null, false)).toBe(true)
2712+
expect(validateSubpath('', { throws: false })).toBe(true)
2713+
expect(validateSubpath(null, { throws: false })).toBe(true)
26922714

26932715
// Test line 156 - validateRequiredByType
2694-
expect(validateRequiredByType('swift', 'version', '', false)).toBe(
2695-
false,
2696-
)
2716+
expect(
2717+
validateRequiredByType('swift', 'version', '', { throws: false }),
2718+
).toBe(false)
26972719
expect(() =>
2698-
validateRequiredByType('swift', 'version', '', true),
2720+
validateRequiredByType('swift', 'version', '', { throws: true }),
26992721
).toThrow(/swift requires a "version" component/)
27002722
})
27012723

@@ -2783,18 +2805,20 @@ describe('PackageURL', () => {
27832805
// Import already at top of file
27842806

27852807
// Test validateSubpath with various inputs (line 135)
2786-
expect(validateSubpath(undefined, false)).toBe(true)
2787-
expect(validateSubpath('valid/path', false)).toBe(true)
2808+
expect(validateSubpath(undefined, { throws: false })).toBe(true)
2809+
expect(validateSubpath('valid/path', { throws: false })).toBe(true)
27882810

27892811
// Test validateStartsWithoutNumber edge case (line 121)
2790-
expect(validateStartsWithoutNumber('qualifier', 'valid', false)).toBe(
2791-
true,
2792-
)
2812+
expect(
2813+
validateStartsWithoutNumber('qualifier', 'valid', { throws: false }),
2814+
).toBe(true)
27932815

27942816
// Test validateRequiredByType with non-empty value (line 156)
2795-
expect(validateRequiredByType('swift', 'version', '1.0.0', false)).toBe(
2796-
true,
2797-
)
2817+
expect(
2818+
validateRequiredByType('swift', 'version', '1.0.0', {
2819+
throws: false,
2820+
}),
2821+
).toBe(true)
27982822
})
27992823

28002824
// Final tests for 100% coverage
@@ -2885,15 +2909,19 @@ describe('PackageURL', () => {
28852909
// Import already at top of file
28862910

28872911
// Test validateStartsWithoutNumber with actual number start (line 121)
2888-
const result1 = validateStartsWithoutNumber('key', '5test', false)
2912+
const result1 = validateStartsWithoutNumber('key', '5test', {
2913+
throws: false,
2914+
})
28892915
expect(result1).toBe(false)
28902916

28912917
// Test validateSubpath with blank string (line 135)
2892-
const result2 = validateSubpath(' ', false)
2918+
const result2 = validateSubpath(' ', { throws: false })
28932919
expect(result2).toBe(true)
28942920

28952921
// Test validateRequiredByType with nullish value (line 156)
2896-
const result3 = validateRequiredByType('type', 'comp', null, false)
2922+
const result3 = validateRequiredByType('type', 'comp', null, {
2923+
throws: false,
2924+
})
28972925
expect(result3).toBe(false)
28982926
})
28992927

@@ -3071,17 +3099,19 @@ describe('PackageURL', () => {
30713099
// Import already at top of file
30723100

30733101
// Test validateStrings with non-string input (line 121)
3074-
expect(validateStrings('test', 123, false)).toBe(false)
3075-
expect(validateStrings('test', {}, false)).toBe(false)
3102+
expect(validateStrings('test', 123, { throws: false })).toBe(false)
3103+
expect(validateStrings('test', {}, { throws: false })).toBe(false)
30763104

30773105
// Test validateStartsWithoutNumber (line 135)
3078-
expect(validateStartsWithoutNumber('test', '9name', false)).toBe(false)
3106+
expect(
3107+
validateStartsWithoutNumber('test', '9name', { throws: false }),
3108+
).toBe(false)
30793109

30803110
// Test validateType with type starting with number (line 135 branch)
3081-
expect(validateType('9type', false)).toBe(false)
3111+
expect(validateType('9type', { throws: false })).toBe(false)
30823112

30833113
// Test validateType with illegal character in throws mode (line 156)
3084-
expect(() => validateType('type$illegal', true)).toThrow(
3114+
expect(() => validateType('type$illegal', { throws: true })).toThrow(
30853115
'type "type$illegal" contains an illegal character',
30863116
)
30873117
})
@@ -3407,15 +3437,15 @@ describe('PackageURL', () => {
34073437

34083438
it('should reject types with illegal characters without throwing errors', () => {
34093439
// Test validateType with illegal character (line 157-158 in validate.ts)
3410-
const result = validateType('type!invalid', false)
3440+
const result = validateType('type!invalid', { throws: false })
34113441
expect(result).toBe(false)
34123442

34133443
// Test with space
3414-
const result2 = validateType('type invalid', false)
3444+
const result2 = validateType('type invalid', { throws: false })
34153445
expect(result2).toBe(false)
34163446

34173447
// Test with special characters
3418-
const result3 = validateType('type@invalid', false)
3448+
const result3 = validateType('type@invalid', { throws: false })
34193449
expect(result3).toBe(false)
34203450
})
34213451

0 commit comments

Comments
 (0)