tests/unit: Fix remaining ESLint errors

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2822>
This commit is contained in:
Evan Welsh 2023-07-09 22:59:46 -07:00
parent 26e018206d
commit c570011376
9 changed files with 450 additions and 266 deletions

View File

@ -8,6 +8,6 @@ overrides:
- js/portalHelper/** - js/portalHelper/**
- subprojects/extensions-app/** - subprojects/extensions-app/**
- subprojects/extensions-tool/** - subprojects/extensions-tool/**
- tests/shell/** - tests/**
parserOptions: parserOptions:
sourceType: module sourceType: module

View File

@ -11,72 +11,116 @@ Environment.init();
const Util = imports.misc.util; const Util = imports.misc.util;
const tests = [ const tests = [
{ input: 'abc cba', {
terms: null, input: 'abc cba',
output: 'abc cba' }, terms: null,
{ input: 'abc cba', output: 'abc cba',
terms: [], },
output: 'abc cba' }, {
{ input: 'abc cba', input: 'abc cba',
terms: [''], terms: [],
output: 'abc cba' }, output: 'abc cba',
{ input: 'abc cba', },
terms: ['a'], {
output: '<b>a</b>bc cb<b>a</b>' }, input: 'abc cba',
{ input: 'abc cba', terms: [''],
terms: ['a', 'a'], output: 'abc cba',
output: '<b>a</b>bc cb<b>a</b>' }, },
{ input: 'CaSe InSenSiTiVe', {
terms: ['cas', 'sens'], input: 'abc cba',
output: '<b>CaS</b>e In<b>SenS</b>iTiVe' }, terms: ['a'],
{ input: 'This contains the < character', output: '<b>a</b>bc cb<b>a</b>',
terms: null, },
output: 'This contains the &lt; character' }, {
{ input: 'Don\'t', input: 'abc cba',
terms: ['t'], terms: ['a', 'a'],
output: 'Don&apos;<b>t</b>' }, output: '<b>a</b>bc cb<b>a</b>',
{ input: 'Don\'t', },
terms: ['n\'t'], {
output: 'Do<b>n&apos;t</b>' }, input: 'CaSe InSenSiTiVe',
{ input: 'Don\'t', terms: ['cas', 'sens'],
terms: ['o', 't'], output: '<b>CaS</b>e In<b>SenS</b>iTiVe',
output: 'D<b>o</b>n&apos;<b>t</b>' }, },
{ input: 'salt&pepper', {
terms: ['salt'], input: 'This contains the < character',
output: '<b>salt</b>&amp;pepper' }, terms: null,
{ input: 'salt&pepper', output: 'This contains the &lt; character',
terms: ['salt', 'alt'], },
output: '<b>salt</b>&amp;pepper' }, {
{ input: 'salt&pepper', input: 'Don\'t',
terms: ['pepper'], terms: ['t'],
output: 'salt&amp;<b>pepper</b>' }, output: 'Don&apos;<b>t</b>',
{ input: 'salt&pepper', },
terms: ['salt', 'pepper'], {
output: '<b>salt</b>&amp;<b>pepper</b>' }, input: 'Don\'t',
{ input: 'salt&pepper', terms: ['n\'t'],
terms: ['t', 'p'], output: 'Do<b>n&apos;t</b>',
output: 'sal<b>t</b>&amp;<b>p</b>e<b>p</b><b>p</b>er' }, },
{ input: 'salt&pepper', {
terms: ['t', '&', 'p'], input: 'Don\'t',
output: 'sal<b>t</b><b>&amp;</b><b>p</b>e<b>p</b><b>p</b>er' }, terms: ['o', 't'],
{ input: 'salt&pepper', output: 'D<b>o</b>n&apos;<b>t</b>',
terms: ['e'], },
output: 'salt&amp;p<b>e</b>pp<b>e</b>r' }, {
{ input: 'salt&pepper', input: 'salt&pepper',
terms: ['&a', '&am', '&amp', '&amp;'], terms: ['salt'],
output: 'salt&amp;pepper' }, output: '<b>salt</b>&amp;pepper',
{ input: '&&&&&', },
terms: ['a'], {
output: '&amp;&amp;&amp;&amp;&amp;' }, input: 'salt&pepper',
{ input: '&;&;&;&;&;', terms: ['salt', 'alt'],
terms: ['a'], output: '<b>salt</b>&amp;pepper',
output: '&amp;;&amp;;&amp;;&amp;;&amp;;' }, },
{ input: '&;&;&;&;&;', {
terms: [';'], input: 'salt&pepper',
output: '&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>' }, terms: ['pepper'],
{ input: '&amp;', output: 'salt&amp;<b>pepper</b>',
terms: ['a'], },
output: '&amp;<b>a</b>mp;' } {
input: 'salt&pepper',
terms: ['salt', 'pepper'],
output: '<b>salt</b>&amp;<b>pepper</b>',
},
{
input: 'salt&pepper',
terms: ['t', 'p'],
output: 'sal<b>t</b>&amp;<b>p</b>e<b>p</b><b>p</b>er',
},
{
input: 'salt&pepper',
terms: ['t', '&', 'p'],
output: 'sal<b>t</b><b>&amp;</b><b>p</b>e<b>p</b><b>p</b>er',
},
{
input: 'salt&pepper',
terms: ['e'],
output: 'salt&amp;p<b>e</b>pp<b>e</b>r',
},
{
input: 'salt&pepper',
terms: ['&a', '&am', '&amp', '&amp;'],
output: 'salt&amp;pepper',
},
{
input: '&&&&&',
terms: ['a'],
output: '&amp;&amp;&amp;&amp;&amp;',
},
{
input: '&;&;&;&;&;',
terms: ['a'],
output: '&amp;;&amp;;&amp;;&amp;;&amp;;',
},
{
input: '&;&;&;&;&;',
terms: [';'],
output: '&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>&amp;<b>;</b>',
},
{
input: '&amp;',
terms: ['a'],
output: '&amp;<b>a</b>mp;',
},
]; ];
try { try {
@ -86,7 +130,7 @@ try {
JsUnit.assertEquals(`Test ${i + 1} highlight ` + JsUnit.assertEquals(`Test ${i + 1} highlight ` +
`"${tests[i].terms}" in "${tests[i].input}"`, `"${tests[i].terms}" in "${tests[i].input}"`,
output, tests[i].output); output, tests[i].output);
let parsed = false; let parsed = false;
try { try {
@ -96,9 +140,8 @@ try {
JsUnit.assertEquals(`Test ${i + 1} is valid markup`, true, parsed); JsUnit.assertEquals(`Test ${i + 1} is valid markup`, true, parsed);
} }
} catch (e) { } catch (e) {
if (typeof(e.isJsUnitException) != 'undefined' if (typeof e.isJsUnitException != 'undefined' &&
&& e.isJsUnitException) e.isJsUnitException) {
{
if (e.comment) if (e.comment)
log(`Error in: ${e.comment}`); log(`Error in: ${e.comment}`);
} }

View File

@ -20,10 +20,10 @@ Util.insertSorted(arrayInt, 3);
Assertions.assertArrayEquals('second test', [1, 2, 3, 3, 4, 5, 6], arrayInt); Assertions.assertArrayEquals('second test', [1, 2, 3, 3, 4, 5, 6], arrayInt);
let obj1 = { a: 1 }; let obj1 = {a: 1};
let obj2 = { a: 2, b: 0 }; let obj2 = {a: 2, b: 0};
let obj3 = { a: 2, b: 1 }; let obj3 = {a: 2, b: 1};
let obj4 = { a: 3 }; let obj4 = {a: 3};
let arrayObj = [obj1, obj3, obj4]; let arrayObj = [obj1, obj3, obj4];

View File

@ -10,90 +10,146 @@ Environment.init();
const JsParse = imports.misc.jsParse; const JsParse = imports.misc.jsParse;
const HARNESS_COMMAND_HEADER = "let imports = obj;" + const HARNESS_COMMAND_HEADER = 'let imports = obj;' +
"let global = obj;" + 'let global = obj;' +
"let Main = obj;" + 'let Main = obj;' +
"let foo = obj;" + 'let foo = obj;' +
"let r = obj;"; 'let r = obj;';
const testsFindMatchingQuote = [ const testsFindMatchingQuote = [
{ input: '"double quotes"', {
output: 0 }, input: '"double quotes"',
{ input: '\'single quotes\'', output: 0,
output: 0 }, },
{ input: 'some unquoted "some quoted"', {
output: 14 }, input: '\'single quotes\'',
{ input: '"mixed \' quotes\'"', output: 0,
output: 0 }, },
{ input: '"escaped \\" quote"', {
output: 0 } input: 'some unquoted "some quoted"',
output: 14,
},
{
input: '"mixed \' quotes\'"',
output: 0,
},
{
input: '"escaped \\" quote"',
output: 0,
},
]; ];
const testsFindMatchingSlash = [ const testsFindMatchingSlash = [
{ input: '/slash/', {
output: 0 }, input: '/slash/',
{ input: '/slash " with $ funny ^\' stuff/', output: 0,
output: 0 }, },
{ input: 'some unslashed /some slashed/', {
output: 15 }, input: '/slash " with $ funny ^\' stuff/',
{ input: '/escaped \\/ slash/', output: 0,
output: 0 } },
{
input: 'some unslashed /some slashed/',
output: 15,
},
{
input: '/escaped \\/ slash/',
output: 0,
},
]; ];
const testsFindMatchingBrace = [ const testsFindMatchingBrace = [
{ input: '[square brace]', {
output: 0 }, input: '[square brace]',
{ input: '(round brace)', output: 0,
output: 0 }, },
{ input: '([()][nesting!])', {
output: 0 }, input: '(round brace)',
{ input: '[we have "quoted [" braces]', output: 0,
output: 0 }, },
{ input: '[we have /regex [/ braces]', {
output: 0 }, input: '([()][nesting!])',
{ input: '([[])[] mismatched braces ]', output: 0,
output: 1 } },
{
input: '[we have "quoted [" braces]',
output: 0,
},
{
input: '[we have /regex [/ braces]',
output: 0,
},
{
input: '([[])[] mismatched braces ]',
output: 1,
},
]; ];
const testsGetExpressionOffset = [ const testsGetExpressionOffset = [
{ input: 'abc.123', {
output: 0 }, input: 'abc.123',
{ input: 'foo().bar', output: 0,
output: 0 }, },
{ input: 'foo(bar', {
output: 4 }, input: 'foo().bar',
{ input: 'foo[abc.match(/"/)]', output: 0,
output: 0 } },
{
input: 'foo(bar',
output: 4,
},
{
input: 'foo[abc.match(/"/)]',
output: 0,
},
]; ];
const testsGetDeclaredConstants = [ const testsGetDeclaredConstants = [
{ input: 'const foo = X; const bar = Y;', {
output: ['foo', 'bar'] }, input: 'const foo = X; const bar = Y;',
{ input: 'const foo=X; const bar=Y', output: ['foo', 'bar'],
output: ['foo', 'bar'] } },
{
input: 'const foo=X; const bar=Y',
output: ['foo', 'bar'],
},
]; ];
const testsIsUnsafeExpression = [ const testsIsUnsafeExpression = [
{ input: 'foo.bar', {
output: false }, input: 'foo.bar',
{ input: 'foo[\'bar\']', output: false,
output: false }, },
{ input: 'foo["a=b=c".match(/=/)', {
output: false }, input: 'foo[\'bar\']',
{ input: 'foo[1==2]', output: false,
output: false }, },
{ input: '(x=4)', {
output: true }, input: 'foo["a=b=c".match(/=/)',
{ input: '(x = 4)', output: false,
output: true }, },
{ input: '(x;y)', {
output: true } input: 'foo[1==2]',
output: false,
},
{
input: '(x=4)',
output: true,
},
{
input: '(x = 4)',
output: true,
},
{
input: '(x;y)',
output: true,
},
]; ];
const testsModifyScope = [ const testsModifyScope = [
"foo['a", "foo['a",
"foo()['b'", "foo()['b'",
"obj.foo()('a', 1, 2, 'b')().", "obj.foo()('a', 1, 2, 'b')().",
"foo.[.", 'foo.[.',
"foo]]]()))].", 'foo]]]()))].',
"123'ab\"", "123'ab\"",
"Main.foo.bar = 3; bar.", 'Main.foo.bar = 3; bar.',
"(Main.foo = 3).", '(Main.foo = 3).',
"Main[Main.foo+=-1]." 'Main[Main.foo+=-1].',
]; ];
// //
@ -104,48 +160,48 @@ for (let i = 0; i < testsFindMatchingQuote.length; i++) {
let text = testsFindMatchingQuote[i].input; let text = testsFindMatchingQuote[i].input;
let match = JsParse.findMatchingQuote(text, text.length - 1); let match = JsParse.findMatchingQuote(text, text.length - 1);
JsUnit.assertEquals('Test testsFindMatchingQuote ' + i, JsUnit.assertEquals(`Test testsFindMatchingQuote ${i}`,
match, testsFindMatchingQuote[i].output); match, testsFindMatchingQuote[i].output);
} }
for (let i = 0; i < testsFindMatchingSlash.length; i++) { for (let i = 0; i < testsFindMatchingSlash.length; i++) {
let text = testsFindMatchingSlash[i].input; let text = testsFindMatchingSlash[i].input;
let match = JsParse.findMatchingSlash(text, text.length - 1); let match = JsParse.findMatchingSlash(text, text.length - 1);
JsUnit.assertEquals('Test testsFindMatchingSlash ' + i, JsUnit.assertEquals(`Test testsFindMatchingSlash ${i}`,
match, testsFindMatchingSlash[i].output); match, testsFindMatchingSlash[i].output);
} }
for (let i = 0; i < testsFindMatchingBrace.length; i++) { for (let i = 0; i < testsFindMatchingBrace.length; i++) {
let text = testsFindMatchingBrace[i].input; let text = testsFindMatchingBrace[i].input;
let match = JsParse.findMatchingBrace(text, text.length - 1); let match = JsParse.findMatchingBrace(text, text.length - 1);
JsUnit.assertEquals('Test testsFindMatchingBrace ' + i, JsUnit.assertEquals(`Test testsFindMatchingBrace ${i}`,
match, testsFindMatchingBrace[i].output); match, testsFindMatchingBrace[i].output);
} }
for (let i = 0; i < testsGetExpressionOffset.length; i++) { for (let i = 0; i < testsGetExpressionOffset.length; i++) {
let text = testsGetExpressionOffset[i].input; let text = testsGetExpressionOffset[i].input;
let match = JsParse.getExpressionOffset(text, text.length - 1); let match = JsParse.getExpressionOffset(text, text.length - 1);
JsUnit.assertEquals('Test testsGetExpressionOffset ' + i, JsUnit.assertEquals(`Test testsGetExpressionOffset ${i}`,
match, testsGetExpressionOffset[i].output); match, testsGetExpressionOffset[i].output);
} }
for (let i = 0; i < testsGetDeclaredConstants.length; i++) { for (let i = 0; i < testsGetDeclaredConstants.length; i++) {
let text = testsGetDeclaredConstants[i].input; let text = testsGetDeclaredConstants[i].input;
let match = JsParse.getDeclaredConstants(text); let match = JsParse.getDeclaredConstants(text);
Assertions.assertArrayEquals('Test testsGetDeclaredConstants ' + i, Assertions.assertArrayEquals(`Test testsGetDeclaredConstants ${i}`,
match, testsGetDeclaredConstants[i].output); match, testsGetDeclaredConstants[i].output);
} }
for (let i = 0; i < testsIsUnsafeExpression.length; i++) { for (let i = 0; i < testsIsUnsafeExpression.length; i++) {
let text = testsIsUnsafeExpression[i].input; let text = testsIsUnsafeExpression[i].input;
let unsafe = JsParse.isUnsafeExpression(text); let unsafe = JsParse.isUnsafeExpression(text);
JsUnit.assertEquals('Test testsIsUnsafeExpression ' + i, JsUnit.assertEquals(`Test testsIsUnsafeExpression ${i}`,
unsafe, testsIsUnsafeExpression[i].output); unsafe, testsIsUnsafeExpression[i].output);
} }
// //
@ -165,19 +221,17 @@ for (let i = 0; i < testsModifyScope.length; i++) {
let matches = text.match(/(.*)\.(.*)/); let matches = text.match(/(.*)\.(.*)/);
if (matches) { if (matches) {
let [expr, base, attrHead] = matches; let [, base] = matches;
if (!JsParse.isUnsafeExpression(base)) { if (!JsParse.isUnsafeExpression(base)) {
with (obj) { try {
try { eval(HARNESS_COMMAND_HEADER + base);
eval(HARNESS_COMMAND_HEADER + base); } catch (e) {
} catch (e) { JsUnit.assertNotEquals(`Code '${base}' is valid code`, e.constructor, SyntaxError);
JsUnit.assertNotEquals("Code '" + base + "' is valid code", e.constructor, SyntaxError);
}
} }
} }
} }
} }
let propertyNames = Object.getOwnPropertyNames(obj); let propertyNames = Object.getOwnPropertyNames(obj);
JsUnit.assertEquals("The context '" + JSON.stringify(obj) + "' was not modified", propertyNames.length, 0); JsUnit.assertEquals(`The context '${JSON.stringify(obj)}' was not modified`, propertyNames.length, 0);
} }

View File

@ -8,12 +8,17 @@ const Pango = imports.gi.Pango;
const Environment = imports.ui.environment; const Environment = imports.ui.environment;
Environment.init(); Environment.init();
const Main = imports.ui.main; // unused, but needed to break dependency loop imports.ui.main; // eslint-disable-line no-unused-expressions
const MessageList = imports.ui.messageList; const MessageList = imports.ui.messageList;
// Assert that @input, assumed to be markup, gets "fixed" to @output, /**
// which is valid markup. If @output is null, @input is expected to * Assert that `input`, assumed to be markup, gets "fixed" to `output`,
// convert to itself * which is valid markup. If `output` is null, `input` is expected to
* convert to itself
*
* @param {string} input the input
* @param {string} output the output
*/
function assertConverts(input, output) { function assertConverts(input, output) {
if (!output) if (!output)
output = input; output = input;
@ -28,8 +33,14 @@ function assertConverts(input, output) {
JsUnit.assertEquals(true, parsed); JsUnit.assertEquals(true, parsed);
} }
// Assert that @input, assumed to be plain text, gets escaped to @output,
// which is valid markup. /**
* Assert that `input`, assumed to be plain text, gets escaped to `output`,
* which is valid markup.
*
* @param {string} input the input
* @param {string} output the output
*/
function assertEscapes(input, output) { function assertEscapes(input, output) {
let fixed = MessageList._fixMarkup(input, false); let fixed = MessageList._fixMarkup(input, false);
JsUnit.assertEquals(output, fixed); JsUnit.assertEquals(output, fixed);

View File

@ -1,6 +1,13 @@
const JsUnit = imports.jsUnit; const JsUnit = imports.jsUnit;
const Params = imports.misc.params; const Params = imports.misc.params;
/**
* Asserts that two "param" objects have the same properties
* with the same values.
*
* @param {object} params the parsed params
* @param {object} expected the expected params
*/
function assertParamsEqual(params, expected) { function assertParamsEqual(params, expected) {
for (let p in params) { for (let p in params) {
JsUnit.assertTrue(p in expected); JsUnit.assertTrue(p in expected);
@ -11,7 +18,7 @@ function assertParamsEqual(params, expected) {
let defaults = { let defaults = {
foo: 'This is a test', foo: 'This is a test',
bar: null, bar: null,
baz: 42 baz: 42,
}; };
assertParamsEqual( assertParamsEqual(
@ -19,14 +26,14 @@ assertParamsEqual(
defaults); defaults);
assertParamsEqual( assertParamsEqual(
Params.parse({ bar: 23 }, defaults), Params.parse({bar: 23}, defaults),
{ foo: 'This is a test', bar: 23, baz: 42 }); {foo: 'This is a test', bar: 23, baz: 42});
JsUnit.assertRaises( JsUnit.assertRaises(
() => { () => {
Params.parse({ extraArg: 'quz' }, defaults); Params.parse({extraArg: 'quz'}, defaults);
}); });
assertParamsEqual( assertParamsEqual(
Params.parse({ extraArg: 'quz' }, defaults, true), Params.parse({extraArg: 'quz'}, defaults, true),
{ foo: 'This is a test', bar: null, baz: 42, extraArg: 'quz' }); {foo: 'This is a test', bar: null, baz: 42, extraArg: 'quz'});

View File

@ -8,17 +8,17 @@ const JsUnit = imports.jsUnit;
const Signals = imports.misc.signals; const Signals = imports.misc.signals;
const Environment = imports.ui.environment; const Environment = imports.ui.environment;
const { TransientSignalHolder, registerDestroyableType } = imports.misc.signalTracker; const {TransientSignalHolder, registerDestroyableType} = imports.misc.signalTracker;
Environment.init(); Environment.init();
const Destroyable = GObject.registerClass({ const Destroyable = GObject.registerClass({
Signals: { 'destroy': {} }, Signals: {'destroy': {}},
}, class Destroyable extends GObject.Object {}); }, class Destroyable extends GObject.Object {});
registerDestroyableType(Destroyable); registerDestroyableType(Destroyable);
const GObjectEmitter = GObject.registerClass({ const GObjectEmitter = GObject.registerClass({
Signals: { 'signal': {} }, Signals: {'signal': {}},
}, class GObjectEmitter extends Destroyable {}); }, class GObjectEmitter extends Destroyable {});
const emitter1 = new Signals.EventEmitter(); const emitter1 = new Signals.EventEmitter();
@ -27,6 +27,7 @@ const emitter2 = new GObjectEmitter();
const tracked1 = new Destroyable(); const tracked1 = new Destroyable();
const tracked2 = {}; const tracked2 = {};
let transientHolder;
let count = 0; let count = 0;
const handler = () => count++; const handler = () => count++;

View File

@ -10,68 +10,114 @@ Environment.init();
const Util = imports.misc.util; const Util = imports.misc.util;
const tests = [ const tests = [
{ input: 'This is a test', {
output: [] }, input: 'This is a test',
{ input: 'This is http://www.gnome.org a test', output: [],
output: [ { url: 'http://www.gnome.org', pos: 8 } ] }, },
{ input: 'This is http://www.gnome.org', {
output: [ { url: 'http://www.gnome.org', pos: 8 } ] }, input: 'This is http://www.gnome.org a test',
{ input: 'http://www.gnome.org a test', output: [{url: 'http://www.gnome.org', pos: 8}],
output: [ { url: 'http://www.gnome.org', pos: 0 } ] }, },
{ input: 'http://www.gnome.org', {
output: [ { url: 'http://www.gnome.org', pos: 0 } ] }, input: 'This is http://www.gnome.org',
{ input: 'Go to http://www.gnome.org.', output: [{url: 'http://www.gnome.org', pos: 8}],
output: [ { url: 'http://www.gnome.org', pos: 6 } ] }, },
{ input: 'Go to http://www.gnome.org/.', {
output: [ { url: 'http://www.gnome.org/', pos: 6 } ] }, input: 'http://www.gnome.org a test',
{ input: '(Go to http://www.gnome.org!)', output: [{url: 'http://www.gnome.org', pos: 0}],
output: [ { url: 'http://www.gnome.org', pos: 7 } ] }, },
{ input: 'Use GNOME (http://www.gnome.org).', {
output: [ { url: 'http://www.gnome.org', pos: 11 } ] }, input: 'http://www.gnome.org',
{ input: 'This is a http://www.gnome.org/path test.', output: [{url: 'http://www.gnome.org', pos: 0}],
output: [ { url: 'http://www.gnome.org/path', pos: 10 } ] }, },
{ input: 'This is a www.gnome.org scheme-less test.', {
output: [ { url: 'www.gnome.org', pos: 10 } ] }, input: 'Go to http://www.gnome.org.',
{ input: 'This is a www.gnome.org/scheme-less test.', output: [{url: 'http://www.gnome.org', pos: 6}],
output: [ { url: 'www.gnome.org/scheme-less', pos: 10 } ] }, },
{ input: 'This is a http://www.gnome.org:99/port test.', {
output: [ { url: 'http://www.gnome.org:99/port', pos: 10 } ] }, input: 'Go to http://www.gnome.org/.',
{ input: 'This is an ftp://www.gnome.org/ test.', output: [{url: 'http://www.gnome.org/', pos: 6}],
output: [ { url: 'ftp://www.gnome.org/', pos: 11 } ] }, },
{ input: 'https://www.gnome.org/(some_url,_with_very_unusual_characters)', {
output: [ { url: 'https://www.gnome.org/(some_url,_with_very_unusual_characters)', pos: 0 } ] }, input: '(Go to http://www.gnome.org!)',
{ input: 'https://www.gnome.org/(some_url_with_unbalanced_parenthesis', output: [{url: 'http://www.gnome.org', pos: 7}],
output: [ { url: 'https://www.gnome.org/', pos: 0 } ] }, },
{ input: 'https://www.gnome.org/ plus trailing junk', {
output: [ { url: 'https://www.gnome.org/', pos: 0 } ] }, input: 'Use GNOME (http://www.gnome.org).',
output: [{url: 'http://www.gnome.org', pos: 11}],
},
{
input: 'This is a http://www.gnome.org/path test.',
output: [{url: 'http://www.gnome.org/path', pos: 10}],
},
{
input: 'This is a www.gnome.org scheme-less test.',
output: [{url: 'www.gnome.org', pos: 10}],
},
{
input: 'This is a www.gnome.org/scheme-less test.',
output: [{url: 'www.gnome.org/scheme-less', pos: 10}],
},
{
input: 'This is a http://www.gnome.org:99/port test.',
output: [{url: 'http://www.gnome.org:99/port', pos: 10}],
},
{
input: 'This is an ftp://www.gnome.org/ test.',
output: [{url: 'ftp://www.gnome.org/', pos: 11}],
},
{
input: 'https://www.gnome.org/(some_url,_with_very_unusual_characters)',
output: [{url: 'https://www.gnome.org/(some_url,_with_very_unusual_characters)', pos: 0}],
},
{
input: 'https://www.gnome.org/(some_url_with_unbalanced_parenthesis',
output: [{url: 'https://www.gnome.org/', pos: 0}],
},
{
input: 'https://www.gnome.org/ plus trailing junk',
output: [{url: 'https://www.gnome.org/', pos: 0}],
},
{ input: 'Visit http://www.gnome.org/ and http://developer.gnome.org', {
output: [ { url: 'http://www.gnome.org/', pos: 6 }, input: 'Visit http://www.gnome.org/ and http://developer.gnome.org',
{ url: 'http://developer.gnome.org', pos: 32 } ] }, output: [{url: 'http://www.gnome.org/', pos: 6},
{url: 'http://developer.gnome.org', pos: 32}],
},
{ input: 'This is not.a.domain test.', {
output: [ ] }, input: 'This is not.a.domain test.',
{ input: 'This is not:a.url test.', output: [],
output: [ ] }, },
{ input: 'This is not:/a.url/ test.', {
output: [ ] }, input: 'This is not:a.url test.',
{ input: 'This is not:/a.url/ test.', output: [],
output: [ ] }, },
{ input: 'This is not@a.url/ test.', {
output: [ ] }, input: 'This is not:/a.url/ test.',
{ input: 'This is surely@not.a/url test.', output: [],
output: [ ] } },
{
input: 'This is not:/a.url/ test.',
output: [],
},
{
input: 'This is not@a.url/ test.',
output: [],
},
{
input: 'This is surely@not.a/url test.',
output: [],
},
]; ];
for (let i = 0; i < tests.length; i++) { for (let i = 0; i < tests.length; i++) {
let match = Util.findUrls(tests[i].input); let match = Util.findUrls(tests[i].input);
JsUnit.assertEquals('Test ' + i + ' match length', JsUnit.assertEquals(`Test ${i} match length`,
match.length, tests[i].output.length); match.length, tests[i].output.length);
for (let j = 0; j < match.length; j++) { for (let j = 0; j < match.length; j++) {
JsUnit.assertEquals('Test ' + i + ', match ' + j + ' url', JsUnit.assertEquals(`Test ${i}, match ${j} url`, match[j].url, tests[i].output[j].url);
match[j].url, tests[i].output[j].url); JsUnit.assertEquals(`Test ${i}, match ${j} position`, match[j].pos, tests[i].output[j].pos);
JsUnit.assertEquals('Test ' + i + ', match ' + j + ' position',
match[j].pos, tests[i].output[j].pos);
} }
} }

View File

@ -10,43 +10,65 @@ Environment.init();
const Util = imports.misc.util; const Util = imports.misc.util;
const tests = [ const tests = [
{ v1: '40', {
v2: '40', v1: '40',
res: 0 }, v2: '40',
{ v1: '40', res: 0,
v2: '42', },
res: -1 }, {
{ v1: '42', v1: '40',
v2: '40', v2: '42',
res: 1 }, res: -1,
{ v1: '3.38.0', },
v2: '40', {
res: -1 }, v1: '42',
{ v1: '40', v2: '40',
v2: '3.38.0', res: 1,
res: 1 }, },
{ v1: '40', {
v2: '3.38.0', v1: '3.38.0',
res: 1 }, v2: '40',
{ v1: '40.alpha.1.1', res: -1,
v2: '40', },
res: -1 }, {
{ v1: '40', v1: '40',
v2: '40.alpha.1.1', v2: '3.38.0',
res: 1 }, res: 1,
{ v1: '40.beta', },
v2: '40', {
res: -1 }, v1: '40',
{ v1: '40.1', v2: '3.38.0',
v2: '40', res: 1,
res: 1 }, },
{ v1: '', {
v2: '40.alpha', v1: '40.alpha.1.1',
res: -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++) { for (let i = 0; i < tests.length; i++) {
name = 'Test #' + i + ' v1: ' + tests[i].v1 + ', v2: ' + tests[i].v2; const name = `Test #${i} v1: ${tests[i].v1}, v2: ${tests[i].v2}`;
print(name); print(name);
JsUnit.assertEquals(name, Util.GNOMEversionCompare (tests[i].v1, tests[i].v2), tests[i].res); JsUnit.assertEquals(name, Util.GNOMEversionCompare(tests[i].v1, tests[i].v2), tests[i].res);
} }