util: Add a GNOME version comparison function

Add a function that can compare GNOME versions, including the new naming
scheme for GNOME 40 and later.

Used in https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/3632

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1619>
This commit is contained in:
Bastien Nocera 2021-02-04 12:26:15 +01:00 committed by Marge Bot
parent af4e54bfc9
commit b0a48fad57
3 changed files with 92 additions and 2 deletions

View File

@ -1,7 +1,7 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported findUrls, spawn, spawnCommandLine, spawnApp, trySpawnCommandLine,
formatTime, formatTimeSpan, createTimeLabel, insertSorted,
ensureActorVisibleInScrollView, wiggle, lerp */
ensureActorVisibleInScrollView, wiggle, lerp, GNOMEversionCompare */
const { Clutter, Gio, GLib, Shell, St, GnomeDesktop } = imports.gi;
const Gettext = imports.gettext;
@ -439,3 +439,41 @@ function wiggle(actor, params) {
function lerp(start, end, progress) {
return start + progress * (end - start);
}
// _GNOMEversionToNumber:
// @version: a GNOME version element
//
// Like Number() but returns sortable values for special-cases
// 'alpha' and 'beta'. Returns NaN for unhandled 'versions'.
function _GNOMEversionToNumber(version) {
let ret = Number(version);
if (!isNaN(ret))
return ret;
if (version === 'alpha')
return -2;
if (version === 'beta')
return -1;
return ret;
}
// GNOMEversionCompare:
// @version1: a string containing a GNOME version
// @version2: a string containing another GNOME version
//
// Returns an integer less than, equal to, or greater than
// zero, if version1 is older, equal or newer than version2
function GNOMEversionCompare(version1, version2) {
const v1Array = version1.split('.');
const v2Array = version2.split('.');
for (let i = 0; i < Math.max(v1Array.length, v2Array.length); i++) {
let elemV1 = _GNOMEversionToNumber(v1Array[i] || '0');
let elemV2 = _GNOMEversionToNumber(v2Array[i] || '0');
if (elemV1 < elemV2)
return -1;
if (elemV1 > elemV2)
return 1;
}
return 0;
}

View File

@ -10,7 +10,7 @@ run_test = configure_file(
testenv = environment()
testenv.set('GSETTINGS_SCHEMA_DIR', join_paths(meson.build_root(), 'data'))
foreach test : ['insertSorted', 'jsParse', 'markup', 'params', 'url']
foreach test : ['insertSorted', 'jsParse', 'markup', 'params', 'url', 'versionCompare']
test(test, run_test,
args: 'unit/@0@.js'.format(test),
env: testenv,

View File

@ -0,0 +1,52 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
// Test cases for version comparison
const JsUnit = imports.jsUnit;
const Environment = imports.ui.environment;
Environment.init();
const Util = imports.misc.util;
const tests = [
{ v1: '40',
v2: '40',
res: 0 },
{ v1: '40',
v2: '42',
res: -1 },
{ v1: '42',
v2: '40',
res: 1 },
{ v1: '3.38.0',
v2: '40',
res: -1 },
{ v1: '40',
v2: '3.38.0',
res: 1 },
{ v1: '40',
v2: '3.38.0',
res: 1 },
{ v1: '40.alpha.1.1',
v2: '40',
res: -1 },
{ v1: '40',
v2: '40.alpha.1.1',
res: 1 },
{ v1: '40.beta',
v2: '40',
res: -1 },
{ v1: '40.1',
v2: '40',
res: 1 },
{ v1: '',
v2: '40.alpha',
res: -1 },
];
for (let i = 0; i < tests.length; i++) {
name = 'Test #' + i + ' v1: ' + tests[i].v1 + ', v2: ' + tests[i].v2;
print(name);
JsUnit.assertEquals(name, Util.GNOMEversionCompare (tests[i].v1, tests[i].v2), tests[i].res);
}