tests: Port insertSorted() test to jasmine
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3164>
This commit is contained in:
parent
d62146bc19
commit
40750f5849
@ -22,6 +22,7 @@ unit_testenv.append('GI_TYPELIB_PATH', st_typelib_path, separator: ':')
|
|||||||
unit_tests = [
|
unit_tests = [
|
||||||
'highlighter',
|
'highlighter',
|
||||||
'injectionManager',
|
'injectionManager',
|
||||||
|
'insertSorted',
|
||||||
]
|
]
|
||||||
|
|
||||||
foreach test : unit_tests
|
foreach test : unit_tests
|
||||||
@ -40,7 +41,6 @@ foreach test : unit_tests
|
|||||||
endforeach
|
endforeach
|
||||||
|
|
||||||
legacy_tests = [
|
legacy_tests = [
|
||||||
'insertSorted',
|
|
||||||
'jsParse',
|
'jsParse',
|
||||||
'markup',
|
'markup',
|
||||||
'params',
|
'params',
|
||||||
|
@ -6,53 +6,59 @@
|
|||||||
// we don't actually use
|
// we don't actually use
|
||||||
import 'resource:///org/gnome/shell/ui/environment.js';
|
import 'resource:///org/gnome/shell/ui/environment.js';
|
||||||
|
|
||||||
import * as Assertions from '../common/assertions.js';
|
import {insertSorted} from 'resource:///org/gnome/shell/misc/util.js';
|
||||||
|
|
||||||
import * as Util from 'resource:///org/gnome/shell/misc/util.js';
|
describe('insertSorted()', () => {
|
||||||
|
it('uses integer sorting by default', () => {
|
||||||
|
const arrayInt = [1, 2, 3, 5, 6];
|
||||||
|
insertSorted(arrayInt, 4);
|
||||||
|
expect(arrayInt).toEqual([1, 2, 3, 4, 5, 6]);
|
||||||
|
});
|
||||||
|
|
||||||
let arrayInt = [1, 2, 3, 5, 6];
|
it('inserts elements with a custom compare function', () => {
|
||||||
Util.insertSorted(arrayInt, 4, (one, two) => one - two);
|
const arrayInt = [6, 5, 3, 2, 1];
|
||||||
|
insertSorted(arrayInt, 4, (one, two) => two - one);
|
||||||
|
expect(arrayInt).toEqual([6, 5, 4, 3, 2, 1]);
|
||||||
|
});
|
||||||
|
|
||||||
Assertions.assertArrayEquals('first test', [1, 2, 3, 4, 5, 6], arrayInt);
|
it('inserts before first equal match', () => {
|
||||||
|
const obj1 = {a: 1};
|
||||||
|
const obj2 = {a: 2, b: 0};
|
||||||
|
const obj3 = {a: 2, b: 1};
|
||||||
|
const obj4 = {a: 3};
|
||||||
|
|
||||||
// no comparator, integer sorting is implied
|
const arrayObj = [obj1, obj3, obj4];
|
||||||
Util.insertSorted(arrayInt, 3);
|
insertSorted(arrayObj, obj2, (one, two) => one.a - two.a);
|
||||||
|
expect(arrayObj).toEqual([obj1, obj2, obj3, obj4]);
|
||||||
|
});
|
||||||
|
|
||||||
Assertions.assertArrayEquals('second test', [1, 2, 3, 3, 4, 5, 6], arrayInt);
|
it('does not call compare func when array was empty', () => {
|
||||||
|
const cmp = jasmine.createSpy();
|
||||||
|
const empty = [];
|
||||||
|
insertSorted(empty, 3, cmp);
|
||||||
|
expect(cmp).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
let obj1 = {a: 1};
|
const checkedCmp = (one, two) => {
|
||||||
let obj2 = {a: 2, b: 0};
|
if (typeof one !== 'number' || typeof two !== 'number')
|
||||||
let obj3 = {a: 2, b: 1};
|
throw new TypeError('Invalid type passed to checkedCmp');
|
||||||
let obj4 = {a: 3};
|
|
||||||
|
|
||||||
let arrayObj = [obj1, obj3, obj4];
|
return one - two;
|
||||||
|
};
|
||||||
|
|
||||||
// obj2 compares equivalent to obj3, should be
|
it('does not access past bounds when inserting at end', () => {
|
||||||
// inserted before
|
const array = [3];
|
||||||
Util.insertSorted(arrayObj, obj2, (one, two) => one.a - two.a);
|
expect(() => {
|
||||||
|
insertSorted(array, 4, checkedCmp);
|
||||||
|
insertSorted(array, 5, checkedCmp);
|
||||||
|
}).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
Assertions.assertArrayEquals('object test', [obj1, obj2, obj3, obj4], arrayObj);
|
it('does not access past bounds when inserting at beginning', () => {
|
||||||
|
const array = [4, 5];
|
||||||
const checkedCmp = (one, two) => {
|
expect(() => {
|
||||||
if (typeof one != 'number' || typeof two != 'number')
|
insertSorted(array, 1, checkedCmp);
|
||||||
throw new TypeError('Invalid type passed to checkedCmp');
|
insertSorted(array, 2, checkedCmp);
|
||||||
|
}).not.toThrow();
|
||||||
return one - two;
|
});
|
||||||
};
|
});
|
||||||
|
|
||||||
let arrayEmpty = [];
|
|
||||||
|
|
||||||
// check that no comparisons are made when
|
|
||||||
// inserting in a empty array
|
|
||||||
Util.insertSorted(arrayEmpty, 3, checkedCmp);
|
|
||||||
|
|
||||||
// Insert at the end and check that we don't
|
|
||||||
// access past it
|
|
||||||
Util.insertSorted(arrayEmpty, 4, checkedCmp);
|
|
||||||
Util.insertSorted(arrayEmpty, 5, checkedCmp);
|
|
||||||
|
|
||||||
// Some more insertions
|
|
||||||
Util.insertSorted(arrayEmpty, 2, checkedCmp);
|
|
||||||
Util.insertSorted(arrayEmpty, 1, checkedCmp);
|
|
||||||
|
|
||||||
Assertions.assertArrayEquals('checkedCmp test', [1, 2, 3, 4, 5], arrayEmpty);
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user