From: Jérôme Benoit Date: Mon, 23 Jun 2025 11:10:47 +0000 (+0200) Subject: test: add UTs for newly added helpers X-Git-Tag: ocpp-server@v2.0.9~9 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=06ca76ae5f156e86591bb1f21c80240ff8dd956f;p=e-mobility-charging-stations-simulator.git test: add UTs for newly added helpers Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index efce9a82..efa0ec5f 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -43,7 +43,7 @@ export const once = any>(fn: T): T => { } as T } -export const has = (property: PropertyKey, object: object | undefined): boolean => { +export const has = (property: PropertyKey, object: null | object | undefined): boolean => { if (object == null) { return false } @@ -73,6 +73,14 @@ export const isEmpty = (value: unknown): boolean => { return (value as unknown[]).length === 0 } + if (valueType === 'Map') { + return (value as Map).size === 0 + } + + if (valueType === 'Set') { + return (value as Set).size === 0 + } + return false } diff --git a/tests/utils/StatisticUtils.test.ts b/tests/utils/StatisticUtils.test.ts index d3c6243f..1ba3366f 100644 --- a/tests/utils/StatisticUtils.test.ts +++ b/tests/utils/StatisticUtils.test.ts @@ -1,9 +1,30 @@ import { expect } from '@std/expect' import { describe, it } from 'node:test' -import { max, min, nthPercentile, stdDeviation } from '../../src/utils/StatisticUtils.js' +import { + max, + mean, + median, + min, + nthPercentile, + stdDeviation, +} from '../../src/utils/StatisticUtils.js' await describe('StatisticUtils test suite', async () => { + await it('Verify mean()', () => { + expect(mean([])).toBe(0) + expect(mean([0.08])).toBe(0.08) + expect(mean([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.1642857142857146) + expect(mean([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.8533333333333335) + }) + + await it('Verify median()', () => { + expect(median([])).toBe(0) + expect(median([0.08])).toBe(0.08) + expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05) + expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535) + }) + await it('Verify min()', () => { expect(min()).toBe(Number.POSITIVE_INFINITY) expect(min(0, 1)).toBe(0) diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index bf874330..42211c3a 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -21,12 +21,15 @@ import { formatDurationSeconds, generateUUID, getRandomFloat, + has, insertAt, isArraySorted, isAsyncFunction, + isEmpty, isNotEmptyArray, isNotEmptyString, isValidDate, + once, roundTo, secureRandom, sleep, @@ -312,6 +315,57 @@ await describe('Utils test suite', async () => { expect(() => clone(weakSet)).toThrow(new Error('# could not be cloned.')) }) + await it('Verify once()', () => { + let called = 0 + const fn = (): number => ++called + const onceFn = once(fn) + const result1 = onceFn() + expect(called).toBe(1) + expect(result1).toBe(1) + const result2 = onceFn() + expect(called).toBe(1) + expect(result2).toBe(1) + const result3 = onceFn() + expect(called).toBe(1) + expect(result3).toBe(1) + }) + + await it('Verify has()', () => { + expect(has('', 'test')).toBe(false) + expect(has('test', '')).toBe(false) + expect(has('test', 'test')).toBe(false) + expect(has('', undefined)).toBe(false) + expect(has('', null)).toBe(false) + expect(has('', [])).toBe(false) + expect(has('', {})).toBe(false) + expect(has(1, { 1: 1 })).toBe(true) + expect(has('1', { 1: 1 })).toBe(true) + expect(has(2, { 1: 1 })).toBe(false) + expect(has('2', { 1: 1 })).toBe(false) + expect(has('1', { 1: '1' })).toBe(true) + expect(has(1, { 1: '1' })).toBe(true) + expect(has('2', { 1: '1' })).toBe(false) + expect(has(2, { 1: '1' })).toBe(false) + }) + + await it('Verify isEmpty()', () => { + expect(isEmpty('')).toBe(true) + expect(isEmpty(' ')).toBe(false) + expect(isEmpty(' ')).toBe(false) + expect(isEmpty('test')).toBe(false) + expect(isEmpty(' test')).toBe(false) + expect(isEmpty('test ')).toBe(false) + expect(isEmpty(undefined)).toBe(false) + expect(isEmpty(null)).toBe(false) + expect(isEmpty(0)).toBe(false) + expect(isEmpty({})).toBe(true) + expect(isEmpty([])).toBe(true) + expect(isEmpty(new Map())).toBe(true) + expect(isEmpty(new Set())).toBe(true) + expect(isEmpty(new WeakMap())).toBe(false) + expect(isEmpty(new WeakSet())).toBe(false) + }) + await it('Verify isNotEmptyString()', () => { expect(isNotEmptyString('')).toBe(false) expect(isNotEmptyString(' ')).toBe(false)