Skip to content

Commit bb61358

Browse files
feat(CSAF2.1): #401 add mandatory test 6.2.39.2 - move common functions to an own file
1 parent 7b1ce42 commit bb61358

File tree

3 files changed

+61
-55
lines changed

3 files changed

+61
-55
lines changed

csaf_2_1/recommendedTests/recommendedTest_6_2_39_2.js

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import Ajv from 'ajv/dist/jtd.js'
2-
import translations from '../../lib/language_specific_translation/translations.js'
3-
import bcp47 from 'bcp47'
2+
import {
3+
containsOneNoteWithTitleAndCategory,
4+
getTranslationInDocumentLang,
5+
isLangSpecifiedAndNotEnglish,
6+
} from '../../lib/shared/languageSpecificTranslation.js'
47

58
const ajv = new Ajv()
69

@@ -42,55 +45,6 @@ const inputSchema = /** @type {const} */ ({
4245

4346
const validateSchema = ajv.compile(inputSchema)
4447

45-
/**
46-
* Checks if the document language is specified and not English
47-
*
48-
* @param {string | undefined} language - The language expression to check
49-
* @returns {boolean} True if the language is valid, false otherwise
50-
*/
51-
export function isLangSpecifiedAndNotEnglish(language) {
52-
return (
53-
!!language && !(bcp47.parse(language)?.langtag.language.language === 'en')
54-
)
55-
}
56-
57-
/**
58-
* test whether exactly one item in document notes exists that has the given title.
59-
* and the given category.
60-
* @param {({} & { category?: string | undefined; title?: string | undefined; } & Record<string, unknown>)[]} notes
61-
* @param {string} titleToFind
62-
* @param {string} category
63-
* @returns {boolean} True if the language is valid, false otherwise
64-
*/
65-
function containsOneNoteWithTitleAndCategory(notes, titleToFind, category) {
66-
return (
67-
notes.filter(
68-
(note) => note.category === category && note.title === titleToFind
69-
).length === 1
70-
)
71-
}
72-
73-
/**
74-
* Get the language specific translation of the given i18nKey
75-
* @param {{ document: { lang?: string; }; }} doc
76-
* @param {string} i18nKey
77-
* @return {string | undefined}
78-
*/
79-
export function getTranslationInDocumentLang(doc, i18nKey) {
80-
if (!doc.document.lang) {
81-
return undefined
82-
}
83-
const language = bcp47.parse(doc.document.lang)?.langtag.language.language
84-
85-
/** @type {Record<string, Record <string,string>>}*/
86-
const translationByLang = translations.translation
87-
if (!language || !translationByLang[language]) {
88-
return undefined
89-
} else {
90-
return translationByLang[language][i18nKey]
91-
}
92-
}
93-
9448
/**
9549
* If the document language is specified but not English, and the license_expression contains license
9650
* identifiers or exceptions that are not listed in the SPDX license list or Aboutcode's "ScanCode LicenseDB",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Checks if the document language is specified and not English
3+
*
4+
* @param {string | undefined} language - The language expression to check
5+
* @returns {boolean} True if the language is valid, false otherwise
6+
*/
7+
export function isLangSpecifiedAndNotEnglish(language) {
8+
return (
9+
!!language && !(bcp47.parse(language)?.langtag.language.language === 'en')
10+
)
11+
}
12+
import bcp47 from 'bcp47'
13+
import translations from '../../lib/language_specific_translation/translations.js'
14+
15+
/**
16+
* test whether exactly one item in document notes exists that has the given title.
17+
* and the given category.
18+
* @param {({} & { category?: string | undefined; title?: string | undefined; } & Record<string, unknown>)[]} notes
19+
* @param {string} titleToFind
20+
* @param {string} category
21+
* @returns {boolean} True if the language is valid, false otherwise
22+
*/
23+
export function containsOneNoteWithTitleAndCategory(
24+
notes,
25+
titleToFind,
26+
category
27+
) {
28+
return (
29+
notes.filter(
30+
(note) => note.category === category && note.title === titleToFind
31+
).length === 1
32+
)
33+
}
34+
35+
/**
36+
* Get the language specific translation of the given i18nKey
37+
* @param {{ document: { lang?: string; }; }} doc
38+
* @param {string} i18nKey
39+
* @return {string | undefined}
40+
*/
41+
export function getTranslationInDocumentLang(doc, i18nKey) {
42+
if (!doc.document.lang) {
43+
return undefined
44+
}
45+
const language = bcp47.parse(doc.document.lang)?.langtag.language.language
46+
47+
/** @type {Record<string, Record <string,string>>}*/
48+
const translationByLang = translations.translation
49+
if (!language || !translationByLang[language]) {
50+
return undefined
51+
} else {
52+
return translationByLang[language][i18nKey]
53+
}
54+
}

tests/csaf_2_1/recommendedTest_6_2_39_2.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import {
2-
getTranslationInDocumentLang,
3-
recommendedTest_6_2_39_2,
4-
} from '../../csaf_2_1/recommendedTests/recommendedTest_6_2_39_2.js'
1+
import { recommendedTest_6_2_39_2 } from '../../csaf_2_1/recommendedTests/recommendedTest_6_2_39_2.js'
52
import { expect } from 'chai'
63
import assert from 'node:assert'
4+
import { getTranslationInDocumentLang } from '../../lib/shared/languageSpecificTranslation.js'
75

86
describe('recommendedTest_6_2_39_2', function () {
97
it('only runs on relevant documents', function () {

0 commit comments

Comments
 (0)