use jasmine
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2020-10-05 20:37:48 +02:00
parent c7ad5298af
commit 1ae21ed665
6 changed files with 1012 additions and 1500 deletions

14
jasmine.json Normal file
View File

@ -0,0 +1,14 @@
{
"spec_dir": "test",
"spec_files": [
"spec/*.js",
"spec/*.ts"
],
"helpers": [
"helpers/*.js",
"helpers/*.ts"
],
"stopSpecOnExpectationFailure": false,
"random": false
}

View File

@ -1,6 +1,6 @@
{ {
"name": "@sapphirecode/utilities", "name": "@sapphirecode/utilities",
"version": "1.8.5", "version": "1.8.6",
"main": "index.js", "main": "index.js",
"author": { "author": {
"name": "Timo Hocker", "name": "Timo Hocker",
@ -22,14 +22,17 @@
"devDependencies": { "devDependencies": {
"@sapphirecode/eslint-config": "^2.1.3", "@sapphirecode/eslint-config": "^2.1.3",
"@stryker-mutator/core": "^3.2.1", "@stryker-mutator/core": "^3.2.1",
"@stryker-mutator/jasmine-framework": "^3.3.1",
"@stryker-mutator/jasmine-runner": "^3.3.1",
"@stryker-mutator/javascript-mutator": "^3.2.1", "@stryker-mutator/javascript-mutator": "^3.2.1",
"ava": "^3.8.2", "@types/jasmine": "^3.5.14",
"eslint": "^7.0.0", "eslint": "^7.0.0",
"jasmine": "^3.6.1",
"nyc": "^15.0.1" "nyc": "^15.0.1"
}, },
"scripts": { "scripts": {
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs", "lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs",
"test": "nyc ava", "test": "nyc jasmine --config=\"jasmine.json\"",
"mutate": "stryker run", "mutate": "stryker run",
"compile": "tsc --allowJs --declaration --emitDeclarationOnly index.js" "compile": "tsc --allowJs --declaration --emitDeclarationOnly index.js"
}, },

View File

@ -17,8 +17,10 @@ module.exports = {
'clear-text', 'clear-text',
'progress' 'progress'
], ],
testRunner: 'command', testRunner: 'jasmine',
transpilers: [], testFramework: 'jasmine',
coverageAnalysis: 'all', jasmineConfigFile: 'jasmine.json',
mutate: [ 'index.js' ] transpilers: [],
coverageAnalysis: 'perTest',
mutate: [ 'index.js' ]
}; };

View File

@ -1,280 +0,0 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of utilities which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
// @ts-nocheck
'use strict';
const test = require ('ava');
const util = require ('../index');
test ('truncate_decimal', (t) => {
const trunc = util.truncate_decimal (1.23456, 2);
t.is (trunc, 1.23);
});
test ('try_parse_json should parse', (t) => {
const str = '{"test":"foo"}';
t.notThrows (() => {
const json = util.try_parse_json (str);
t.deepEqual (json, { test: 'foo' });
});
});
test ('try_parse_json should fail', (t) => {
const str = '{"test":foo"}';
t.notThrows (() => {
const json = util.try_parse_json (str);
t.is (json, null);
});
});
test ('copy object', (t) => {
const obj = { foo: 'bar', bar: { foo: 'baz' } };
const copy = util.copy_object (obj);
t.deepEqual (obj, copy);
copy.foo = 'baz';
copy.bar.foo = 'bar';
t.is (copy.foo, 'baz');
t.is (copy.bar.foo, 'bar');
t.is (obj.foo, 'bar');
t.is (obj.bar.foo, 'baz');
});
test ('run regex', (t) => {
const data = 'foobarfoo';
const regex = /foo/gu;
let count = 0;
util.run_regex (regex, data, (res) => {
t.is (res[0], 'foo');
count++;
});
t.is (count, 2);
});
test ('run non-global regex', (t) => {
const data = 'foobarfoo';
const regex = /foo/u;
let count = 0;
util.run_regex (regex, data, (res) => {
t.is (res[0], 'foo');
count++;
});
t.is (count, 1);
});
test ('run non-global regex without result', (t) => {
const data = 'foobarfoo';
const regex = /baz/u;
let count = 0;
util.run_regex (regex, data, () => {
count++;
});
t.is (count, 0);
});
test ('check isnil with undefined', (t) => {
t.is (util.is_nil (), true);
});
test ('check isnil with null', (t) => {
t.is (util.is_nil (null), true);
});
test ('check isnil with empty string', (t) => {
t.is (util.is_nil (''), false);
});
test ('check isnil with string', (t) => {
t.is (util.is_nil ('foo'), false);
});
test ('check isnil with 0', (t) => {
t.is (util.is_nil (0), false);
});
test ('check isnil with nan', (t) => {
t.is (util.is_nil (parseInt ('foo')), true);
});
test ('check isnil with int', (t) => {
t.is (util.is_nil (parseInt ('42')), false);
});
test ('recursive filter', (t) => {
const raw = [
{ name: 'include_foo' },
{
name: 'include_bar',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
},
{
name: 'baz',
children: [
{ name: 'include_foo' },
{ name: 'bar' }
]
},
{
name: 'barbaz',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
}
];
const filtered = [
{ name: 'include_foo' },
{
name: 'include_bar',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
},
{
name: 'baz',
children: [ { name: 'include_foo' } ]
}
];
const filter = {
field: 'name',
filter: /^include_.*/ui
};
const result = util.recursive_filter (raw, [ filter ]);
t.deepEqual (filtered, result);
});
test ('recursive filter multifield', (t) => {
const raw = [
{ name: 'foo', f: 'include' },
{
name: 'include_bar',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
},
{
name: 'baz',
children: [
{ name: 'include_foo' },
{ name: 'bar' }
]
},
{
name: 'barbaz',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
}
];
const filtered = [ { name: 'foo', f: 'include' } ];
const filter = {
field: [
'name',
'f'
],
filter: /foo include/ui
};
const result = util.recursive_filter (raw, [ filter ]);
t.deepEqual (filtered, result);
});
test ('recursive filter undefined multifield', (t) => {
const res = util.recursive_filter (
[ { foo: 'bar' } ],
[
{
field: [
'foo',
'bar'
],
filter: /\s/u
}
]
);
t.deepEqual (res, []);
});
test ('recursive filter with or group', (t) => {
const to_filter = [
{ name: 'foo' },
{ name: 'bar' },
{ name: 'baz' }
];
const filter = [
{
or: [
{ field: 'name', filter: /foo/u },
{ field: 'name', filter: /bar/u }
]
}
];
const res = util.recursive_filter (to_filter, filter);
t.deepEqual (res, to_filter.slice (0, 2));
});
test ('recursive filter carry field', (t) => {
const to_filter = [
{
name: 'foo',
children: [
{ name: 'bar' },
{ name: 'baz' },
{ foo: 'bar' }
]
}
];
const res = util.recursive_filter (
to_filter,
[ { field: 'name', filter: /foo bar/ui } ],
'children',
[ 'name' ]
);
t.deepEqual (res, [
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});
test ('recursive filter custom function', (t) => {
const to_filter = [
{
name: 'foo',
children: [
{ name: 'bar' },
{ name: 'baz' },
{ foo: 'bar' }
]
}
];
const res = util.recursive_filter (
to_filter,
[ { function: (elem) => elem.name === 'foo bar' } ],
'children',
[ 'name' ]
);
t.deepEqual (res, [
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});

309
test/spec/index.js Normal file
View File

@ -0,0 +1,309 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of utilities which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
// @ts-nocheck
'use strict';
const util = require ('../../index');
// eslint-disable-next-line max-lines-per-function
describe ('utilities', () => {
it ('should truncate_decimal', () => {
const trunc = util.truncate_decimal (1.23456, 2);
expect (trunc)
.toEqual (1.23);
});
it ('try_parse_json should parse', () => {
const str = '{"test":"foo"}';
expect (() => {
const json = util.try_parse_json (str);
expect (json)
.toEqual ({ test: 'foo' });
}).not.toThrow ();
});
it ('try_parse_json should fail', () => {
const str = '{"test":foo"}';
expect (() => {
const json = util.try_parse_json (str);
expect (json)
.toBeNull ();
}).not.toThrow ();
});
it ('should copy object', () => {
const obj = { foo: 'bar', bar: { foo: 'baz' } };
const copy = util.copy_object (obj);
expect (obj).not.toBe (copy);
expect (obj)
.toEqual (copy);
copy.foo = 'baz';
copy.bar.foo = 'bar';
expect (copy.foo)
.toEqual ('baz');
expect (copy.bar.foo)
.toEqual ('bar');
expect (obj.foo)
.toEqual ('bar');
expect (obj.bar.foo)
.toEqual ('baz');
});
it ('should run regex', () => {
const data = 'foobarfoo';
const regex = /foo/gu;
let count = 0;
util.run_regex (regex, data, (res) => {
expect (res[0])
.toEqual ('foo');
count++;
});
expect (count)
.toEqual (2);
});
it ('should run non-global regex', () => {
const data = 'foobarfoo';
const regex = /foo/u;
let count = 0;
util.run_regex (regex, data, (res) => {
expect (res[0])
.toEqual ('foo');
count++;
});
expect (count)
.toEqual (1);
});
it ('should run non-global regex without result', () => {
const data = 'foobarfoo';
const regex = /baz/u;
let count = 0;
util.run_regex (regex, data, () => {
count++;
});
expect (count)
.toEqual (0);
});
it ('should check isnil with undefined', () => {
expect (util.is_nil ())
.toEqual (true);
});
it ('should check isnil with null', () => {
expect (util.is_nil (null))
.toEqual (true);
});
it ('should check isnil with empty string', () => {
expect (util.is_nil (''))
.toEqual (false);
});
it ('should check isnil with string', () => {
expect (util.is_nil ('foo'))
.toEqual (false);
});
it ('should check isnil with 0', () => {
expect (util.is_nil (0))
.toEqual (false);
});
it ('should check isnil with nan', () => {
expect (util.is_nil (parseInt ('foo')))
.toEqual (true);
});
it ('should check isnil with int', () => {
expect (util.is_nil (parseInt ('42')))
.toEqual (false);
});
it ('recursive filter', () => {
const raw = [
{ name: 'include_foo' },
{
name: 'include_bar',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
},
{
name: 'baz',
children: [
{ name: 'include_foo' },
{ name: 'bar' }
]
},
{
name: 'barbaz',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
}
];
const filtered = [
{ name: 'include_foo' },
{
name: 'include_bar',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
},
{
name: 'baz',
children: [ { name: 'include_foo' } ]
}
];
const filter = {
field: 'name',
filter: /^include_.*/ui
};
const result = util.recursive_filter (raw, [ filter ]);
expect (filtered)
.toEqual (result);
});
it ('recursive filter multifield', () => {
const raw = [
{ name: 'foo', f: 'include' },
{
name: 'include_bar',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
},
{
name: 'baz',
children: [
{ name: 'include_foo' },
{ name: 'bar' }
]
},
{
name: 'barbaz',
children: [
{ name: 'foo' },
{ name: 'bar' }
]
}
];
const filtered = [ { name: 'foo', f: 'include' } ];
const filter = {
field: [
'name',
'f'
],
filter: /foo include/ui
};
const result = util.recursive_filter (raw, [ filter ]);
expect (filtered)
.toEqual (result);
});
it ('recursive filter undefined multifield', () => {
const res = util.recursive_filter (
[ { foo: 'bar' } ],
[
{
field: [
'foo',
'bar'
],
filter: /\s/u
}
]
);
expect (res)
.toEqual ([]);
});
it ('recursive filter with or group', () => {
const to_filter = [
{ name: 'foo' },
{ name: 'bar' },
{ name: 'baz' }
];
const filter = [
{
or: [
{ field: 'name', filter: /foo/u },
{ field: 'name', filter: /bar/u }
]
}
];
const res = util.recursive_filter (to_filter, filter);
expect (res)
.toEqual (to_filter.slice (0, 2));
});
it ('recursive filter carry field', () => {
const to_filter = [
{
name: 'foo',
children: [
{ name: 'bar' },
{ name: 'baz' },
{ foo: 'bar' }
]
}
];
const res = util.recursive_filter (
to_filter,
[ { field: 'name', filter: /foo bar/ui } ],
'children',
[ 'name' ]
);
expect (res)
.toEqual ([
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});
it ('recursive filter custom function', () => {
const to_filter = [
{
name: 'foo',
children: [
{ name: 'bar' },
{ name: 'baz' },
{ foo: 'bar' }
]
}
];
const res = util.recursive_filter (
to_filter,
[ { function: (elem) => elem.name === 'foo bar' } ],
'children',
[ 'name' ]
);
expect (res)
.toEqual ([
{
name: 'foo',
children: [ { name: 'bar' } ]
}
]);
});
});

1888
yarn.lock

File diff suppressed because it is too large Load Diff