]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
test: add UTs for newly added helpers
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 23 Jun 2025 11:10:47 +0000 (13:10 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 23 Jun 2025 11:10:47 +0000 (13:10 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/utils/Utils.ts
tests/utils/StatisticUtils.test.ts
tests/utils/Utils.test.ts

index efce9a828c26617fa3a6edca7b6d9bda50d23ea5..efa0ec5f20da5abdae74d9148ae4a3cac6b6f1dc 100644 (file)
@@ -43,7 +43,7 @@ export const once = <T extends (...args: any[]) => 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<unknown, unknown>).size === 0
+  }
+
+  if (valueType === 'Set') {
+    return (value as Set<unknown>).size === 0
+  }
+
   return false
 }
 
index d3c6243fa8174ef95dc67bd476b85797d4a73849..1ba3366f67be3e013b947d630802c07825b9a679 100644 (file)
@@ -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)
index bf87433002742a07b4eac31808cf2973bab12fdb..42211c3a2f123ed018bbc85195bf94694f361fb9 100644 (file)
@@ -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('#<WeakSet> 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)