UNPKG

@schukai/monster

Version:

Monster is a simple library for creating fast, robust and lightweight websites.

215 lines (162 loc) 5.74 kB
import {expect} from "chai" import {Comparator} from "../../../source/util/comparator.mjs"; describe('Comparator', function () { describe('create new instance', function () { it('should return a comparator object', function () { expect(new Comparator()).to.be.a('object'); }); it('should return a comparator object', function () { expect(new Comparator(function () { })).to.be.a('object'); }); it('should throw TypeError', function () { expect(() => new Comparator(true)).to.throw(TypeError); }); it('should throw TypeError', function () { expect(() => new Comparator("test")).to.throw(TypeError); }); }); describe('equal()', function () { [ ['test1', "test", false], [5.1, 5, false], [5.1, 5.1, true], [true, true, true], [false, true, false], [false, false, true], [-4, -4, true], ].forEach(function (data) { let a = data.shift() let b = data.shift() let c = data.shift() it('should compare ' + a + ' and ' + b + ' return ' + c, function () { expect(new Comparator().equal(a, b)).is.equal(c) }); }); }); describe('equal()', function () { [ ['test1', true], ['test1', 5], ['test1', null], ['test1', parseInt("a")], [false, 5], [undefined, null], ].forEach(function (data) { let a = data.shift() let b = data.shift() it('should compare ' + a + ' and ' + b + ' throw TypeError', function () { expect(() => new Comparator().equal(a, b)).to.throw(TypeError); }); }); }); describe('greaterThan()', function () { [ ['test1', "test", true], [5.1, 5, true], [5.1, 5.1, false], [true, true, false], [false, true, false], [false, false, false], [-4, -4, false], [-4, 4, false], ].forEach(function (data) { let a = data.shift() let b = data.shift() let c = data.shift() it('should compare ' + a + ' and ' + b + ' return ' + c, function () { expect(new Comparator().greaterThan(a, b)).is.equal(c) }); }); }); describe('reverse().greaterThan()', function () { [ ['test1', "test", true], [5.1, 5, true], [5.1, 5.1, false], [true, true, false], [false, true, false], [false, false, false], [-4, -4, false], [-4, 4, false], ].forEach(function (data) { let b = data.shift() let a = data.shift() let c = data.shift() it('should compare ' + a + ' and ' + b + ' return ' + c, function () { expect(new Comparator().reverse().greaterThan(a, b)).is.equal(c) }); }); }); describe('greaterThanOrEqual()', function () { [ ['test1', "test", true], [5.1, 5, true], [5.1, 5.1, true], [true, true, true], [false, true, false], [false, false, true], [-4, -4, true], [-4, 4, false], ].forEach(function (data) { let a = data.shift() let b = data.shift() let c = data.shift() it('should compare ' + a + ' and ' + b + ' return ' + c, function () { expect(new Comparator().greaterThanOrEqual(a, b)).is.equal(c) }); }); }); describe('lessThan()', function () { [ ['test1', "test", true], [5.1, 5, true], [5.1, 5.1, false], [true, true, false], [false, true, false], [false, false, false], [-4, -4, false], [-4, 4, false], ].forEach(function (data) { let b = data.shift() let a = data.shift() let c = data.shift() it('should compare ' + a + ' and ' + b + ' return ' + c, function () { expect(new Comparator().lessThan(a, b)).is.equal(c) }); }); }); describe('documentations', function () { it('should run ...', function () { expect(new Comparator().lessThanOrEqual(2, 5)).to.be.true; expect(new Comparator().greaterThan(4, 2)).to.be.true; expect(new Comparator().equal(4, 4)).to.be.true; expect(new Comparator().equal(4, 5)).to.be.false; }); it('should run with own function ...', function () { expect(new Comparator(function (a, b) { if (a.v === b.v) return 0; return a.v < b.v ? -1 : 1; }).equal({v: 2}, {v: 2})).to.be.true; }); }) describe('lessThanOrEqual()', function () { [ ['test1', "test", true], [5.1, 5, true], [5.1, 5.1, true], [true, true, true], [false, true, false], [false, false, true], [-4, -4, true], [-4, 4, false], ].forEach(function (data) { let b = data.shift() let a = data.shift() let c = data.shift() it('should compare ' + a + ' and ' + b + ' return ' + c, function () { expect(new Comparator().lessThanOrEqual(a, b)).is.equal(c) }); }); }); });