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/**
- subprojects/extensions-app/**
- subprojects/extensions-tool/**
- tests/shell/**
- tests/**
parserOptions:
sourceType: module

View File

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

View File

@ -10,90 +10,146 @@ Environment.init();
const JsParse = imports.misc.jsParse;
const HARNESS_COMMAND_HEADER = "let imports = obj;" +
"let global = obj;" +
"let Main = obj;" +
"let foo = obj;" +
"let r = obj;";
const HARNESS_COMMAND_HEADER = 'let imports = obj;' +
'let global = obj;' +
'let Main = obj;' +
'let foo = obj;' +
'let r = obj;';
const testsFindMatchingQuote = [
{ input: '"double quotes"',
output: 0 },
{ input: '\'single quotes\'',
output: 0 },
{ input: 'some unquoted "some quoted"',
output: 14 },
{ input: '"mixed \' quotes\'"',
output: 0 },
{ input: '"escaped \\" quote"',
output: 0 }
{
input: '"double quotes"',
output: 0,
},
{
input: '\'single quotes\'',
output: 0,
},
{
input: 'some unquoted "some quoted"',
output: 14,
},
{
input: '"mixed \' quotes\'"',
output: 0,
},
{
input: '"escaped \\" quote"',
output: 0,
},
];
const testsFindMatchingSlash = [
{ input: '/slash/',
output: 0 },
{ input: '/slash " with $ funny ^\' stuff/',
output: 0 },
{ input: 'some unslashed /some slashed/',
output: 15 },
{ input: '/escaped \\/ slash/',
output: 0 }
{
input: '/slash/',
output: 0,
},
{
input: '/slash " with $ funny ^\' stuff/',
output: 0,
},
{
input: 'some unslashed /some slashed/',
output: 15,
},
{
input: '/escaped \\/ slash/',
output: 0,
},
];
const testsFindMatchingBrace = [
{ input: '[square brace]',
output: 0 },
{ input: '(round brace)',
output: 0 },
{ input: '([()][nesting!])',
output: 0 },
{ input: '[we have "quoted [" braces]',
output: 0 },
{ input: '[we have /regex [/ braces]',
output: 0 },
{ input: '([[])[] mismatched braces ]',
output: 1 }
{
input: '[square brace]',
output: 0,
},
{
input: '(round brace)',
output: 0,
},
{
input: '([()][nesting!])',
output: 0,
},
{
input: '[we have "quoted [" braces]',
output: 0,
},
{
input: '[we have /regex [/ braces]',
output: 0,
},
{
input: '([[])[] mismatched braces ]',
output: 1,
},
];
const testsGetExpressionOffset = [
{ input: 'abc.123',
output: 0 },
{ input: 'foo().bar',
output: 0 },
{ input: 'foo(bar',
output: 4 },
{ input: 'foo[abc.match(/"/)]',
output: 0 }
{
input: 'abc.123',
output: 0,
},
{
input: 'foo().bar',
output: 0,
},
{
input: 'foo(bar',
output: 4,
},
{
input: 'foo[abc.match(/"/)]',
output: 0,
},
];
const testsGetDeclaredConstants = [
{ input: 'const foo = X; const bar = Y;',
output: ['foo', 'bar'] },
{ input: 'const foo=X; const bar=Y',
output: ['foo', 'bar'] }
{
input: 'const foo = X; const bar = Y;',
output: ['foo', 'bar'],
},
{
input: 'const foo=X; const bar=Y',
output: ['foo', 'bar'],
},
];
const testsIsUnsafeExpression = [
{ input: 'foo.bar',
output: false },
{ input: 'foo[\'bar\']',
output: false },
{ input: 'foo["a=b=c".match(/=/)',
output: false },
{ input: 'foo[1==2]',
output: false },
{ input: '(x=4)',
output: true },
{ input: '(x = 4)',
output: true },
{ input: '(x;y)',
output: true }
{
input: 'foo.bar',
output: false,
},
{
input: 'foo[\'bar\']',
output: false,
},
{
input: 'foo["a=b=c".match(/=/)',
output: false,
},
{
input: 'foo[1==2]',
output: false,
},
{
input: '(x=4)',
output: true,
},
{
input: '(x = 4)',
output: true,
},
{
input: '(x;y)',
output: true,
},
];
const testsModifyScope = [
"foo['a",
"foo()['b'",
"obj.foo()('a', 1, 2, 'b')().",
"foo.[.",
"foo]]]()))].",
'foo.[.',
'foo]]]()))].',
"123'ab\"",
"Main.foo.bar = 3; bar.",
"(Main.foo = 3).",
"Main[Main.foo+=-1]."
'Main.foo.bar = 3; bar.',
'(Main.foo = 3).',
'Main[Main.foo+=-1].',
];
//
@ -104,7 +160,7 @@ for (let i = 0; i < testsFindMatchingQuote.length; i++) {
let text = testsFindMatchingQuote[i].input;
let match = JsParse.findMatchingQuote(text, text.length - 1);
JsUnit.assertEquals('Test testsFindMatchingQuote ' + i,
JsUnit.assertEquals(`Test testsFindMatchingQuote ${i}`,
match, testsFindMatchingQuote[i].output);
}
@ -112,7 +168,7 @@ for (let i = 0; i < testsFindMatchingSlash.length; i++) {
let text = testsFindMatchingSlash[i].input;
let match = JsParse.findMatchingSlash(text, text.length - 1);
JsUnit.assertEquals('Test testsFindMatchingSlash ' + i,
JsUnit.assertEquals(`Test testsFindMatchingSlash ${i}`,
match, testsFindMatchingSlash[i].output);
}
@ -120,7 +176,7 @@ for (let i = 0; i < testsFindMatchingBrace.length; i++) {
let text = testsFindMatchingBrace[i].input;
let match = JsParse.findMatchingBrace(text, text.length - 1);
JsUnit.assertEquals('Test testsFindMatchingBrace ' + i,
JsUnit.assertEquals(`Test testsFindMatchingBrace ${i}`,
match, testsFindMatchingBrace[i].output);
}
@ -128,7 +184,7 @@ for (let i = 0; i < testsGetExpressionOffset.length; i++) {
let text = testsGetExpressionOffset[i].input;
let match = JsParse.getExpressionOffset(text, text.length - 1);
JsUnit.assertEquals('Test testsGetExpressionOffset ' + i,
JsUnit.assertEquals(`Test testsGetExpressionOffset ${i}`,
match, testsGetExpressionOffset[i].output);
}
@ -136,7 +192,7 @@ for (let i = 0; i < testsGetDeclaredConstants.length; i++) {
let text = testsGetDeclaredConstants[i].input;
let match = JsParse.getDeclaredConstants(text);
Assertions.assertArrayEquals('Test testsGetDeclaredConstants ' + i,
Assertions.assertArrayEquals(`Test testsGetDeclaredConstants ${i}`,
match, testsGetDeclaredConstants[i].output);
}
@ -144,7 +200,7 @@ for (let i = 0; i < testsIsUnsafeExpression.length; i++) {
let text = testsIsUnsafeExpression[i].input;
let unsafe = JsParse.isUnsafeExpression(text);
JsUnit.assertEquals('Test testsIsUnsafeExpression ' + i,
JsUnit.assertEquals(`Test testsIsUnsafeExpression ${i}`,
unsafe, testsIsUnsafeExpression[i].output);
}
@ -165,19 +221,17 @@ for (let i = 0; i < testsModifyScope.length; i++) {
let matches = text.match(/(.*)\.(.*)/);
if (matches) {
let [expr, base, attrHead] = matches;
let [, base] = matches;
if (!JsParse.isUnsafeExpression(base)) {
with (obj) {
try {
eval(HARNESS_COMMAND_HEADER + base);
} 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);
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;
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;
// Assert that @input, assumed to be markup, gets "fixed" to @output,
// which is valid markup. If @output is null, @input is expected to
// convert to itself
/**
* Assert that `input`, assumed to be markup, gets "fixed" to `output`,
* 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) {
if (!output)
output = input;
@ -28,8 +33,14 @@ function assertConverts(input, output) {
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) {
let fixed = MessageList._fixMarkup(input, false);
JsUnit.assertEquals(output, fixed);

View File

@ -1,6 +1,13 @@
const JsUnit = imports.jsUnit;
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) {
for (let p in params) {
JsUnit.assertTrue(p in expected);
@ -11,7 +18,7 @@ function assertParamsEqual(params, expected) {
let defaults = {
foo: 'This is a test',
bar: null,
baz: 42
baz: 42,
};
assertParamsEqual(

View File

@ -27,6 +27,7 @@ const emitter2 = new GObjectEmitter();
const tracked1 = new Destroyable();
const tracked2 = {};
let transientHolder;
let count = 0;
const handler = () => count++;

View File

@ -10,68 +10,114 @@ Environment.init();
const Util = imports.misc.util;
const tests = [
{ input: 'This is a test',
output: [] },
{ input: 'This is http://www.gnome.org a test',
output: [ { url: 'http://www.gnome.org', pos: 8 } ] },
{ input: 'This is http://www.gnome.org',
output: [ { url: 'http://www.gnome.org', pos: 8 } ] },
{ input: 'http://www.gnome.org a test',
output: [ { url: 'http://www.gnome.org', pos: 0 } ] },
{ input: 'http://www.gnome.org',
output: [ { url: 'http://www.gnome.org', pos: 0 } ] },
{ input: 'Go to http://www.gnome.org.',
output: [ { url: 'http://www.gnome.org', pos: 6 } ] },
{ input: 'Go to http://www.gnome.org/.',
output: [ { url: 'http://www.gnome.org/', pos: 6 } ] },
{ input: '(Go to http://www.gnome.org!)',
output: [ { url: 'http://www.gnome.org', pos: 7 } ] },
{ 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: 'This is a test',
output: [],
},
{
input: 'This is http://www.gnome.org a test',
output: [{url: 'http://www.gnome.org', pos: 8}],
},
{
input: 'This is http://www.gnome.org',
output: [{url: 'http://www.gnome.org', pos: 8}],
},
{
input: 'http://www.gnome.org a test',
output: [{url: 'http://www.gnome.org', pos: 0}],
},
{
input: 'http://www.gnome.org',
output: [{url: 'http://www.gnome.org', pos: 0}],
},
{
input: 'Go to http://www.gnome.org.',
output: [{url: 'http://www.gnome.org', pos: 6}],
},
{
input: 'Go to http://www.gnome.org/.',
output: [{url: 'http://www.gnome.org/', pos: 6}],
},
{
input: '(Go to http://www.gnome.org!)',
output: [{url: 'http://www.gnome.org', pos: 7}],
},
{
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',
{
input: 'Visit http://www.gnome.org/ and http://developer.gnome.org',
output: [{url: 'http://www.gnome.org/', pos: 6},
{ url: 'http://developer.gnome.org', pos: 32 } ] },
{url: 'http://developer.gnome.org', pos: 32}],
},
{ input: 'This is not.a.domain test.',
output: [ ] },
{ input: 'This is not:a.url test.',
output: [ ] },
{ input: 'This is not:/a.url/ test.',
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: [ ] }
{
input: 'This is not.a.domain test.',
output: [],
},
{
input: 'This is not:a.url test.',
output: [],
},
{
input: 'This is not:/a.url/ test.',
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++) {
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);
for (let j = 0; j < match.length; j++) {
JsUnit.assertEquals('Test ' + i + ', match ' + 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} url`, match[j].url, tests[i].output[j].url);
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 tests = [
{ v1: '40',
{
v1: '40',
v2: '40',
res: 0 },
{ v1: '40',
res: 0,
},
{
v1: '40',
v2: '42',
res: -1 },
{ v1: '42',
res: -1,
},
{
v1: '42',
v2: '40',
res: 1 },
{ v1: '3.38.0',
res: 1,
},
{
v1: '3.38.0',
v2: '40',
res: -1 },
{ v1: '40',
res: -1,
},
{
v1: '40',
v2: '3.38.0',
res: 1 },
{ v1: '40',
res: 1,
},
{
v1: '40',
v2: '3.38.0',
res: 1 },
{ v1: '40.alpha.1.1',
res: 1,
},
{
v1: '40.alpha.1.1',
v2: '40',
res: -1 },
{ v1: '40',
res: -1,
},
{
v1: '40',
v2: '40.alpha.1.1',
res: 1 },
{ v1: '40.beta',
res: 1,
},
{
v1: '40.beta',
v2: '40',
res: -1 },
{ v1: '40.1',
res: -1,
},
{
v1: '40.1',
v2: '40',
res: 1 },
{ v1: '',
res: 1,
},
{
v1: '',
v2: '40.alpha',
res: -1 },
res: -1,
},
];
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);
JsUnit.assertEquals(name, Util.GNOMEversionCompare(tests[i].v1, tests[i].v2), tests[i].res);
}