aboutsummaryrefslogtreecommitdiff
path: root/test/internal
diff options
context:
space:
mode:
authorDivisionByZero <[email protected]>2024-09-26 17:50:05 +0200
committerGitHub <[email protected]>2024-09-26 17:50:05 +0200
commit9537dfddba882bd93d9a429697fd44bc72428426 (patch)
treef60b5b694d5e4e8ddab228aeb20c7b7746685edc /test/internal
parent424b120a4d94b15b6e77c04a0aaffd0016a9c870 (diff)
downloadfaker-9537dfddba882bd93d9a429697fd44bc72428426.tar.xz
faker-9537dfddba882bd93d9a429697fd44bc72428426.zip
infra: update file structure for util/internal (#3141)
Diffstat (limited to 'test/internal')
-rw-r--r--test/internal/__snapshots__/mersenne.spec.ts.snap25
-rw-r--r--test/internal/locale-proxy.spec.ts197
-rw-r--r--test/internal/mersenne-test-utils.ts17
-rw-r--r--test/internal/mersenne.spec.ts140
4 files changed, 197 insertions, 182 deletions
diff --git a/test/internal/__snapshots__/mersenne.spec.ts.snap b/test/internal/__snapshots__/mersenne.spec.ts.snap
deleted file mode 100644
index c045e2e7..00000000
--- a/test/internal/__snapshots__/mersenne.spec.ts.snap
+++ /dev/null
@@ -1,25 +0,0 @@
-// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
-
-exports[`generateMersenne32Randomizer() > seed: [42,1,2] > should return deterministic value for next() 1`] = `0.8562037434894592`;
-
-exports[`generateMersenne32Randomizer() > seed: [1211,1,2] > should return deterministic value for next() 1`] = `0.8916433283593506`;
-
-exports[`generateMersenne32Randomizer() > seed: [1337,1,2] > should return deterministic value for next() 1`] = `0.17990487208589911`;
-
-exports[`generateMersenne32Randomizer() > seed: 42 > should return deterministic value for next() 1`] = `0.37454011430963874`;
-
-exports[`generateMersenne32Randomizer() > seed: 1211 > should return deterministic value for next() 1`] = `0.9285201537422836`;
-
-exports[`generateMersenne32Randomizer() > seed: 1337 > should return deterministic value for next() 1`] = `0.2620246761944145`;
-
-exports[`generateMersenne53Randomizer() > seed: [42,1,2] > should return deterministic value for next() 1`] = `0.8562037477947296`;
-
-exports[`generateMersenne53Randomizer() > seed: [1211,1,2] > should return deterministic value for next() 1`] = `0.8916433279801969`;
-
-exports[`generateMersenne53Randomizer() > seed: [1337,1,2] > should return deterministic value for next() 1`] = `0.17990487224060836`;
-
-exports[`generateMersenne53Randomizer() > seed: 42 > should return deterministic value for next() 1`] = `0.3745401188473625`;
-
-exports[`generateMersenne53Randomizer() > seed: 1211 > should return deterministic value for next() 1`] = `0.9285201539025842`;
-
-exports[`generateMersenne53Randomizer() > seed: 1337 > should return deterministic value for next() 1`] = `0.2620246750155817`;
diff --git a/test/internal/locale-proxy.spec.ts b/test/internal/locale-proxy.spec.ts
new file mode 100644
index 00000000..1b6e060b
--- /dev/null
+++ b/test/internal/locale-proxy.spec.ts
@@ -0,0 +1,197 @@
+import { describe, expect, it } from 'vitest';
+import { FakerError, en } from '../../src';
+import { createLocaleProxy } from '../../src/internal/locale-proxy';
+
+describe('LocaleProxy', () => {
+ const locale = createLocaleProxy(en);
+ const enAirline = en.airline ?? { never: 'missing' };
+
+ describe('locale', () => {
+ it('should be possible to use equals on locale', () => {
+ expect(locale).toEqual(createLocaleProxy(en));
+ });
+
+ it('should be possible to use not equals on locale', () => {
+ expect(locale).not.toEqual(createLocaleProxy({}));
+ });
+ });
+
+ describe('category', () => {
+ it('should be possible to check for a missing category', () => {
+ expect('category' in locale).toBe(true);
+ });
+
+ it('should be possible to check for an existing category', () => {
+ expect('airline' in locale).toBe(true);
+ });
+
+ it('should be possible to access the title', () => {
+ expect(locale.metadata.title).toBe('English');
+ });
+
+ it('should be possible to access a missing category', () => {
+ expect(locale.category).toBeDefined();
+ });
+
+ it('should not be possible to add a new category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ locale.category = {};
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should not be possible to replace a category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ locale.airline = {};
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should not be possible to delete a missing category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ delete locale.category;
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should not be possible to delete an existing category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ delete locale.airline;
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should be possible to get all categories keys on empty locale', () => {
+ const empty = createLocaleProxy({});
+
+ expect(Object.keys(empty)).toEqual([]);
+ });
+
+ it('should be possible to get all categories keys on actual locale', () => {
+ expect(Object.keys(locale).sort()).toEqual(Object.keys(en).sort());
+ });
+ });
+
+ describe('entry', () => {
+ it('should be possible to check for a missing entry in a missing category', () => {
+ expect('missing' in locale.category).toBe(false);
+ });
+
+ it('should be possible to check for a missing entry in a present category', () => {
+ expect('missing' in locale.airline).toBe(false);
+ });
+
+ it('should be possible to check for a present entry', () => {
+ expect('airline' in locale.airline).toBe(true);
+ });
+
+ it('should not be possible to access a missing entry in a missing category', () => {
+ expect(() => locale.category.missing).toThrow(
+ new FakerError(
+ `The locale data for 'category.missing' are missing in this locale.
+ Please contribute the missing data to the project or use a locale/Faker instance that has these data.
+ For more information see https://fakerjs.dev/guide/localization.html`
+ )
+ );
+ });
+
+ it('should not be possible to access a missing entry in a present category', () => {
+ expect(() => locale.airline.missing).toThrow(
+ new FakerError(
+ `The locale data for 'airline.missing' are missing in this locale.
+ Please contribute the missing data to the project or use a locale/Faker instance that has these data.
+ For more information see https://fakerjs.dev/guide/localization.html`
+ )
+ );
+ });
+
+ it('should be possible to access a present entry', () => {
+ expect(locale.airline.airline).toBeDefined();
+ });
+
+ it('should not be possible to access an unavailable entry in a present category', () => {
+ const unavailable = createLocaleProxy({
+ airline: { airline: null },
+ });
+
+ expect(() => unavailable.airline.airline).toThrow(
+ new FakerError(
+ `The locale data for 'airline.airline' aren't applicable to this locale.
+ If you think this is a bug, please report it at: https://github.com/faker-js/faker`
+ )
+ );
+ });
+
+ it('should not be possible to add a new entry in a missing category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ locale.category.missing = {};
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should not be possible to add a new entry in an existing category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ locale.airline.missing = {};
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should not be possible to replace an entry in an existing category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ locale.airline.airline = ['dummy'];
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should not be possible to delete a missing entry in a missing category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ delete locale.category.missing;
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should not be possible to delete a missing entry in an existing category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ delete locale.airline.missing;
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should not be possible to delete an existing entry in an existing category', () => {
+ expect(() => {
+ // @ts-expect-error: LocaleProxy is read-only.
+ delete locale.airline.airline;
+ }).toThrow(
+ new FakerError('You cannot edit the locale data on the faker instance')
+ );
+ });
+
+ it('should be possible to get all keys from missing category', () => {
+ expect(Object.keys(locale.missing)).toEqual([]);
+ });
+
+ it('should be possible to get all keys from existing category', () => {
+ expect(Object.keys(locale.airline).sort()).toEqual(
+ Object.keys(enAirline).sort()
+ );
+ });
+ });
+});
diff --git a/test/internal/mersenne-test-utils.ts b/test/internal/mersenne-test-utils.ts
deleted file mode 100644
index ee7a43da..00000000
--- a/test/internal/mersenne-test-utils.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-// Moved to a separate file to avoid importing the tests
-
-/**
- * The maximum value that can be returned by `MersenneTwister19937.genrandReal2()`.
- * This is the max possible value with 32 bits of precision that is less than 1.
- */
-export const TWISTER_32CO_MAX_VALUE = 0.9999999997671694;
-/**
- * The maximum value that can be returned by `MersenneTwister19937.genrandRes53()`.
- * This is the max possible value with 53 bits of precision that is less than 1.
- */
-export const TWISTER_53CO_MAX_VALUE = 0.9999999999999999;
-// Re-exported because the value might change in the future
-/**
- * The maximum value that can be returned by `next()`.
- */
-export const MERSENNE_MAX_VALUE = TWISTER_32CO_MAX_VALUE;
diff --git a/test/internal/mersenne.spec.ts b/test/internal/mersenne.spec.ts
deleted file mode 100644
index f0e48bd0..00000000
--- a/test/internal/mersenne.spec.ts
+++ /dev/null
@@ -1,140 +0,0 @@
-import { beforeAll, beforeEach, describe, expect, it } from 'vitest';
-import {
- MersenneTwister19937,
- generateMersenne32Randomizer,
- generateMersenne53Randomizer,
-} from '../../src/internal/mersenne';
-import type { Randomizer } from '../../src/randomizer';
-import { seededRuns } from '../support/seeded-runs';
-import { times } from '../support/times';
-import {
- MERSENNE_MAX_VALUE,
- TWISTER_32CO_MAX_VALUE,
- TWISTER_53CO_MAX_VALUE,
-} from './mersenne-test-utils';
-
-const NON_SEEDED_BASED_RUN = 25;
-
-function newTwister(
- seed: number = Math.random() * Number.MAX_SAFE_INTEGER
-): MersenneTwister19937 {
- const twister = new MersenneTwister19937();
- twister.initGenrand(seed);
- return twister;
-}
-
-describe('MersenneTwister19937', () => {
- describe('genrandInt32()', () => {
- it('should be able to return 0', () => {
- const twister = newTwister(257678572);
-
- // There is no single value seed that can produce 0 in the first call
- for (let i = 0; i < 5; i++) {
- twister.genrandInt32();
- }
-
- const actual = twister.genrandInt32();
- expect(actual).toBe(0);
- });
-
- it('should be able to return 2^32-1', () => {
- const twister = newTwister(2855577693);
- const actual = twister.genrandInt32();
- expect(actual).toBe(2 ** 32 - 1);
- });
- });
-
- describe('genrandReal2()', () => {
- it('should be able to return 0', () => {
- const twister = newTwister();
- // shortcut to return minimal value
- // the test above shows that it is possible to return 0
- twister.genrandInt32 = () => 0;
- const actual = twister.genrandReal2();
- expect(actual).toBe(0);
- });
-
- it('should be able to return almost 1', () => {
- const twister = newTwister();
- // shortcut to return maximal value
- // the test above shows that it is possible to return 2^32-1
- twister.genrandInt32 = () => 2 ** 32 - 1;
- const actual = twister.genrandReal2();
- expect(actual).toBe(TWISTER_32CO_MAX_VALUE);
- });
- });
-
- describe('genrandRes53()', () => {
- it('should be able to return 0', () => {
- const twister = newTwister();
- // shortcut to return minimal value
- // the test above shows that it is possible to return 0
- twister.genrandInt32 = () => 0;
- const actual = twister.genrandRes53();
- expect(actual).toBe(0);
- });
-
- it('should be able to return almost 1', () => {
- const twister = newTwister();
- // shortcut to return maximal value
- // the test above shows that it is possible to return 2^32-1
- twister.genrandInt32 = () => 2 ** 32 - 1;
- const actual = twister.genrandRes53();
- expect(actual).toBe(TWISTER_53CO_MAX_VALUE);
- });
- });
-});
-
-describe.each([
- ['generateMersenne32Randomizer()', generateMersenne32Randomizer],
- ['generateMersenne53Randomizer()', generateMersenne53Randomizer],
-])('%s', (_, factory) => {
- const randomizer: Randomizer = factory();
-
- it('should return a result matching the interface', () => {
- expect(randomizer).toBeDefined();
- expect(randomizer).toBeTypeOf('object');
- expect(randomizer.next).toBeTypeOf('function');
- expect(randomizer.seed).toBeTypeOf('function');
- });
-
- describe.each(
- [...seededRuns, ...seededRuns.map((v) => [v, 1, 2])].map((v) => [v])
- )('seed: %j', (seed) => {
- beforeEach(() => {
- randomizer.seed(seed);
- });
-
- it('should return deterministic value for next()', () => {
- const actual = randomizer.next();
-
- expect(actual).toMatchSnapshot();
- });
- });
-
- function randomSeed(): number {
- return Math.ceil(Math.random() * 1_000_000_000);
- }
-
- // Create and log-back the seed for debug purposes
- describe.each(
- times(NON_SEEDED_BASED_RUN).flatMap(() => [
- [randomSeed()],
- [[randomSeed(), randomSeed()]],
- ])
- )('random seeded tests %j', (seed) => {
- beforeAll(() => {
- randomizer.seed(seed);
- });
-
- describe('next', () => {
- it('should return random number from interval [0, 1)', () => {
- const actual = randomizer.next();
-
- expect(actual).toBeGreaterThanOrEqual(0);
- expect(actual).toBeLessThanOrEqual(MERSENNE_MAX_VALUE);
- expect(actual).toBeLessThan(1);
- });
- });
- });
-});