From 391c074ef93987d0b696c131a77bc2b02ebf2d43 Mon Sep 17 00:00:00 2001 From: Meier Lukas Date: Thu, 7 Sep 2023 21:32:29 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20some=20tools=20?= =?UTF-8?q?functions=20(#1377)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✅ Add tests for some tools functions * 🐛 Build issue with language definition --- src/tools/_tests/bytesHelper.spec.ts | 73 ++++++++++++++++ src/tools/_tests/humanFileSize.spec.ts | 110 +++++++++++++++++++++++++ src/tools/_tests/language.spec.ts | 22 +++++ src/tools/humanFileSize.ts | 16 ++-- src/tools/language.ts | 9 +- 5 files changed, 219 insertions(+), 11 deletions(-) create mode 100644 src/tools/_tests/bytesHelper.spec.ts create mode 100644 src/tools/_tests/humanFileSize.spec.ts create mode 100644 src/tools/_tests/language.spec.ts diff --git a/src/tools/_tests/bytesHelper.spec.ts b/src/tools/_tests/bytesHelper.spec.ts new file mode 100644 index 000000000..b7b4a301c --- /dev/null +++ b/src/tools/_tests/bytesHelper.spec.ts @@ -0,0 +1,73 @@ +import { describe, expect, it } from 'vitest'; + +import { bytes } from '../bytesHelper'; + +describe('bytes.toPerSecondString', () => { + it('should format 999 bytes as 999 b/s', () => { + // Arrange + const byteCount = 999; + // Act + const result = bytes.toPerSecondString(byteCount); + // Assert + expect(result).toBe('999.0 b/s'); + }); + it.each([ + [1, 'b/s', 'Kb/s'], + [2, 'Kb/s', 'Mb/s'], + [3, 'Mb/s', 'Gb/s'], + ])('should format 1000^%s bytes or 1000 %s as 1.0 %s', (power, _unit, nextUnit) => { + // Arrange + const byteCount = Math.pow(1000, power); + // Act + const result = bytes.toPerSecondString(byteCount); + // Assert + expect(result).toBe(`1.0 ${nextUnit}`); + }); + it('should format 1000^4 bytes or 1 Tb/s with as 1000.0 Gb/s', () => { + // Arrange + const byteCount = Math.pow(1000, 4); + // Act + const result = bytes.toPerSecondString(byteCount); + // Assert + expect(result).toBe('1000.0 Gb/s'); + }); + it('should format undefined as -', () => { + // Arrange + const byteCount = undefined; + // Act + const result = bytes.toPerSecondString(byteCount); + // Assert + expect(result).toBe('-'); + }); +}); + +describe('bytes.toString', () => { + it('should format 999 bytes as 999 B', () => { + // Arrange + const byteCount = 999; + // Act + const result = bytes.toString(byteCount); + // Assert + expect(result).toBe('999.0 B'); + }); + it.each([ + [1, 'B', 'KiB'], + [2, 'KiB', 'MiB'], + [3, 'MiB', 'GiB'], + ])('should format 1024^%s bytes or 1024 %s as 1.0 %s', (power, _unit, nextUnit) => { + // Arrange + const byteCount = Math.pow(1024, power); + // Act + const result = bytes.toString(byteCount); + // Assert + expect(result).toBe(`1.0 ${nextUnit}`); + }); + it('should format 1024^4 bytes or 1 TiB with as 1024.0 GiB', () => { + // Arrange + const byteCount = Math.pow(1024, 4); + // Act + const result = bytes.toString(byteCount); + // Assert + expect(result).toBe('1024.0 GiB'); + }); +}); diff --git a/src/tools/_tests/humanFileSize.spec.ts b/src/tools/_tests/humanFileSize.spec.ts new file mode 100644 index 000000000..421642a19 --- /dev/null +++ b/src/tools/_tests/humanFileSize.spec.ts @@ -0,0 +1,110 @@ +import { describe, expect, it, vi } from 'vitest'; + +import { humanFileSize } from '../humanFileSize'; + +describe('humanFileSize', () => { + it('should format 1023 bytes as 1023 B', () => { + // Arrange + const bytes = 1023; + + // Act + const result = humanFileSize(bytes, false); + + // Assert + expect(result).toBe('1023 B'); + }); + it.each([ + [1, 'B', 'KiB'], + [2, 'KiB', 'MiB'], + [3, 'MiB', 'GiB'], + [4, 'GiB', 'TiB'], + [5, 'TiB', 'PiB'], + [6, 'PiB', 'EiB'], + [7, 'EiB', 'ZiB'], + [8, 'ZiB', 'YiB'], + ])( + 'should format 1024^%s B or 1024 %s with 1024 threshhold as 1.0 %s', + (power, _unit, nextUnit) => { + // Arrange + const bytes = Math.pow(1024, power); + + // Act + const result = humanFileSize(bytes, false); + + // Assert + expect(result).toBe(`1.0 ${nextUnit}`); + } + ); + it('should format 1024^9 B or 1024 YiB with 1024 threshhold as 1024.0 YiB', () => { + // Arrange + const bytes = Math.pow(1024, 9); + + // Act + const result = humanFileSize(bytes, false); + + // Assert + expect(result).toBe('1024.0 YiB'); + }); + it('should format 999 bytes as 999 B', () => { + // Arrange + const bytes = 999; + + // Act + const result = humanFileSize(bytes); + + // Assert + expect(result).toBe('999 B'); + }); + it.each([ + [1, 'B', 'KB'], + [2, 'KB', 'MB'], + [3, 'MB', 'GB'], + [4, 'GB', 'TB'], + [5, 'TB', 'PB'], + [6, 'PB', 'EB'], + [7, 'EB', 'ZB'], + [8, 'ZB', 'YB'], + ])( + 'should format 1000^%s B or 1000 %s with 1000 threshhold as 1.0 %s', + (power, _unit, nextUnit) => { + // Arrange + const bytes = Math.pow(1000, power); + + // Act + const result = humanFileSize(bytes); + + // Assert + expect(result).toBe(`1.0 ${nextUnit}`); + } + ); + it('should format 1000^9 B or 1000 YB with 1000 threshhold as 1000.0 YB', () => { + // Arrange + const bytes = Math.pow(1000, 9); + + // Act + const result = humanFileSize(bytes); + + // Assert + expect(result).toBe('1000.0 YB'); + }); + it('should format 1000 B with 1000 threshhold and 0 decimal places as 1 KB', () => { + // Arrange + const bytes = 1000; + + // Act + const result = humanFileSize(bytes, true, 0); + + // Assert + expect(result).toBe('1 KB'); + }); + it('should format 1000 B with 1000 threshhold and 4 decimal places as 1.0000 KB', () => { + // Arrange + const bytes = 1000; + + // Act + const result = humanFileSize(bytes, true, 4); + + // Assert + expect(result).toBe('1.0000 KB'); + }); +}); diff --git a/src/tools/_tests/language.spec.ts b/src/tools/_tests/language.spec.ts new file mode 100644 index 000000000..a510514a7 --- /dev/null +++ b/src/tools/_tests/language.spec.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from 'vitest'; + +import { getLanguageByCode } from '../language'; + +describe('getLanguageByCode', () => { + it('should return Vietnamese for code vi', () => { + // Arrange + const code = 'vi'; + // Act + const result = getLanguageByCode(code); + // Assert + expect(result.translatedName).toBe('Vietnamese'); + }); + it('should return English as fallback for code null', () => { + // Arrange + const code = null; + // Act + const result = getLanguageByCode(code); + // Assert + expect(result.translatedName).toBe('English'); + }); +}); diff --git a/src/tools/humanFileSize.ts b/src/tools/humanFileSize.ts index dfd6c9c1f..aaf0d68e6 100644 --- a/src/tools/humanFileSize.ts +++ b/src/tools/humanFileSize.ts @@ -2,30 +2,30 @@ * Format bytes as human-readable text. * * @param bytes Number of bytes. - * @param si True to use metric (SI) units, aka powers of 1000. False to use + * @param use1024Threshhold True to use metric (SI) units, aka powers of 1000. False to use * binary (IEC), aka powers of 1024. - * @param dp Number of decimal places to display. + * @param decimalPlaces Number of decimal places to display. * * @return Formatted string. */ -export function humanFileSize(initialBytes: number, si = true, dp = 1) { - const thresh = si ? 1000 : 1024; +export function humanFileSize(initialBytes: number, use1024Threshhold = true, decimalPlaces = 1) { + const thresh = use1024Threshhold ? 1000 : 1024; let bytes = initialBytes; if (Math.abs(bytes) < thresh) { return `${bytes} B`; } - const units = si - ? ['kb', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] + const units = use1024Threshhold + ? ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; let u = -1; - const r = 10 ** dp; + const r = 10 ** decimalPlaces; do { bytes /= thresh; u += 1; } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1); - return `${bytes.toFixed(dp)} ${units[u]}`; + return `${bytes.toFixed(decimalPlaces)} ${units[u]}`; } diff --git a/src/tools/language.ts b/src/tools/language.ts index fa07c0678..69cabde0e 100644 --- a/src/tools/language.ts +++ b/src/tools/language.ts @@ -13,7 +13,7 @@ export type Language = { locale: string; }; -export const languages: Language[] = [ +export const languages = [ { shortName: 'de', originalName: 'Deutsch', @@ -93,6 +93,7 @@ export const languages: Language[] = [ originalName: 'LOLCAT', translatedName: 'LOLCAT', emoji: '🐱', + country: 'LOL', locale: 'en-gb', }, // Norwegian @@ -224,9 +225,11 @@ export const languages: Language[] = [ originalName: 'Magyar', translatedName: 'Hungarian', emoji: '🇭🇺', + country: 'HU', locale: 'hu', }, -]; +] as const satisfies Readonly; export const getLanguageByCode = (code: string | null) => - languages.find((language) => language.shortName === code) ?? languages[languages.length - 1]; + languages.find((language) => language.shortName === code) ?? + languages.find((x) => x.locale === 'en-gb')!;