Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Niekes committed Dec 15, 2023
1 parent 3a2f782 commit b829aba
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
38 changes: 36 additions & 2 deletions tests/number.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
import { test } from 'tape';
import { getRandomInt } from '../dist/utils/number.js';
import { getRandomInt, getRandomFloat, interpolateLinear } from '../dist/utils/number.js';

test('getRandomInt should return random int within min max range', (t) => {
const randomInt = getRandomInt(0, 100);

test('getRandomInt should return constant when min and max are the same', (t) => {
t.equal(getRandomInt(0, 0), 0);
t.equal(getRandomInt(100, 100), 100);
t.equal(typeof randomInt, 'number');
t.equal(randomInt <= 100 && randomInt >= 0, true);
t.end();
});

test('getRandomFloat should return random int within min max range', (t) => {
const randonFloat = getRandomFloat(0, 100);

t.equal(getRandomFloat(0.1, 0.1), 0.1);
t.equal(getRandomFloat(99.99, 99.99), 99.99);
t.equal(typeof randonFloat, 'number');
t.equal(randonFloat <= 100 && randonFloat >= 0, true);
t.end();
});

test.only('interpolateLinear should return random int within min max range', (t) => {
const value1 = interpolateLinear(0.5, 0, 1, 0, 10);
const value2 = interpolateLinear(0.5, 0, 1, 0, 100);
const value3 = interpolateLinear(1.2, 0, 1, 0, 10);
const value4 = interpolateLinear(-30, 0, 1, 0, 10);
const value5 = interpolateLinear(0, -100, 100, -1, 1);

t.equal(value1, 5);
t.equal(value2, 50);
t.equal(value3, 10);
t.equal(value4, 0);
t.equal(value5, 0);
t.equal(typeof value1, 'number');
t.equal(typeof value2, 'number');
t.equal(typeof value3, 'number');
t.equal(typeof value4, 'number');
t.equal(typeof value5, 'number');
t.end();
});
26 changes: 25 additions & 1 deletion tests/object.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from 'tape';
import { has } from '../dist/utils/object.js';
import { has, isEmpty, isObject } from '../dist/utils/object.js';

test('has should return true or false correctly', (t) => {
const obj = {
Expand All @@ -10,3 +10,27 @@ test('has should return true or false correctly', (t) => {
t.equal(has(obj, 'name'), false);
t.end();
});

test('isEmpty should check weather an object is empty or not', (t) => {
const obj = {
prop: 1
};

t.equal(isEmpty({}), true);
t.equal(isEmpty(obj), false);
t.end();
});

test('isEmpty should check weather an object is an object', (t) => {
const obj = {
prop: 1
};

t.equal(isObject({}), true);
t.equal(isObject(obj), true);
t.equal(isObject(123), false);
t.equal(isObject('123'), false);
t.equal(isObject(new Date()), false);
t.equal(isObject(Number), false);
t.end();
});

0 comments on commit b829aba

Please sign in to comment.