Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechpavlu committed Mar 16, 2024
1 parent a526697 commit 4393844
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
26 changes: 18 additions & 8 deletions src/baseGenerators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ import {
* @template ValueType Type of the value to be generated using the built value generator
* @template Conf Type of the configuration to be passed to the value generator
*/
export type ValueGeneratorBuilder<ValueType extends GeneratedValue, Conf extends ValueGeneratorConfig> =
(config: Conf) => ValueGenerator<ValueType, Conf>;
export type ValueGeneratorBuilder<
ValueType extends GeneratedValue,
Conf extends ValueGeneratorConfig,
> = (config: Conf) => ValueGenerator<ValueType, Conf>;

/**
* Type definition of the Registry of all the value generators.
*
* @template ValueType Type of the value to be generated using the built value generator
* @template Conf Type of the configuration to be passed to the value generator
*/
interface ValueGeneratorRegistry<ValueType extends GeneratedValue, Conf extends ValueGeneratorConfig> {
interface ValueGeneratorRegistry<
ValueType extends GeneratedValue,
Conf extends ValueGeneratorConfig,
> {
[name: string]: ValueGeneratorBuilder<ValueType, Conf>;
}

Expand All @@ -44,7 +49,7 @@ export const get = <
Conf extends ValueGeneratorConfig,
>(
name: string,
config: Conf
config: Conf,
): ValueGenerator<GenValue, Conf> => {
const generatorBuilderFunction = VALUE_GENERATOR_REGISTRY[name];

Expand All @@ -55,7 +60,6 @@ export const get = <
return generatorBuilderFunction(config) as ValueGenerator<GenValue, Conf>;
};


/**
* Access method to check there is a value generator builder
* assigned to the given name.
Expand All @@ -75,10 +79,16 @@ export const hasGenerator = (name: string): boolean => {
* @param builder Value Generator builder to be assigned under the given name
*/
export const registerValueGenerator = <
GeneratorValueType extends GeneratedValue, GivenConfig extends ValueGeneratorConfig
>(name: string, builder: ValueGeneratorBuilder<GeneratorValueType, GivenConfig>) => {
GeneratorValueType extends GeneratedValue,
GivenConfig extends ValueGeneratorConfig,
>(
name: string,
builder: ValueGeneratorBuilder<GeneratorValueType, GivenConfig>,
) => {
if (hasGenerator(name)) {
throw new Error(`There is already one value generator with name '${name}' registered`);
throw new Error(
`There is already one value generator with name '${name}' registered`,
);
}

VALUE_GENERATOR_REGISTRY[name] = builder;
Expand Down
45 changes: 24 additions & 21 deletions test/baseGenerators/numeric/FloatGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
import { FloatGenerator, FloatGeneratorConfig, IntegerGenerator, IntegerGeneratorConfig } from '../../../src';
import {
FloatGenerator,
FloatGeneratorConfig,
} from '../../../src';

describe("FloatGenerator class", () => {
it("should return a float within a given range (without precision)", () => {
describe('FloatGenerator class', () => {
it('should return a float within a given range (without precision)', () => {
const conf: FloatGeneratorConfig = {
min: 0,
max: 3
}
max: 3,
};

const generator = new FloatGenerator(conf);

expect(generator.get()).toBeGreaterThanOrEqual(0)
expect(generator.get()).toBeLessThanOrEqual(3)
})
expect(generator.get()).toBeGreaterThanOrEqual(0);
expect(generator.get()).toBeLessThanOrEqual(3);
});

it("should return a float within a given range (with precision)", () => {
it('should return a float within a given range (with precision)', () => {
const conf: FloatGeneratorConfig = {
min: 0,
max: 3,
decimalDigits: 4
}
decimalDigits: 4,
};

const generator = new FloatGenerator(conf);

expect(generator.get()).toBeGreaterThanOrEqual(0)
expect(generator.get()).toBeLessThanOrEqual(3)
expect(`${generator.get()}`.split(".")[1]!.length).toBeLessThanOrEqual(4);
})
expect(generator.get()).toBeGreaterThanOrEqual(0);
expect(generator.get()).toBeLessThanOrEqual(3);
expect(`${generator.get()}`.split('.')[1]!.length).toBeLessThanOrEqual(4);
});

it("should trigger the pipe", () => {
it('should trigger the pipe', () => {
const conf: FloatGeneratorConfig = {
min: 0,
max: 3,
pipes: [() => "test"]
}
pipes: [() => 'test'],
};

const generator = new FloatGenerator(conf);
expect(generator.get()).toBe("test")
})
})
expect(generator.get()).toBe('test');
});
});
24 changes: 12 additions & 12 deletions test/baseGenerators/numeric/IntegerGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { IntegerGenerator, IntegerGeneratorConfig } from '../../../src';

describe("IntegerGenerator class", () => {
it("should return a integer number in a given range", () => {
describe('IntegerGenerator class', () => {
it('should return a integer number in a given range', () => {
const conf: IntegerGeneratorConfig = {
min: 0,
max: 3
}
max: 3,
};

const generator = new IntegerGenerator(conf);

expect(generator.get()).toBeGreaterThanOrEqual(0);
expect(generator.get()).toBeLessThanOrEqual(3)
})
expect(generator.get()).toBeLessThanOrEqual(3);
});

it("should trigger the pipe", () => {
it('should trigger the pipe', () => {
const conf: IntegerGeneratorConfig = {
min: 0,
max: 3,
pipes: [() => "test"]
}
pipes: [() => 'test'],
};

const generator = new IntegerGenerator(conf);
expect(generator.get()).toBe("test")
})
})
expect(generator.get()).toBe('test');
});
});

0 comments on commit 4393844

Please sign in to comment.