Skip to content

Commit 2d3ac19

Browse files
Copilotfelickz
andcommitted
Add comprehensive edge case tests for normalizeCweId function
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 77fb98c commit 2d3ac19

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

__tests__/main.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,49 @@ describe('main', () => {
3131
const normalizedId = normalizeCweId('abc')
3232
expect(normalizedId).toBeNull()
3333
})
34+
35+
it('should return null for empty strings', () => {
36+
// Test that empty strings return null
37+
const normalizedId = normalizeCweId('')
38+
expect(normalizedId).toBeNull()
39+
})
40+
41+
it('should return null for strings with only spaces', () => {
42+
// Test that strings with only spaces return null
43+
const normalizedId = normalizeCweId(' ')
44+
expect(normalizedId).toBeNull()
45+
})
46+
47+
it('should handle strings with leading/trailing spaces', () => {
48+
// Test that strings with spaces are parsed correctly
49+
const normalizedId = normalizeCweId(' 99 ')
50+
expect(normalizedId).toBe('99')
51+
})
52+
53+
it('should return null for negative numbers', () => {
54+
// Test that negative numbers return null (CWE IDs should be positive)
55+
const normalizedId = normalizeCweId('-99')
56+
expect(normalizedId).toBeNull()
57+
})
58+
59+
it('should handle strings with mixed alphanumeric characters (parseInt is lenient)', () => {
60+
// Test that mixed alphanumeric strings are parsed leniently
61+
// parseInt stops at first non-numeric character, so '99abc' becomes 99
62+
// This is acceptable for our use case as malformed tags would be rare
63+
const normalizedId = normalizeCweId('99abc')
64+
expect(normalizedId).toBe('99')
65+
})
66+
67+
it('should handle zero', () => {
68+
// Test that zero is handled correctly
69+
const normalizedId = normalizeCweId('0')
70+
expect(normalizedId).toBe('0')
71+
})
72+
73+
it('should handle zero with leading zeros', () => {
74+
// Test that zero with leading zeros is handled correctly
75+
const normalizedId = normalizeCweId('000')
76+
expect(normalizedId).toBe('0')
77+
})
3478
})
3579
})

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function log(message: string, level = LogLevel.Info): void {
4949
*/
5050
export function normalizeCweId(cweId: string): string | null {
5151
const parsedCweId = parseInt(cweId, 10)
52-
if (Number.isNaN(parsedCweId)) {
52+
if (Number.isNaN(parsedCweId) || parsedCweId < 0) {
5353
return null
5454
}
5555
return String(parsedCweId)

0 commit comments

Comments
 (0)