Skip to content

Commit

Permalink
Test updates (#501)
Browse files Browse the repository at this point in the history
* update tests

* japanese-util updates

* update json-schema tests
  • Loading branch information
Casheeew authored Jan 6, 2024
1 parent d17ca4b commit 60276d4
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 166 deletions.
34 changes: 13 additions & 21 deletions test/cache-map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,27 @@

/* eslint-disable no-multi-spaces */

import {expect, test} from 'vitest';
import {describe, expect, test} from 'vitest';
import {CacheMap} from '../ext/js/general/cache-map.js';

/** */
function testConstructor() {
test('constructor', () => {
const data = [
[false, () => new CacheMap(0)],
[false, () => new CacheMap(1)],
[false, () => new CacheMap(Number.MAX_VALUE)],
[true, () => new CacheMap(-1)],
[true, () => new CacheMap(1.5)],
[true, () => new CacheMap(Number.NaN)],
[true, () => new CacheMap(Number.POSITIVE_INFINITY)]
];
describe('constructor', () => {
const shouldThrow = [-1, 1.5, Number.NaN, Number.POSITIVE_INFINITY];
const shouldNotThrow = [0, 1, Number.MAX_VALUE];

for (const [throws, create] of data) {
if (throws) {
expect(create).toThrowError();
} else {
expect(create).not.toThrowError();
}
}
test.each(shouldNotThrow)('`() => new CacheMap(%d)` should not throw', (param) => {
expect(() => new CacheMap(param)).not.toThrowError();
});
test.each(shouldThrow)('`() => new CacheMap(%d)` should throw', (param) => {
expect(() => new CacheMap(param)).toThrowError();
});
});
}

/** */
function testApi() {
test('api', () => {
describe('api', () => {
const data = [
{
maxSize: 1,
Expand Down Expand Up @@ -99,7 +91,7 @@ function testApi() {
}
];

for (const {maxSize, expectedSize, calls} of data) {
test.each(data)('api-test-%#', ({maxSize, expectedSize, calls}) => {
const cache = new CacheMap(maxSize);
expect(cache.maxSize).toStrictEqual(maxSize);
for (const call of calls) {
Expand All @@ -117,7 +109,7 @@ function testApi() {
}
}
expect(cache.size).toStrictEqual(expectedSize);
}
});
});
}

Expand Down
130 changes: 81 additions & 49 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,140 +21,142 @@ import {DynamicProperty, deepEqual} from '../ext/js/core.js';

/** */
function testDynamicProperty() {
test('DynamicProperty', () => {
describe('DynamicProperty', () => {
/** @type {import('test/core').DynamicPropertyTestData} */
const data = [
{
initialValue: 0,
/** @type {{operation: ?string, expectedDefaultValue: number, expectedValue: number, expectedOverrideCount: number, expeectedEventOccurred: boolean, args: [value: number, priority?: number]}[]} */
operations: [
{
operation: null,
args: [0],
expectedDefaultValue: 0,
expectedValue: 0,
expectedOverrideCount: 0,
expeectedEventOccurred: false
expectedEventOccurred: false
},
{
operation: 'set.defaultValue',
args: [1],
expectedDefaultValue: 1,
expectedValue: 1,
expectedOverrideCount: 0,
expeectedEventOccurred: true
expectedEventOccurred: true
},
{
operation: 'set.defaultValue',
args: [1],
expectedDefaultValue: 1,
expectedValue: 1,
expectedOverrideCount: 0,
expeectedEventOccurred: false
expectedEventOccurred: false
},
{
operation: 'set.defaultValue',
args: [0],
expectedDefaultValue: 0,
expectedValue: 0,
expectedOverrideCount: 0,
expeectedEventOccurred: true
expectedEventOccurred: true
},
{
operation: 'setOverride',
args: [8],
expectedDefaultValue: 0,
expectedValue: 8,
expectedOverrideCount: 1,
expeectedEventOccurred: true
expectedEventOccurred: true
},
{
operation: 'setOverride',
args: [16],
expectedDefaultValue: 0,
expectedValue: 8,
expectedOverrideCount: 2,
expeectedEventOccurred: false
expectedEventOccurred: false
},
{
operation: 'setOverride',
args: [32, 1],
expectedDefaultValue: 0,
expectedValue: 32,
expectedOverrideCount: 3,
expeectedEventOccurred: true
expectedEventOccurred: true
},
{
operation: 'setOverride',
args: [64, -1],
expectedDefaultValue: 0,
expectedValue: 32,
expectedOverrideCount: 4,
expeectedEventOccurred: false
expectedEventOccurred: false
},
{
operation: 'clearOverride',
args: [-4],
expectedDefaultValue: 0,
expectedValue: 32,
expectedOverrideCount: 3,
expeectedEventOccurred: false
expectedEventOccurred: false
},
{
operation: 'clearOverride',
args: [-3],
expectedDefaultValue: 0,
expectedValue: 32,
expectedOverrideCount: 2,
expeectedEventOccurred: false
expectedEventOccurred: false
},
{
operation: 'clearOverride',
args: [-2],
expectedDefaultValue: 0,
expectedValue: 64,
expectedOverrideCount: 1,
expeectedEventOccurred: true
expectedEventOccurred: true
},
{
operation: 'clearOverride',
args: [-1],
expectedDefaultValue: 0,
expectedValue: 0,
expectedOverrideCount: 0,
expeectedEventOccurred: true
expectedEventOccurred: true
}
]
}
];

for (const {initialValue, operations} of data) {
const property = new DynamicProperty(initialValue);
const overrideTokens = [];
let eventOccurred = false;
const onChange = () => { eventOccurred = true; };
property.on('change', onChange);
for (const {operation, args, expectedDefaultValue, expectedValue, expectedOverrideCount, expeectedEventOccurred} of operations) {
eventOccurred = false;
switch (operation) {
case 'set.defaultValue': property.defaultValue = args[0]; break;
case 'setOverride': overrideTokens.push(property.setOverride(...args)); break;
case 'clearOverride': property.clearOverride(overrideTokens[overrideTokens.length + args[0]]); break;
describe.each(data)('Test DynamicProperty($initialValue)', ({initialValue, operations}) => {
test('works as expected', () => {
const property = new DynamicProperty(initialValue);
const overrideTokens = [];
let eventOccurred = false;
const onChange = () => { eventOccurred = true; };
property.on('change', onChange);
for (const {operation, args, expectedDefaultValue, expectedValue, expectedOverrideCount, expectedEventOccurred} of operations) {
eventOccurred = false;
switch (operation) {
case 'set.defaultValue': property.defaultValue = args[0]; break;
case 'setOverride': overrideTokens.push(property.setOverride(...args)); break;
case 'clearOverride': property.clearOverride(overrideTokens[overrideTokens.length + args[0]]); break;
}
expect(eventOccurred).toStrictEqual(expectedEventOccurred);
expect(property.defaultValue).toStrictEqual(expectedDefaultValue);
expect(property.value).toStrictEqual(expectedValue);
expect(property.overrideCount).toStrictEqual(expectedOverrideCount);
}
expect(eventOccurred).toStrictEqual(expeectedEventOccurred);
expect(property.defaultValue).toStrictEqual(expectedDefaultValue);
expect(property.value).toStrictEqual(expectedValue);
expect(property.overrideCount).toStrictEqual(expectedOverrideCount);
}
property.off('change', onChange);
}
property.off('change', onChange);
});
});
});
}

/** */
function testDeepEqual() {
describe('deepEqual', () => {
const data = [
// Simple tests
/** @type {import('test/core').DeepEqualTestData} */
const simpleTestsData = [
{
value1: 0,
value2: 0,
Expand Down Expand Up @@ -194,9 +196,10 @@ function testDeepEqual() {
value1: true,
value2: false,
expected: false
},

// Simple object tests
}
];
/** @type {import('test/core').DeepEqualTestData} */
const simpleObjectTestsData = [
{
value1: {},
value2: {},
Expand All @@ -216,9 +219,10 @@ function testDeepEqual() {
value1: {},
value2: null,
expected: false
},

// Complex object tests
}
];
/** @type {import('test/core').DeepEqualTestData} */
const complexObjectTestsData = [
{
value1: [1],
value2: [],
Expand Down Expand Up @@ -259,27 +263,55 @@ function testDeepEqual() {
value1: {test: {test2: [true]}},
value2: {test: {test2: [true]}},
expected: true
},

// Recursive
}
];
/** @type {import('test/core').DeepEqualTestData} */
const recursiveTestsData = [
{
value1: (() => { const x = {}; x.x = x; return x; })(),
value2: (() => { const x = {}; x.x = x; return x; })(),
expected: false
}
];
describe('simple tests', () => {
test.each(simpleTestsData)('deepEqual($value1, $value2) -> $expected', ({value1, value2, expected}) => {
const actual1 = deepEqual(value1, value2);
expect(actual1).toStrictEqual(expected);

const actual2 = deepEqual(value2, value1);
expect(actual2).toStrictEqual(expected);
});
});

describe('simple object tests', () => {
test.each(simpleObjectTestsData)('deepEqual($value1, $value2) -> $expected', ({value1, value2, expected}) => {
const actual1 = deepEqual(value1, value2);
expect(actual1).toStrictEqual(expected);

const actual2 = deepEqual(value2, value1);
expect(actual2).toStrictEqual(expected);
});
});

describe('complex object tests', () => {
test.each(complexObjectTestsData)('deepEqual($value1, $value2) -> $expected', ({value1, value2, expected}) => {
const actual1 = deepEqual(value1, value2);
expect(actual1).toStrictEqual(expected);

const actual2 = deepEqual(value2, value1);
expect(actual2).toStrictEqual(expected);
});
});

let index = 0;
for (const {value1, value2, expected} of data) {
test(`${index}`, () => {
describe('recursive tests', () => {
test.each(recursiveTestsData)('deepEqual($value1, $value2) -> $expected', ({value1, value2, expected}) => {
const actual1 = deepEqual(value1, value2);
expect(actual1).toStrictEqual(expected);

const actual2 = deepEqual(value2, value1);
expect(actual2).toStrictEqual(expected);
});
++index;
}
});
});
}

Expand Down
8 changes: 4 additions & 4 deletions test/css-json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
*/

import fs from 'fs';
import {expect, test} from 'vitest';
import {describe, expect, test} from 'vitest';
import {formatRulesJson, generateRules, getTargets} from '../dev/generate-css-json';

/** */
function main() {
test('css-json', () => {
for (const {cssFilePath, overridesCssFilePath, outputPath} of getTargets()) {
describe('css-json', () => {
test.each(getTargets())('css-json-test-%#', ({cssFilePath, overridesCssFilePath, outputPath}) => {
const actual = fs.readFileSync(outputPath, {encoding: 'utf8'});
const expected = formatRulesJson(generateRules(cssFilePath, overridesCssFilePath));
expect(actual).toStrictEqual(expected);
}
});
});
}

Expand Down
Loading

0 comments on commit 60276d4

Please sign in to comment.