Skip to content

Commit 4187d31

Browse files
committed
Consolidate package-url.test.mts: parameterize validation tests
Consolidate 11 validation function tests into a single parameterized it.each test covering both new API ({ throws: boolean }) and legacy boolean parameter compatibility. Tests: validateRequired, validateRequiredByType, validateStartsWithoutNumber, validateEmptyByType, validateName, validateNamespace, validateQualifierKey, validateStrings, validateType, validateVersion. Reduces ~120 lines while maintaining identical test coverage.
1 parent 276b765 commit 4187d31

File tree

1 file changed

+146
-144
lines changed

1 file changed

+146
-144
lines changed

test/package-url.test.mts

Lines changed: 146 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,167 +1749,169 @@ describe('PackageURL', () => {
17491749
})
17501750

17511751
// Test validation functions with throws parameter
1752-
it('should handle validation errors with throws parameter', () => {
1753-
// validateRequired
1754-
expect(() => validateRequired('field', null, { throws: true })).toThrow(
1755-
'"field" is a required component',
1756-
)
1757-
expect(() => validateRequired('field', '', { throws: true })).toThrow(
1752+
it.each([
1753+
[
1754+
'validateRequired',
1755+
validateRequired,
1756+
'field',
1757+
null as unknown,
17581758
'"field" is a required component',
1759-
)
1760-
expect(validateRequired('field', null, { throws: false })).toBe(false)
1761-
expect(validateRequired('field', '', { throws: false })).toBe(false)
1762-
// Test with undefined parameter (line 160)
1763-
expect(validateRequired('field', null, undefined)).toBe(false)
1764-
expect(validateRequired('field', 'value', undefined)).toBe(true)
1765-
// Test with legacy boolean parameter
1766-
expect(validateRequired('field', null, false)).toBe(false)
1767-
expect(validateRequired('field', 'value', false)).toBe(true)
1768-
expect(() => validateRequired('field', null, true)).toThrow(
1759+
'value' as unknown,
1760+
],
1761+
[
1762+
'validateRequired empty string',
1763+
validateRequired,
1764+
'field',
1765+
'' as unknown,
17691766
'"field" is a required component',
1770-
)
1771-
1772-
// validateRequiredByType
1773-
expect(() =>
1774-
validateRequiredByType('npm', 'name', null, { throws: true }),
1775-
).toThrow('npm requires a "name" component')
1776-
expect(() =>
1777-
validateRequiredByType('npm', 'name', '', { throws: true }),
1778-
).toThrow('npm requires a "name" component')
1779-
expect(
1780-
validateRequiredByType('npm', 'name', null, { throws: false }),
1781-
).toBe(false)
1782-
// Test with undefined parameter (line 182)
1783-
expect(validateRequiredByType('npm', 'name', null, undefined)).toBe(
1784-
false,
1785-
)
1786-
expect(validateRequiredByType('npm', 'name', 'value', undefined)).toBe(
1787-
true,
1788-
)
1789-
// Test with legacy boolean parameter
1790-
expect(validateRequiredByType('npm', 'name', null, false)).toBe(false)
1791-
expect(() => validateRequiredByType('npm', 'name', null, true)).toThrow(
1767+
'value' as unknown,
1768+
],
1769+
[
1770+
'validateRequiredByType',
1771+
(_field: string, value: unknown, opts: any) =>
1772+
validateRequiredByType('npm', 'name', value, opts),
1773+
'name',
1774+
null as unknown,
17921775
'npm requires a "name" component',
1793-
)
1776+
'value' as unknown,
1777+
],
1778+
[
1779+
'validateRequiredByType empty string',
1780+
(_field: string, value: unknown, opts: any) =>
1781+
validateRequiredByType('npm', 'name', value, opts),
1782+
'name',
1783+
'' as unknown,
1784+
'npm requires a "name" component',
1785+
'value' as unknown,
1786+
],
1787+
[
1788+
'validateStartsWithoutNumber',
1789+
validateStartsWithoutNumber,
1790+
'field',
1791+
'1test' as unknown,
1792+
'field "1test" cannot start with a number',
1793+
'test' as unknown,
1794+
],
1795+
[
1796+
'validateEmptyByType',
1797+
(_field: string, value: unknown, opts: any) =>
1798+
validateEmptyByType('swift', 'namespace', value, opts),
1799+
'namespace',
1800+
'not-empty' as unknown,
1801+
/swift "namespace" component must be empty/,
1802+
'' as unknown,
1803+
],
1804+
[
1805+
'validateName',
1806+
(_field: string, value: unknown, opts: any) =>
1807+
validateName(value, opts),
1808+
'name',
1809+
null as unknown,
1810+
/"name" is a required component/,
1811+
'valid' as unknown,
1812+
],
1813+
[
1814+
'validateNamespace',
1815+
(_field: string, value: unknown, opts: any) =>
1816+
validateNamespace(value, opts),
1817+
'namespace',
1818+
123 as unknown,
1819+
/"namespace" must be a string/,
1820+
'valid' as unknown,
1821+
],
1822+
[
1823+
'validateQualifierKey',
1824+
(_field: string, value: unknown, opts: any) =>
1825+
validateQualifierKey(value as string, opts),
1826+
'key',
1827+
'key!invalid' as unknown,
1828+
/qualifier "key!invalid" contains an illegal character/,
1829+
'validkey' as unknown,
1830+
],
1831+
[
1832+
'validateStrings',
1833+
(field: string, value: unknown, opts: any) =>
1834+
validateStrings(field, value, opts),
1835+
'test',
1836+
123 as unknown,
1837+
/"test" must be a string/,
1838+
'valid' as unknown,
1839+
],
1840+
[
1841+
'validateType',
1842+
(_field: string, value: unknown, opts: any) =>
1843+
validateType(value, opts),
1844+
'type',
1845+
'type$illegal' as unknown,
1846+
/type "type\$illegal" contains an illegal character/,
1847+
'validtype' as unknown,
1848+
],
1849+
[
1850+
'validateVersion',
1851+
(_field: string, value: unknown, opts: any) =>
1852+
validateVersion(value, opts),
1853+
'version',
1854+
123 as unknown,
1855+
/"version" must be a string/,
1856+
'1.0.0' as unknown,
1857+
],
1858+
])(
1859+
'should support both option object and legacy boolean parameter for %s',
1860+
(_name, validatorFn, field, invalidValue, errorMessage, validValue) => {
1861+
// Test new API with { throws: true }
1862+
expect(() =>
1863+
validatorFn(field as string, invalidValue as any, { throws: true }),
1864+
).toThrow(errorMessage)
1865+
// Test new API with { throws: false }
1866+
expect(
1867+
validatorFn(field as string, invalidValue as any, {
1868+
throws: false,
1869+
}),
1870+
).toBe(false)
1871+
// Test with undefined parameter
1872+
expect(
1873+
validatorFn(field as string, invalidValue as any, undefined),
1874+
).toBe(false)
1875+
expect(
1876+
validatorFn(field as string, validValue as any, undefined),
1877+
).toBe(true)
1878+
// Test legacy boolean parameter
1879+
expect(validatorFn(field as string, invalidValue as any, false)).toBe(
1880+
false,
1881+
)
1882+
expect(validatorFn(field as string, validValue as any, false)).toBe(
1883+
true,
1884+
)
1885+
expect(() =>
1886+
validatorFn(field as string, invalidValue as any, true),
1887+
).toThrow(errorMessage)
1888+
},
1889+
)
17941890

1795-
// validateStartsWithoutNumber
1796-
expect(() =>
1797-
validateStartsWithoutNumber('field', '1test', { throws: true }),
1798-
).toThrow('field "1test" cannot start with a number')
1799-
expect(
1800-
validateStartsWithoutNumber('field', '1test', { throws: false }),
1801-
).toBe(false)
1802-
// Test with undefined parameter (line 203)
1803-
expect(validateStartsWithoutNumber('field', '1test', undefined)).toBe(
1804-
false,
1805-
)
1806-
expect(validateStartsWithoutNumber('field', 'test', undefined)).toBe(
1891+
it('should validate qualifiers with both option object and legacy boolean parameter', () => {
1892+
// Test new API
1893+
expect(validateQualifiers({ key: 'value' }, { throws: false })).toBe(
18071894
true,
18081895
)
1809-
// Test with legacy boolean parameter
1810-
expect(validateStartsWithoutNumber('field', '1test', false)).toBe(false)
1811-
expect(() =>
1812-
validateStartsWithoutNumber('field', '1test', true),
1813-
).toThrow('field "1test" cannot start with a number')
1896+
expect(validateQualifiers(null, { throws: false })).toBe(true)
1897+
expect(validateQualifiers([], { throws: false })).toBe(false)
1898+
expect(() => validateQualifiers([], { throws: true })).toThrow(
1899+
'"qualifiers" must be a plain object',
1900+
)
18141901

1815-
// validateQualifiers with undefined parameter (line 123)
1902+
// Test with undefined parameter
18161903
expect(validateQualifiers({ key: 'value' }, undefined)).toBe(true)
18171904
expect(validateQualifiers(null, undefined)).toBe(true)
18181905
expect(validateQualifiers([], undefined)).toBe(false)
1819-
// Test with legacy boolean parameter
1906+
1907+
// Test legacy boolean parameter
18201908
expect(validateQualifiers({ key: 'value' }, false)).toBe(true)
18211909
expect(validateQualifiers([], false)).toBe(false)
18221910
expect(() => validateQualifiers([], true)).toThrow(
18231911
'"qualifiers" must be a plain object',
18241912
)
18251913
})
18261914

1827-
// Test legacy boolean parameter support for backward compatibility
1828-
it('should support legacy boolean parameter in validation functions', () => {
1829-
// validateEmptyByType with legacy boolean parameter (line 28)
1830-
expect(() =>
1831-
validateEmptyByType('swift', 'namespace', 'not-empty', true),
1832-
).toThrow(/swift "namespace" component must be empty/)
1833-
expect(
1834-
validateEmptyByType('swift', 'namespace', 'not-empty', false),
1835-
).toBe(false)
1836-
expect(validateEmptyByType('swift', 'namespace', '', false)).toBe(true)
1837-
expect(validateEmptyByType('swift', 'namespace', null, false)).toBe(
1838-
true,
1839-
)
1840-
// Test with undefined parameter
1841-
expect(validateEmptyByType('swift', 'namespace', '', undefined)).toBe(
1842-
true,
1843-
)
1844-
expect(
1845-
validateEmptyByType('swift', 'namespace', 'not-empty', undefined),
1846-
).toBe(false)
1847-
1848-
// validateName with legacy boolean parameter (line 47)
1849-
expect(() => validateName(null, true)).toThrow(
1850-
/"name" is a required component/,
1851-
)
1852-
expect(validateName(null, false)).toBe(false)
1853-
expect(validateName('valid', false)).toBe(true)
1854-
// Test with undefined parameter
1855-
expect(validateName('valid', undefined)).toBe(true)
1856-
expect(validateName(null, undefined)).toBe(false)
1857-
1858-
// validateNamespace with legacy boolean parameter (line 62)
1859-
expect(() => validateNamespace(123, true)).toThrow(
1860-
/"namespace" must be a string/,
1861-
)
1862-
expect(validateNamespace(123, false)).toBe(false)
1863-
expect(validateNamespace('valid', false)).toBe(true)
1864-
expect(validateNamespace(null, false)).toBe(true)
1865-
// Test with undefined parameter
1866-
expect(validateNamespace('valid', undefined)).toBe(true)
1867-
expect(validateNamespace(123, undefined)).toBe(false)
1868-
1869-
// validateQualifierKey with legacy boolean parameter (line 75)
1870-
expect(() => validateQualifierKey('key!invalid', true)).toThrow(
1871-
/qualifier "key!invalid" contains an illegal character/,
1872-
)
1873-
expect(validateQualifierKey('key!invalid', false)).toBe(false)
1874-
expect(validateQualifierKey('validkey', false)).toBe(true)
1875-
// Test with undefined parameter
1876-
expect(validateQualifierKey('validkey', undefined)).toBe(true)
1877-
expect(validateQualifierKey('key!invalid', undefined)).toBe(false)
1878-
1879-
// validateStrings with legacy boolean parameter (line 227)
1880-
expect(() => validateStrings('test', 123, true)).toThrow(
1881-
/"test" must be a string/,
1882-
)
1883-
expect(validateStrings('test', 123, false)).toBe(false)
1884-
expect(validateStrings('test', 'valid', false)).toBe(true)
1885-
expect(validateStrings('test', null, false)).toBe(true)
1886-
// Test with undefined parameter
1887-
expect(validateStrings('test', 'valid', undefined)).toBe(true)
1888-
expect(validateStrings('test', 123, undefined)).toBe(false)
1889-
1890-
// validateType with legacy boolean parameter (line 260)
1891-
expect(() => validateType('type$illegal', true)).toThrow(
1892-
/type "type\$illegal" contains an illegal character/,
1893-
)
1894-
expect(validateType('type$illegal', false)).toBe(false)
1895-
expect(validateType('validtype', false)).toBe(true)
1896-
// Test with undefined parameter
1897-
expect(validateType('validtype', undefined)).toBe(true)
1898-
expect(validateType('type$illegal', undefined)).toBe(false)
1899-
1900-
// validateVersion with legacy boolean parameter (line 309)
1901-
expect(validateVersion('1.0.0', false)).toBe(true)
1902-
expect(validateVersion(123, false)).toBe(false)
1903-
expect(() => validateVersion(123, true)).toThrow(
1904-
/"version" must be a string/,
1905-
)
1906-
expect(validateVersion(null, false)).toBe(true)
1907-
expect(validateVersion(undefined, false)).toBe(true)
1908-
// Test with undefined parameter
1909-
expect(validateVersion('1.0.0', undefined)).toBe(true)
1910-
expect(validateVersion(123, undefined)).toBe(false)
1911-
})
1912-
19131915
// Test npm namespace validation non-throws mode
19141916
it('should validate npm namespace without @ character in non-throws mode', () => {
19151917
// Test purl-type.ts lines 428-429 - return false path

0 commit comments

Comments
 (0)