Skip to content

Commit 276b765

Browse files
committed
Consolidate result.test.mts: parameterize try* method tests
Refactor tryFromString, tryFromObject, and tryFromJSON tests into parameterized it.each tests with valid/invalid input cases. Reduces ~35 lines while maintaining identical test coverage.
1 parent c8c29b0 commit 276b765

File tree

1 file changed

+135
-60
lines changed

1 file changed

+135
-60
lines changed

test/result.test.mts

Lines changed: 135 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -230,73 +230,148 @@ describe('Result types', () => {
230230

231231
describe('PackageURL Result methods', () => {
232232
describe('tryFromString', () => {
233-
it('should return Ok for valid purl string', () => {
234-
const result = PackageURL.tryFromString('pkg:npm/[email protected]')
235-
236-
expect(result.isOk()).toBe(true)
237-
const purl = result.unwrap()
238-
expect(purl.type).toBe('npm')
239-
expect(purl.name).toBe('lodash')
240-
expect(purl.version).toBe('4.17.21')
241-
})
242-
243-
it('should return Err for invalid purl string', () => {
244-
const result = PackageURL.tryFromString('invalid-purl')
245-
246-
expect(result.isErr()).toBe(true)
247-
expect((result as Err<Error>).error).toBeInstanceOf(Error)
248-
})
233+
it.each([
234+
[
235+
'valid purl string',
236+
'pkg:npm/[email protected]',
237+
true,
238+
'npm',
239+
'lodash',
240+
'4.17.21',
241+
],
242+
[
243+
'invalid purl string',
244+
'invalid-purl',
245+
false,
246+
undefined,
247+
undefined,
248+
undefined,
249+
],
250+
] as const)(
251+
'should return %s result for %s',
252+
(
253+
_desc,
254+
input,
255+
shouldBeOk,
256+
expectedType,
257+
expectedName,
258+
expectedVersion,
259+
) => {
260+
const result = PackageURL.tryFromString(input)
261+
262+
expect(result.isOk()).toBe(shouldBeOk)
263+
if (shouldBeOk) {
264+
const purl = result.unwrap()
265+
expect(purl.type).toBe(expectedType)
266+
expect(purl.name).toBe(expectedName)
267+
expect(purl.version).toBe(expectedVersion)
268+
} else {
269+
expect((result as Err<Error>).error).toBeInstanceOf(Error)
270+
}
271+
},
272+
)
249273
})
250274

251275
describe('tryFromObject', () => {
252-
it('should return Ok for valid object', () => {
253-
const obj = { type: 'npm', name: 'lodash', version: '4.17.21' }
254-
const result = PackageURL.tryFromObject(obj)
255-
256-
expect(result.isOk()).toBe(true)
257-
const purl = result.unwrap()
258-
expect(purl.type).toBe('npm')
259-
expect(purl.name).toBe('lodash')
260-
expect(purl.version).toBe('4.17.21')
261-
})
262-
263-
it('should return Err for invalid object', () => {
264-
const obj = { type: '', name: 'lodash' }
265-
const result = PackageURL.tryFromObject(obj)
266-
267-
expect(result.isErr()).toBe(true)
268-
expect((result as Err<Error>).error).toBeInstanceOf(Error)
269-
})
276+
it.each([
277+
[
278+
'valid object',
279+
{ type: 'npm', name: 'lodash', version: '4.17.21' },
280+
true,
281+
'npm',
282+
'lodash',
283+
'4.17.21',
284+
],
285+
[
286+
'invalid object',
287+
{ type: '', name: 'lodash' },
288+
false,
289+
undefined,
290+
undefined,
291+
undefined,
292+
],
293+
] as const)(
294+
'should return %s result for %s',
295+
(
296+
_desc,
297+
input,
298+
shouldBeOk,
299+
expectedType,
300+
expectedName,
301+
expectedVersion,
302+
) => {
303+
const result = PackageURL.tryFromObject(input)
304+
305+
expect(result.isOk()).toBe(shouldBeOk)
306+
if (shouldBeOk) {
307+
const purl = result.unwrap()
308+
expect(purl.type).toBe(expectedType)
309+
expect(purl.name).toBe(expectedName)
310+
expect(purl.version).toBe(expectedVersion)
311+
} else {
312+
expect((result as Err<Error>).error).toBeInstanceOf(Error)
313+
}
314+
},
315+
)
270316
})
271317

272318
describe('tryFromJSON', () => {
273-
it('should return Ok for valid JSON', () => {
274-
const json = '{"type":"npm","name":"lodash","version":"4.17.21"}'
275-
const result = PackageURL.tryFromJSON(json)
276-
277-
expect(result.isOk()).toBe(true)
278-
const purl = result.unwrap()
279-
expect(purl.type).toBe('npm')
280-
expect(purl.name).toBe('lodash')
281-
expect(purl.version).toBe('4.17.21')
282-
})
283-
284-
it('should return Err for invalid JSON', () => {
285-
const result = PackageURL.tryFromJSON('invalid json')
286-
287-
expect(result.isErr()).toBe(true)
288-
expect((result as Err<Error>).error.message).toContain(
319+
it.each([
320+
[
321+
'valid JSON',
322+
'{"type":"npm","name":"lodash","version":"4.17.21"}',
323+
true,
324+
'npm',
325+
'lodash',
326+
'4.17.21',
327+
undefined,
328+
],
329+
[
330+
'invalid JSON',
331+
'invalid json',
332+
false,
333+
undefined,
334+
undefined,
335+
undefined,
289336
'Invalid JSON string',
290-
)
291-
})
292-
293-
it('should return Err for valid JSON with invalid purl data', () => {
294-
const json = '{"type":"","name":"lodash"}'
295-
const result = PackageURL.tryFromJSON(json)
296-
297-
expect(result.isErr()).toBe(true)
298-
expect((result as Err<Error>).error).toBeInstanceOf(Error)
299-
})
337+
],
338+
[
339+
'valid JSON with invalid purl data',
340+
'{"type":"","name":"lodash"}',
341+
false,
342+
undefined,
343+
undefined,
344+
undefined,
345+
undefined,
346+
],
347+
] as const)(
348+
'should return %s result for %s',
349+
(
350+
_desc,
351+
input,
352+
shouldBeOk,
353+
expectedType,
354+
expectedName,
355+
expectedVersion,
356+
errorMessageContains,
357+
) => {
358+
const result = PackageURL.tryFromJSON(input)
359+
360+
expect(result.isOk()).toBe(shouldBeOk)
361+
if (shouldBeOk) {
362+
const purl = result.unwrap()
363+
expect(purl.type).toBe(expectedType)
364+
expect(purl.name).toBe(expectedName)
365+
expect(purl.version).toBe(expectedVersion)
366+
} else {
367+
const error = (result as Err<Error>).error
368+
expect(error).toBeInstanceOf(Error)
369+
if (errorMessageContains !== undefined) {
370+
expect(error.message).toContain(errorMessageContains)
371+
}
372+
}
373+
},
374+
)
300375
})
301376

302377
describe('tryParseString', () => {

0 commit comments

Comments
 (0)