Skip to content

Commit d963974

Browse files
authored
tests: test internal helpers (#454)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent cb69542 commit d963974

File tree

6 files changed

+107
-6
lines changed

6 files changed

+107
-6
lines changed

.mocharc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2424
* @type {import('@types/mocha').Mocha.MochaOptions}
2525
*/
2626
module.exports = {
27+
timeout: 5000,
2728
spec: [
2829
'tests',
2930
'libs'

HISTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ All notable changes to this project will be documented in this file.
66

77
* Docs
88
* Use [TSDoc](https://tsdoc.org/) syntax in TypeScript files, instead of [JSDoc](https://jsdoc.app/) (via [#318], [#453])
9+
* Misc
10+
* Added tests for internal helpers (via [#454])
911

1012
[#318]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/318
1113
[#453]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/453
14+
[#454]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/454
1215

1316
## 1.10.0 - 2023-01-28
1417

tests/_data/specLoader.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const resPath = path.resolve(__dirname, '..', '..', 'res')
2525
/**
2626
* @param {string} resourceFile
2727
* @returns {*}
28+
* @throws {Error} if parsing the `resourceFile` failed somehow
2829
*/
2930
function loadSpec (resourceFile) {
3031
return JSON.parse(
@@ -38,11 +39,16 @@ function loadSpec (resourceFile) {
3839
* @param {string} resourceFile
3940
* @param {string} path
4041
* @returns {*}
42+
* @throws {TypeError} if resolving the `path` failed
43+
* @throws {Error} if parsing the `resourceFile` failed somehow
4144
*/
4245
function getSpecElement (resourceFile, ...path) {
4346
let element = loadSpec(resourceFile)
4447
for (const segment of path) {
4548
element = element[segment]
49+
if (undefined === element) {
50+
throw TypeError(`undefined element: ${resourceFile}#${path.join('.')}`)
51+
}
4652
}
4753
return element
4854
}
@@ -51,9 +57,18 @@ function getSpecElement (resourceFile, ...path) {
5157
* @param {string} resourceFile
5258
* @param {string} path
5359
* @returns {Array<number|string>}
60+
* @throws {TypeError} if resolved `path` is not non-empty-list
61+
* @throws {TypeError} if resolving the `path` failed
62+
* @throws {Error} if parsing the `resourceFile` failed somehow
5463
*/
5564
function getSpecEnum (resourceFile, ...path) {
56-
return getSpecElement(resourceFile, 'definitions', ...path, 'enum')
65+
const element = getSpecElement(
66+
resourceFile,
67+
'definitions', ...path, 'enum')
68+
if (!Array.isArray(element) || element.length === 0) {
69+
throw TypeError(`did not resolve non-empty-list for: ${resourceFile}#${path.join('.')}`)
70+
}
71+
return element
5772
}
5873

5974
module.exports = {

tests/_data/specLoader.spec.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*!
2+
This file is part of CycloneDX JavaScript Library.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache-2.0
17+
Copyright (c) OWASP Foundation. All Rights Reserved.
18+
*/
19+
20+
const assert = require('assert')
21+
const { suite, test } = require('mocha')
22+
23+
const { loadSpec, getSpecElement, getSpecEnum } = require('./specLoader')
24+
25+
suite('test helpers: specLoader', () => {
26+
/* eslint-disable-next-line camelcase -- for better readability, JSON-path replaced `.` with `_` */
27+
const expected_definitions_affectedStatus_enum = [
28+
'affected',
29+
'unaffected',
30+
'unknown'
31+
]
32+
33+
suite('loadSpec()', () => {
34+
test('unknown file', () => {
35+
assert.throws(
36+
() => {
37+
loadSpec('DOES-NOT-EXIST.schema.json')
38+
},
39+
Error,
40+
'missing expected error'
41+
)
42+
})
43+
test('happy path', () => {
44+
const loaded = loadSpec('bom-1.4.SNAPSHOT.schema.json')
45+
// dummy test to see if loading worked somehow ...
46+
assert.deepEqual(loaded.definitions.affectedStatus.enum, expected_definitions_affectedStatus_enum)
47+
})
48+
})
49+
50+
suite('getSpecElement()', () => {
51+
test('unknown path', () => {
52+
assert.throws(
53+
() => {
54+
getSpecElement(
55+
'bom-1.4.SNAPSHOT.schema.json',
56+
'properties', 'UNKNOWN_PROP')
57+
},
58+
TypeError('undefined element: bom-1.4.SNAPSHOT.schema.json#properties.UNKNOWN_PROP'),
59+
'missing expected error'
60+
)
61+
})
62+
test('happy path', () => {
63+
const loaded = getSpecElement(
64+
'bom-1.4.SNAPSHOT.schema.json',
65+
'definitions', 'affectedStatus', 'enum')
66+
// dummy test to see if loading worked somehow ...
67+
assert.deepEqual(loaded, expected_definitions_affectedStatus_enum)
68+
})
69+
})
70+
71+
suite('getSpecEnum()', () => {
72+
test('happy path', () => {
73+
const loaded = getSpecEnum(
74+
'bom-1.4.SNAPSHOT.schema.json',
75+
'affectedStatus')
76+
// dummy test to see if loading worked somehow ...
77+
assert.deepEqual(loaded, expected_definitions_affectedStatus_enum)
78+
})
79+
})
80+
})

tests/_helpers/stringFunctions.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const { suite, test } = require('mocha')
2222

2323
const stringFunctions = require('./stringFunctions')
2424

25-
suite('helpers: stringFunctions', () => {
25+
suite('test helpers: stringFunctions', () => {
2626
suite('capitaliseFirstLetter()', () => {
2727
[
2828
['foo', 'Foo'],

tests/unit/Models.Bom.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ suite('Models.Bom', () => {
7979
test(`for: ${newVersion}`, () => {
8080
const bom = new Bom()
8181
assert.notStrictEqual(bom.version, newVersion)
82-
83-
assert.throws(() => {
84-
bom.version = newVersion
85-
}, /not PositiveInteger/i)
82+
assert.throws(
83+
() => {
84+
bom.version = newVersion
85+
},
86+
/not PositiveInteger/i
87+
)
8688
})
8789
)
8890
)

0 commit comments

Comments
 (0)