Skip to content

Commit a207e43

Browse files
committed
[INTERNAL] SpecificationVersion: Add test helper function #getVersionsForRange
1 parent 44024d9 commit a207e43

File tree

16 files changed

+153
-81
lines changed

16 files changed

+153
-81
lines changed

lib/specifications/SpecificationVersion.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class SpecificationVersion {
6363
* Test whether the instance's Specification Version falls into the provided range
6464
*
6565
* @public
66-
@param {string} range [Semver]{@link https://www.npmjs.com/package/semver}-style version range,
67-
for example <code>2.2 - 2.4</code>
66+
* @param {string} range [Semver]{@link https://www.npmjs.com/package/semver}-style version range,
67+
* for example <code>2.2 - 2.4</code> or <code>=3.0</code>
6868
* @returns {boolean} True if the instance's Specification Version falls into the provided range
6969
*/
7070
satisfies(range) {
@@ -263,6 +263,22 @@ for example <code>2.2 - 2.4</code>
263263
const comparator = new SpecificationVersion(specVersion);
264264
return comparator.neq(testVersion);
265265
}
266+
267+
/**
268+
* Create an array of Specification Versions that match with the provided range. This is mainly used
269+
* for testing purposes. I.e. to execute identical tests for a range of specification versions.
270+
*
271+
* @public
272+
* @param {string} range [Semver]{@link https://www.npmjs.com/package/semver}-style version range,
273+
* for example <code>2.2 - 2.4</code> or <code>=3.0</code>
274+
* @returns {string[]} Array of versions that match the specified range
275+
*/
276+
static getVersionsForRange(range) {
277+
return SUPPORTED_VERSIONS.filter((specVersion) => {
278+
const comparator = new SpecificationVersion(specVersion);
279+
return comparator.satisfies(range);
280+
});
281+
}
266282
}
267283

268284
function getUnsupportedSpecVersionMessage(specVersion) {

test/lib/specifications/SpecificationVersion.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ test("(instance) satisfies", (t) => {
6767
t.is(new SpecificationVersion("2.2").satisfies("^2.2"), true);
6868
t.is(new SpecificationVersion("2.3").satisfies("^2.2"), true);
6969

70+
// range: >=2.2
71+
t.is(new SpecificationVersion("2.1").satisfies(">=2.2"), false);
72+
t.is(new SpecificationVersion("2.2").satisfies(">=2.2"), true);
73+
t.is(new SpecificationVersion("2.3").satisfies(">=2.2"), true);
74+
t.is(new SpecificationVersion("3.1").satisfies(">=2.2"), true);
75+
7076
// range: > 1.0
7177
t.is(new SpecificationVersion("1.0").satisfies("> 1.0"), false);
7278
t.is(new SpecificationVersion("1.1").satisfies("> 1.0"), true);
@@ -162,6 +168,12 @@ test("(static) satisfies", (t) => {
162168
t.is(SpecificationVersion.satisfies("2.2", "^2.2"), true);
163169
t.is(SpecificationVersion.satisfies("2.3", "^2.2"), true);
164170

171+
// range: >=2.2
172+
t.is(SpecificationVersion.satisfies("2.1", ">=2.2"), false);
173+
t.is(SpecificationVersion.satisfies("2.2", ">=2.2"), true);
174+
t.is(SpecificationVersion.satisfies("2.3", ">=2.2"), true);
175+
t.is(SpecificationVersion.satisfies("3.1", ">=2.2"), true);
176+
165177
// range: > 1.0
166178
t.is(SpecificationVersion.satisfies("1.0", "> 1.0"), false);
167179
t.is(SpecificationVersion.satisfies("1.1", "> 1.0"), true);
@@ -212,6 +224,45 @@ test("(static) low level comparator", (t) => {
212224
t.is(SpecificationVersion.neq("2.2", "2.2"), false);
213225
});
214226

227+
test("(static) getVersionsForRange", (t) => {
228+
// range: 1.x
229+
t.deepEqual(SpecificationVersion.getVersionsForRange("1.x"), [
230+
"1.0", "1.1"
231+
]);
232+
233+
// range: ^2.2
234+
t.deepEqual(SpecificationVersion.getVersionsForRange("^2.2"), [
235+
"2.2", "2.3", "2.4", "2.5", "2.6"
236+
]);
237+
238+
// range: >=2.2
239+
t.deepEqual(SpecificationVersion.getVersionsForRange(">=2.2"), [
240+
"2.2", "2.3", "2.4", "2.5", "2.6",
241+
"3.0", "3.1",
242+
]);
243+
244+
// range: > 1.0
245+
t.deepEqual(SpecificationVersion.getVersionsForRange("> 1.0"), [
246+
"1.1",
247+
"2.0", "2.1", "2.2", "2.3", "2.4", "2.5", "2.6",
248+
"3.0", "3.1",
249+
]);
250+
251+
// range: 2.2 - 2.4
252+
t.deepEqual(SpecificationVersion.getVersionsForRange("2.2 - 2.4"), [
253+
"2.2", "2.3", "2.4"
254+
]);
255+
256+
// range: 0.1 || 1.0 - 1.1 || ^2.5
257+
t.deepEqual(SpecificationVersion.getVersionsForRange("0.1 || 1.0 - 1.1 || ^2.5"), [
258+
"0.1", "1.0", "1.1",
259+
"2.5", "2.6"
260+
]);
261+
262+
// Incorrect range returns empty array
263+
t.deepEqual(SpecificationVersion.getVersionsForRange("not a range"), []);
264+
});
265+
215266
test("getSemverCompatibleVersion", (t) => {
216267
t.is(__localFunctions__.getSemverCompatibleVersion("0.1"), "0.1.0");
217268
t.is(__localFunctions__.getSemverCompatibleVersion("1.1"), "1.1.0");

test/lib/validation/schema/__helper__/builder-bundleOptions.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import SpecificationVersion from "../../../../../lib/specifications/SpecificationVersion.js";
2+
13
/**
24
* Common test functionality for builder/bundles/bundleOptions section in config
35
*/
@@ -10,13 +12,9 @@ export default {
1012
* @param {string} type one of "application" and "library", "component"
1113
*/
1214
defineTests: function(test, assertValidation, type) {
13-
// Version specific tests
14-
let specVersionsToTest = ["3.1", "3.0"];
15-
if (type === "component") {
16-
// Component type only became available with specVersion 3.1
17-
specVersionsToTest = ["3.1"];
18-
}
19-
specVersionsToTest.forEach(function(specVersion) {
15+
// Version specific tests (component type only became available with specVersion 3.1)
16+
const range = type === "component" ? ">=3.1" : ">=3.0";
17+
SpecificationVersion.getVersionsForRange(range).forEach(function(specVersion) {
2018
test(`${type} (specVersion ${specVersion}): builder/bundles/bundleOptions`, async (t) => {
2119
await assertValidation(t, {
2220
"specVersion": specVersion,

test/lib/validation/schema/__helper__/customConfiguration.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import SpecificationVersion from "../../../../../lib/specifications/SpecificationVersion.js";
2+
13
/**
24
* Common test functionality for customConfiguration section in config
35
*/
@@ -13,31 +15,32 @@ export default {
1315
*/
1416
defineTests: function(test, assertValidation, type, additionalConfiguration) {
1517
additionalConfiguration = additionalConfiguration || {};
16-
if (type === "component") {
17-
return;
18-
}
19-
// version specific tests for customConfiguration
20-
test(`${type}: Invalid customConfiguration (specVersion 2.0)`, async (t) => {
21-
await assertValidation(t, Object.assign({
22-
"specVersion": "2.0",
23-
"type": type,
24-
"metadata": {
25-
"name": "my-" + type
26-
},
27-
"customConfiguration": {}
28-
}, additionalConfiguration), [
29-
{
30-
dataPath: "",
31-
keyword: "additionalProperties",
32-
message: "should NOT have additional properties",
33-
params: {
34-
additionalProperty: "customConfiguration",
18+
if (type !== "component") { // Component type only became available with specVersion 3.1
19+
// version specific tests for customConfiguration
20+
test(`${type}: Invalid customConfiguration (specVersion 2.0)`, async (t) => {
21+
await assertValidation(t, Object.assign({
22+
"specVersion": "2.0",
23+
"type": type,
24+
"metadata": {
25+
"name": "my-" + type
26+
},
27+
"customConfiguration": {}
28+
}, additionalConfiguration), [
29+
{
30+
dataPath: "",
31+
keyword: "additionalProperties",
32+
message: "should NOT have additional properties",
33+
params: {
34+
additionalProperty: "customConfiguration",
35+
}
3536
}
36-
}
37-
]);
38-
});
37+
]);
38+
});
39+
}
3940

40-
["2.6", "2.5", "2.4", "2.3", "2.2", "2.1"].forEach((specVersion) => {
41+
// Component type only became available with specVersion 3.1
42+
const range = type === "component" ? ">=3.1" : ">=2.1";
43+
SpecificationVersion.getVersionsForRange(range).forEach((specVersion) => {
4144
test(`${type}: Valid customConfiguration (specVersion ${specVersion})`, async (t) => {
4245
await assertValidation(t, Object.assign( {
4346
"specVersion": specVersion,

test/lib/validation/schema/__helper__/extension.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import SpecificationVersion from "../../../../../lib/specifications/SpecificationVersion.js";
12
import customConfiguration from "./customConfiguration.js";
23

34
/**
@@ -18,7 +19,7 @@ export default {
1819

1920
customConfiguration.defineTests(test, assertValidation, type, additionalConfiguration);
2021

21-
["3.1", "3.0", "2.6", "2.5", "2.4", "2.3", "2.2", "2.1", "2.0"].forEach((specVersion) => {
22+
SpecificationVersion.getVersionsForRange(">=2.0").forEach((specVersion) => {
2223
test(`kind: extension / type: ${type} basic (${specVersion})`, async (t) => {
2324
await assertValidation(t, Object.assign({
2425
"specVersion": specVersion,
@@ -67,7 +68,7 @@ export default {
6768
});
6869
});
6970

70-
["2.6", "2.5", "2.4", "2.3", "2.2", "2.1", "2.0"].forEach((specVersion) => {
71+
SpecificationVersion.getVersionsForRange("2.0 - 2.6").forEach((specVersion) => {
7172
test(`kind: extension / type: ${type}: Invalid metadata.name (${specVersion})`, async (t) => {
7273
await assertValidation(t, Object.assign({
7374
"specVersion": specVersion,
@@ -86,7 +87,7 @@ export default {
8687
});
8788
});
8889

89-
["3.1", "3.0"].forEach((specVersion) => {
90+
SpecificationVersion.getVersionsForRange(">=3.0").forEach((specVersion) => {
9091
test(`kind: extension / type: ${type}: Invalid metadata.name (${specVersion})`, async (t) => {
9192
await assertValidation(t, Object.assign({
9293
"specVersion": specVersion,

test/lib/validation/schema/__helper__/framework.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import SpecificationVersion from "../../../../../lib/specifications/SpecificationVersion.js";
2+
13
/**
24
* Common test functionality for framework section in config
35
*/
@@ -11,12 +13,9 @@ export default {
1113
* @param {string} type one of "application", "component", library" and "theme-library"
1214
*/
1315
defineTests: function(test, assertValidation, type) {
14-
let specVersionsToTest = ["3.1", "3.0", "2.6", "2.5", "2.4", "2.3", "2.2", "2.1", "2.0"];
15-
if (type === "component") {
16-
// Component type only became available with specVersion 3.1
17-
specVersionsToTest = ["3.1"];
18-
}
19-
specVersionsToTest.forEach((specVersion) => {
16+
// Component type only became available with specVersion 3.1
17+
const range = type === "component" ? ">=3.1" : ">=2.0";
18+
SpecificationVersion.getVersionsForRange(range).forEach((specVersion) => {
2019
test(`${type} (specVersion ${specVersion}): framework configuration: OpenUI5`, async (t) => {
2120
const config = {
2221
"specVersion": specVersion,

test/lib/validation/schema/__helper__/project.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import SpecificationVersion from "../../../../../lib/specifications/SpecificationVersion.js";
12
import framework from "./framework.js";
23
import customConfiguration from "./customConfiguration.js";
34
import bundleOptions from "./builder-bundleOptions.js";
@@ -29,12 +30,9 @@ export default {
2930
}
3031

3132
// version specific tests
32-
let specVersionsToTest = ["3.1", "3.0", "2.6", "2.5", "2.4", "2.3", "2.2", "2.1", "2.0"];
33-
if (type === "component") {
34-
// Component type only became available with specVersion 3.1
35-
specVersionsToTest = ["3.1"];
36-
}
37-
specVersionsToTest.forEach((specVersion) => {
33+
// Component type only became available with specVersion 3.1
34+
const range = type === "component" ? ">=3.1" : ">=2.0";
35+
SpecificationVersion.getVersionsForRange(range).forEach((specVersion) => {
3836
// tests for all kinds and version 2.0 and above
3937
test(`${type} (specVersion ${specVersion}): No metadata`, async (t) => {
4038
await assertValidation(t, {
@@ -288,12 +286,9 @@ export default {
288286
});
289287
}
290288

291-
let specVersionsToTest2 = ["3.1", "3.0"];
292-
if (type === "component") {
293-
// Component type only became available with specVersion 3.1
294-
specVersionsToTest2 = ["3.1"];
295-
}
296-
specVersionsToTest2.forEach((specVersion) => {
289+
// Component type only became available with specVersion 3.1
290+
const v3Range = type === "component" ? ">=3.1" : ">=3.0";
291+
SpecificationVersion.getVersionsForRange(v3Range).forEach((specVersion) => {
297292
test(`${type} (specVersion ${specVersion}): Invalid metadata.name`, async (t) => {
298293
await assertValidation(t, {
299294
"specVersion": specVersion,

test/lib/validation/schema/specVersion/kind/extension.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from "ava";
22
import Ajv from "ajv";
33
import ajvErrors from "ajv-errors";
4+
import SpecificationVersion from "../../../../../../lib/specifications/SpecificationVersion.js";
45
import AjvCoverage from "../../../../../utils/AjvCoverage.js";
56
import {_Validator as Validator} from "../../../../../../lib/validation/validator.js";
67
import ValidationError from "../../../../../../lib/validation/ValidationError.js";
@@ -38,7 +39,8 @@ test.after.always((t) => {
3839
};
3940
t.context.ajvCoverage.verify(thresholds);
4041
});
41-
["3.1", "3.0", "2.6", "2.5", "2.4", "2.3", "2.2", "2.1", "2.0"].forEach((specVersion) => {
42+
43+
SpecificationVersion.getVersionsForRange(">=2.0").forEach((specVersion) => {
4244
test(`Type project-shim (${specVersion})`, async (t) => {
4345
await assertValidation(t, {
4446
"specVersion": specVersion,

test/lib/validation/schema/specVersion/kind/extension/project-shim.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from "ava";
22
import Ajv from "ajv";
33
import ajvErrors from "ajv-errors";
4+
import SpecificationVersion from "../../../../../../../lib/specifications/SpecificationVersion.js";
45
import AjvCoverage from "../../../../../../utils/AjvCoverage.js";
56
import {_Validator as Validator} from "../../../../../../../lib/validation/validator.js";
67
import ValidationError from "../../../../../../../lib/validation/ValidationError.js";
@@ -45,7 +46,7 @@ test.after.always((t) => {
4546
t.context.ajvCoverage.verify(thresholds);
4647
});
4748

48-
["3.0", "2.6", "2.5", "2.4", "2.3", "2.2", "2.1", "2.0"].forEach((specVersion) => {
49+
SpecificationVersion.getVersionsForRange(">=2.0").forEach((specVersion) => {
4950
test(`kind: extension / type: project-shim (${specVersion})`, async (t) => {
5051
await assertValidation(t, {
5152
"specVersion": specVersion,
@@ -127,7 +128,7 @@ test.after.always((t) => {
127128
});
128129
});
129130

130-
["3.0"].forEach(function(specVersion) {
131+
SpecificationVersion.getVersionsForRange(">=3.0").forEach(function(specVersion) {
131132
test(`Invalid extension name (specVersion ${specVersion})`, async (t) => {
132133
await assertValidation(t, {
133134
"specVersion": specVersion,

test/lib/validation/schema/specVersion/kind/extension/server-middleware.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from "ava";
22
import Ajv from "ajv";
33
import ajvErrors from "ajv-errors";
4+
import SpecificationVersion from "../../../../../../../lib/specifications/SpecificationVersion.js";
45
import AjvCoverage from "../../../../../../utils/AjvCoverage.js";
56
import {_Validator as Validator} from "../../../../../../../lib/validation/validator.js";
67
import ValidationError from "../../../../../../../lib/validation/ValidationError.js";
@@ -45,7 +46,7 @@ test.after.always((t) => {
4546
t.context.ajvCoverage.verify(thresholds);
4647
});
4748

48-
["3.0"].forEach(function(specVersion) {
49+
SpecificationVersion.getVersionsForRange(">=3.0").forEach(function(specVersion) {
4950
test(`Invalid extension name (specVersion ${specVersion})`, async (t) => {
5051
await assertValidation(t, {
5152
"specVersion": specVersion,

test/lib/validation/schema/specVersion/kind/extension/task.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from "ava";
22
import Ajv from "ajv";
33
import ajvErrors from "ajv-errors";
4+
import SpecificationVersion from "../../../../../../../lib/specifications/SpecificationVersion.js";
45
import AjvCoverage from "../../../../../../utils/AjvCoverage.js";
56
import {_Validator as Validator} from "../../../../../../../lib/validation/validator.js";
67
import ValidationError from "../../../../../../../lib/validation/ValidationError.js";
@@ -45,7 +46,7 @@ test.after.always((t) => {
4546
t.context.ajvCoverage.verify(thresholds);
4647
});
4748

48-
["3.0"].forEach(function(specVersion) {
49+
SpecificationVersion.getVersionsForRange(">=3.0").forEach(function(specVersion) {
4950
test(`Invalid extension name (specVersion ${specVersion})`, async (t) => {
5051
await assertValidation(t, {
5152
"specVersion": specVersion,

0 commit comments

Comments
 (0)