Skip to content

Commit

Permalink
chore: organize test suites (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored May 19, 2023
1 parent b98f883 commit 75b45c6
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 198 deletions.
58 changes: 26 additions & 32 deletions src/core/compare/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@ import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import * as compare from './index.js';

const basics = {
inspect: suite('compare:inspect'),
wildcard: suite('compare:wildcard'),
const suites = {
'inspect/': suite('compare/inspect'),
'wildcard/': suite('compare/wildcard'),

undefined: suite('compare:undefined'),
boolean: suite('compare:boolean'),
number: suite('compare:number'),
bigint: suite('compare:bigint'),
symbol: suite('compare:symbol'),
string: suite('compare:string'),
object: suite('compare:object'),
'undefined/': suite('compare/undefined'),
'boolean/': suite('compare/boolean'),
'number/': suite('compare/number'),
'bigint/': suite('compare/bigint'),
'symbol/': suite('compare/symbol'),
'string/': suite('compare/string'),
'object/': suite('compare/object'),

date: suite('compare:date'),
time: suite('compare:time'),
'date/': suite('compare/date'),
'time/': suite('compare/time'),

order: suite('compare:order'),
};
'order/': suite('compare/order'),
'order/key': suite('compare/order:key'),
} as const;

const composite = {
order: suite('compare:key+order'),
};

// ---- standard ----

basics.inspect('inspect', () => {
suites['inspect/']('inspect', () => {
assert.type(compare.inspect, 'function');

const data = [{ id: 0, name: 'B' }, { name: 'A' }, { id: 1, name: 'C' }];
Expand All @@ -37,25 +32,28 @@ basics.inspect('inspect', () => {
]);
});

basics.undefined('sort undefined values with null values above', () => {
suites['undefined/']('sort undefined values with null values above', () => {
assert.equal(
[undefined, 3, 0, null, 1, -1, undefined, -2, undefined, null].sort(compare.undefined),
[3, 0, 1, -1, -2, null, null, undefined, undefined, undefined]
);
});
basics.boolean('sort boolean values with true above', () => {

suites['boolean/']('sort boolean values with true above', () => {
assert.equal(
[true, false, true, false, true, false, true, false, true, false].sort(compare.boolean),
[true, true, true, true, true, false, false, false, false, false]
);
});
basics.number('sort number in descending order', () => {

suites['number/']('sort number in descending order', () => {
assert.equal(
[5, 3, 9, 6, 0, 2, 1, -1, 4, -2].sort(compare.number),
[9, 6, 5, 4, 3, 2, 1, 0, -1, -2]
);
});
basics.string('sort string in alphabetical order', () => {

suites['string/']('sort string in alphabetical order', () => {
assert.equal(
['k', 'h', 'g', 'f', 'e', 'l', 'd', 'm', 'c', 'b', 'j', 'i', 'a'].sort(compare.string),
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']
Expand All @@ -66,18 +64,14 @@ basics.string('sort string in alphabetical order', () => {
);
});

basics.order('customized compare with order', () => {
suites['order/']('customized compare with order', () => {
const months = ['January', 'February', 'March', 'April', 'May', 'June'];
const list = ['March', 'June', 'May', 'April', 'January', 'June', 'February'];
const result = ['January', 'February', 'March', 'April', 'May', 'June', 'June'];
assert.equal(list.sort(compare.order(months)), result);
});

Object.values(basics).forEach((v) => v.run());

// ---- composite ----

composite.order('nested keyed compare with order', () => {
suites['order/key']('nested keyed compare with order', () => {
const months = ['January', 'February', 'March', 'April', 'May', 'June'];
const posts = [
{ date: { pub: { month: 'March' } } },
Expand All @@ -99,4 +93,4 @@ composite.order('nested keyed compare with order', () => {
]);
});

Object.values(composite).forEach((v) => v.run());
Object.values(suites).forEach((v) => v.run());
12 changes: 6 additions & 6 deletions src/core/lambda/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import * as assert from 'uvu/assert';
import curry from './curry.js';
import pipe from './pipe.js';

const basics = {
curry: suite('lambda:curry'),
pipe: suite('lambda:pipe'),
const suites = {
'curry/': suite('lambda/curry'),
'pipe/': suite('lambda/pipe'),
};

basics.curry('properly curry a function', () => {
suites['curry/']('properly curry a function', () => {
const sum = (a: number, b: number, c: number) => a + b + c;
const curried = curry(sum);

Expand All @@ -20,7 +20,7 @@ basics.curry('properly curry a function', () => {
assert.equal(curried(1)(1)(1), 3);
});

basics.pipe('properly apply functions in ltr order', () => {
suites['pipe/']('properly apply functions in ltr order', () => {
const cap = (v: string) => v.toUpperCase();
const name = <T extends { name: string }>(v: T) => v.name;
const split = (v: string) => v.split('');
Expand All @@ -29,4 +29,4 @@ basics.pipe('properly apply functions in ltr order', () => {
assert.equal(pipeline({ name: 'mom' }), ['M', 'O', 'M']);
});

Object.values(basics).forEach((v) => v.run());
Object.values(suites).forEach((v) => v.run());
38 changes: 18 additions & 20 deletions src/core/random/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,53 @@ import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import * as random from './index.js';

const basics = {
float: suite('random:float'),
int: suite('random:int'),
bool: suite('random:bool'),
array: suite('random:array'),
key: suite('random:key'),
val: suite('random:val'),
// hex: suite('random:hex'),
// ipv4: suite('random:ipv4'),
uuid: suite('random:uuid'),
const suites = {
'float/': suite('random/float'),
'int/': suite('random/int'),
'bool/': suite('random/bool'),
'array/': suite('random/array'),
'key/': suite('random/key'),
'val/': suite('random/val'),
// 'hex/': suite('random/hex'),
// 'ipv4/': suite('random/ipv4'),
'uuid/': suite('random/uuid'),
};

// ---- standard ----

basics.float('generate random float', () => {
suites['float/']('generate random float', () => {
const number = random.float();
assert.type(number, 'number');
assert.ok(number >= 0 && number <= 1);
});

basics.int('generate random integer', () => {
suites['int/']('generate random integer', () => {
const number = random.int();
assert.type(number, 'number');
assert.ok(number === 0 || number === 1);
});

basics.bool('generate random bool', () => {
suites['bool/']('generate random bool', () => {
assert.type(random.bool(), 'boolean');
});

basics.array('generate array with random values', () => {
suites['array/']('generate array with random values', () => {
const array = random.array(5, 3);
assert.type(array, 'object');
assert.equal(array.length, 5);
});

basics.key('get random key from object', () => {
suites['key/']('get random key from object', () => {
const key = random.key({ foo: 0, bar: 1 });
assert.type(key, 'string');
assert.ok(key === 'foo' || key === 'bar');
});

basics.val('get random value from object', () => {
suites['val/']('get random value from object', () => {
const val = random.val({ foo: 0, bar: 1 });
assert.type(val, 'number');
assert.ok(val === 0 || val === 1);
});

basics.uuid('generate random uuid', () => {
suites['uuid/']('generate random uuid', () => {
const floating = random.uuid();
assert.equal(floating.length, 36);
assert.equal(floating.split('-').length, 5);
Expand All @@ -61,4 +59,4 @@ basics.uuid('generate random uuid', () => {
assert.equal(secure.split('-').length, 5);
});

Object.values(basics).forEach((v) => v.run());
Object.values(suites).forEach((v) => v.run());
20 changes: 10 additions & 10 deletions src/core/standard/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import * as std from './index.js';

const basics = {
capitalize: suite('std:capitalize'),
identical: suite('std:identical'),
const suites = {
'capitalize/': suite('std/capitalize'),
'identical/': suite('std/identical'),
};

basics.capitalize('change one letter for one word', () => {
suites['capitalize/']('change one letter for one word', () => {
assert.equal(std.capitalize('hello'), 'Hello');
});
basics.capitalize('change two letter for two words', () => {
suites['capitalize/']('change two letter for two words', () => {
assert.equal(std.capitalize('hello world'), 'Hello World');
});

basics.identical('identical primitive checks', () => {
suites['identical/']('identical primitive checks', () => {
// boolean
assert.ok(std.identical(true, true));
assert.ok(!std.identical(true, false));
Expand Down Expand Up @@ -55,22 +55,22 @@ basics.identical('identical primitive checks', () => {
)
);
});
basics.identical('identical array checks', () => {
suites['identical/']('identical array checks', () => {
assert.ok(std.identical([], []));
assert.ok(std.identical(['', 1, !0], ['', 1, !0]));
assert.ok(std.identical([{ x: [] }], [{ x: [] }]));
assert.ok(!std.identical(['', 0, !0], ['', 1, !1]));
assert.ok(!std.identical([{ x: [] }], [{ y: [] }]));
});
basics.identical('identical object checks', () => {
suites['identical/']('identical object checks', () => {
assert.ok(std.identical({}, {}));
assert.ok(std.identical({ a: '', b: 1, c: !0 }, { a: '', b: 1, c: !0 }));
assert.ok(std.identical({ x: [{}], y: { a: 0 } }, { x: [{}], y: { a: 0 } }));
});
basics.identical('identical clone', async () => {
suites['identical/']('identical clone', async () => {
const { clone } = await import('../../std/ntv/object.js');
const data = { a: [1, '', {}], o: { now: new Date() } };
assert.ok(std.identical(data, clone(data)));
});

Object.values(basics).forEach((v) => v.run());
Object.values(suites).forEach((v) => v.run());
12 changes: 6 additions & 6 deletions src/core/standard/unique.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import * as assert from 'uvu/assert';

import unique from './unique.js';

const basics = {
simple: suite('unique:simple'),
object: suite('unique:object'),
const suites = {
'simple/': suite('unique/simple'),
'object/': suite('unique/object'),
};

basics.simple('make array items unique', () => {
suites['simple/']('make array items unique', () => {
assert.equal(unique([true, false, !0, !1]), [true, false]);
assert.equal(unique([1, 1, 2, 3, 2, 4, 5]), [1, 2, 3, 4, 5]);
assert.equal(unique(['a', 'a', 'b', 'c', 'b']), ['a', 'b', 'c']);
Expand All @@ -17,7 +17,7 @@ basics.simple('make array items unique', () => {
assert.equal(unique(months), ['jan', 'feb', 'mar']);
});

basics.object('make array of object unique', () => {
suites['object/']('make array of object unique', () => {
assert.equal(
unique(
[
Expand Down Expand Up @@ -53,4 +53,4 @@ basics.object('make array of object unique', () => {
);
});

Object.values(basics).forEach((v) => v.run());
Object.values(suites).forEach((v) => v.run());
24 changes: 8 additions & 16 deletions src/core/temporal/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import * as dt from './index.js';

const basics = {
build: suite('temporal:build'),
format: suite('temporal:format'),
travel: suite('temporal:travel'),
const suites = {
'build/': suite('temporal/build'),
'format/': suite('temporal/format'),
'travel/': suite('temporal/travel'),
};

const fixed = new Date('2017/09/08, 13:02:03');

// ---- build ----

basics.build('basic formatter builder', () => {
suites['build/']('basic formatter builder', () => {
const format = dt.build({ base: 'UTC' });

assert.type(format, 'function');
Expand All @@ -22,9 +20,7 @@ basics.build('basic formatter builder', () => {
assert.equal(renderer('DD/MM/YYYY (Z)'), '08/09/2017 (+0)');
});

// ---- format ----

basics.format('basic rendering', () => {
suites['format/']('basic rendering', () => {
const renderer = dt.format(fixed);

assert.equal(renderer('foo'), 'foo');
Expand Down Expand Up @@ -55,12 +51,8 @@ basics.format('basic rendering', () => {
'Valid from: [2017-09-08 ~ 13:02:03]'
);
});
basics.format('throw on invalid date', () => {
suites['format/']('throw on invalid date', () => {
assert.throws(() => dt.format('invalid'));
});

// ---- travel ----

// basics.travel('basic travelling', () => {});

Object.values(basics).forEach((v) => v.run());
Object.values(suites).forEach((v) => v.run());
Loading

0 comments on commit 75b45c6

Please sign in to comment.