aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorHarsohail Brar <[email protected]>2022-05-22 13:14:09 -0600
committerGitHub <[email protected]>2022-05-22 21:14:09 +0200
commitbee6054f8da67e26dcfdf572103eebabbd6443c0 (patch)
tree2390cf171db82a39acea58b793f630a5034feb35 /test
parentbd4d3dbc5ca54983ad28745825e4767c1f291e60 (diff)
downloadfaker-bee6054f8da67e26dcfdf572103eebabbd6443c0.tar.xz
faker-bee6054f8da67e26dcfdf572103eebabbd6443c0.zip
feat: color module (#801)
Co-authored-by: Shinigami <[email protected]>
Diffstat (limited to 'test')
-rw-r--r--test/color.spec.ts475
-rw-r--r--test/commerce.spec.ts2
-rw-r--r--test/vehicle.spec.ts2
3 files changed, 477 insertions, 2 deletions
diff --git a/test/color.spec.ts b/test/color.spec.ts
new file mode 100644
index 00000000..9d438291
--- /dev/null
+++ b/test/color.spec.ts
@@ -0,0 +1,475 @@
+import { afterEach, describe, expect, it } from 'vitest';
+import { faker } from '../src';
+import { CSS_FUNCTIONS, CSS_SPACES } from '../src/modules/color';
+
+const seededRuns = [
+ {
+ seed: 42,
+ expectations: {
+ human: 'grey',
+ space: 'Rec. 709',
+ cssSupportedFunction: 'hsla',
+ cssSupportedSpace: 'rec2020',
+ rgb: '#8be4ab',
+ hsl: [135, 0.8, 0.96],
+ hwb: [135, 0.8, 0.96],
+ cmyk: [0.37, 0.8, 0.96, 0.18],
+ lab: [0.37454, 59.3086, 90.1429],
+ lch: [0.37454, 183.2, 218.7],
+ colorByCSSColorSpace: [0.3745, 0.7966, 0.9508],
+ },
+ },
+ {
+ seed: 1337,
+ expectations: {
+ human: 'black',
+ space: 'ProPhoto RGB Color Space',
+ cssSupportedFunction: 'hsl',
+ cssSupportedSpace: 'display-p3',
+ rgb: '#5c346b',
+ hsl: [94, 0.56, 0.16],
+ hwb: [94, 0.56, 0.16],
+ cmyk: [0.26, 0.56, 0.16, 0.21],
+ lab: [0.262024, 12.106, -68.2632],
+ lch: [0.262024, 128.9, 36.5],
+ colorByCSSColorSpace: [0.262, 0.5605, 0.1586],
+ },
+ },
+ {
+ seed: 1211,
+ expectations: {
+ human: 'azure',
+ space: 'LMS',
+ cssSupportedFunction: 'color',
+ cssSupportedSpace: 'rec2020',
+ rgb: '#eadb42',
+ hsl: [335, 0.46, 0.9],
+ hwb: [335, 0.46, 0.9],
+ cmyk: [0.93, 0.46, 0.9, 0.78],
+ lab: [0.928521, -8.197, 78.6944],
+ lch: [0.928521, 105.6, 205.5],
+ colorByCSSColorSpace: [0.9286, 0.459, 0.8935],
+ },
+ },
+];
+
+const NON_SEEDED_BASED_RUN = 5;
+
+const functionNames = [
+ 'human',
+ 'space',
+ 'cssSupportedFunction',
+ 'cssSupportedSpace',
+ 'rgb',
+ 'hsl',
+ 'hwb',
+ 'cmyk',
+ 'lab',
+ 'lch',
+ 'colorByCSSColorSpace',
+];
+
+describe('color', () => {
+ afterEach(() => {
+ faker.locale = 'en';
+ });
+
+ for (const { seed, expectations } of seededRuns) {
+ describe(`seed: ${seed}`, () => {
+ for (const functionName of functionNames) {
+ it(`${functionName}()`, () => {
+ faker.seed(seed);
+
+ const actual = faker.color[functionName]();
+ expect(actual).toEqual(expectations[functionName]);
+ });
+ }
+ });
+ }
+
+ // Create and log-back the seed for debug purposes
+ faker.seed(Math.ceil(Math.random() * 1_000_000_000));
+
+ describe(`random seeded tests for seed ${JSON.stringify(
+ faker.seed()
+ )}`, () => {
+ for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
+ describe(`human()`, () => {
+ it('should return random human readable color from human color array', () => {
+ const color = faker.color.human();
+ expect(faker.definitions.color.human).toContain(color);
+ });
+ });
+
+ describe(`space()`, () => {
+ it('should return random color space from color space array', () => {
+ const space = faker.color.space();
+ expect(faker.definitions.color.space).toContain(space);
+ });
+ });
+
+ describe(`cssSupportedFunction()`, () => {
+ it('should return random css supported color function from css functions array', () => {
+ const func = faker.color.cssSupportedFunction();
+ expect(CSS_FUNCTIONS).toContain(func);
+ });
+ });
+
+ describe(`cssSupportedSpace()`, () => {
+ it('should return random css supported color space from css spaces array', () => {
+ const space = faker.color.cssSupportedSpace();
+ expect(CSS_SPACES).toContain(space);
+ });
+ });
+
+ describe(`rgb()`, () => {
+ it('should return a random rgb hex color', () => {
+ const color = faker.color.rgb();
+ expect(color).match(/^(#[a-f0-9]{6})$/);
+ });
+ });
+
+ describe(`rgb({ prefix: '0x' })`, () => {
+ it('should return a random rgb hex color with # prefix', () => {
+ const color = faker.color.rgb({ prefix: '0x' });
+ expect(color).match(/^(0x[a-f0-9]{6})$/);
+ });
+ });
+
+ describe(`rgbHex({ prefix: '0x', case: 'lower' })`, () => {
+ it('should return a random rgb hex color with # prefix and lower case only', () => {
+ const color = faker.color.rgb({ prefix: '0x', casing: 'lower' });
+ expect(color).match(/^(0x[a-f0-9]{6})$/);
+ });
+ });
+
+ describe(`rgb({ prefix: '0x', case: 'upper' })`, () => {
+ it('should return a random rgb hex color with # prefix and upper case only', () => {
+ const color = faker.color.rgb({ prefix: '0x', casing: 'upper' });
+ expect(color).match(/^(0x[A-F0-9]{6})$/);
+ });
+ });
+
+ describe(`rgb({ format: 'decimal' })`, () => {
+ it('should return a random rgb color in decimal format', () => {
+ const color = faker.color.rgb({ format: 'decimal' });
+ expect(color).length(3);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(255);
+ });
+ });
+ });
+
+ describe(`rgb({ format: 'css' })`, () => {
+ it('should return a random rgb color in css format', () => {
+ const color = faker.color.rgb({ format: 'css' });
+ expect(color).match(/^(rgb\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}\))$/);
+ });
+ });
+
+ describe(`rgb({ format: 'binary' })`, () => {
+ it('should return a random rgb color in binary format', () => {
+ const color = faker.color.rgb({ format: 'binary' });
+ expect(color).match(/^([01]{8} [01]{8} [01]{8})$/);
+ });
+ });
+
+ describe(`rgb({ includeAlpha: true })`, () => {
+ it('should return a random rgb color in hex format with alpha value', () => {
+ const color = faker.color.rgb({ includeAlpha: true });
+ expect(color).match(/^(#[a-fA-F0-9]{8})$/);
+ });
+ });
+
+ describe(`rgb({ format: 'decimal', includeAlpha: true })`, () => {
+ it('should return a random rgb color in hex format with alpha value', () => {
+ const color = faker.color.rgb({
+ format: 'decimal',
+ includeAlpha: true,
+ });
+ expect(color[color.length - 1]).toBeGreaterThanOrEqual(0);
+ expect(color[color.length - 1]).toBeLessThanOrEqual(1);
+ color.slice(0, 4).forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(255);
+ });
+ });
+ });
+
+ describe(`rgb({ format: 'binary', includeAlpha: true })`, () => {
+ it('should return a random rgb color in binary format with alpha value', () => {
+ const color = faker.color.rgb({
+ format: 'binary',
+ includeAlpha: true,
+ });
+ expect(color).match(/^([01]{8} [01]{8} [01]{8} [01]{8,32})$/);
+ });
+ });
+
+ describe(`rgb({ format: 'css', includeAlpha: true })`, () => {
+ it('should return a random rgb color in css format with alpha value', () => {
+ const color = faker.color.rgb({ format: 'css', includeAlpha: true });
+ expect(color).match(
+ /^(rgba\([0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, \d*\.?\d*\))$/
+ );
+ });
+ });
+
+ describe(`cmyk()`, () => {
+ it('should return a random cmyk color', () => {
+ const color = faker.color.cmyk();
+ expect(color).length(4);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(1);
+ });
+ });
+ });
+
+ describe(`cmyk({ format: 'decimal' })`, () => {
+ it('should return a random cmyk color in decimal format', () => {
+ const color = faker.color.cmyk({ format: 'decimal' });
+ expect(color).length(4);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(1);
+ });
+ });
+ });
+
+ describe(`cmyk({ format: 'css' })`, () => {
+ it('should return a random cmyk color in css format', () => {
+ const color = faker.color.cmyk({ format: 'css' });
+ expect(color).match(
+ /^(cmyk\([0-9]{1,3}%, [0-9]{1,3}%, [0-9]{1,3}%, [0-9]{1,3}%\))$/
+ );
+ });
+ });
+
+ describe(`cmyk({ format: 'binary' })`, () => {
+ it('should return a random cmyk color in binary format', () => {
+ const color = faker.color.cmyk({ format: 'binary' });
+ expect(color).match(
+ /^([01]{8,32} [01]{8,32} [01]{8,32} [01]{8,32})$/
+ );
+ });
+ });
+
+ describe(`hsl()`, () => {
+ it('should return a random hsl color in decimal format', () => {
+ const color = faker.color.hsl();
+ expect(color).length(3);
+ expect(color[0]).toBeGreaterThanOrEqual(0);
+ expect(color[0]).toBeLessThanOrEqual(360);
+ color.slice(1).forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(1);
+ });
+ });
+ });
+
+ describe(`hsl({ format: 'css' })`, () => {
+ it('should return a random hsl color in css format', () => {
+ const color = faker.color.hsl({ format: 'css' });
+ expect(color).match(
+ /^(hsl\([0-9]{1,3}deg [0-9]{1,3}% [0-9]{1,3}%\))$/
+ );
+ });
+ });
+
+ describe(`hsl({ format: 'css', includeAlpha: true })`, () => {
+ it('should return a random hsl color in css format with an alpha value', () => {
+ const color = faker.color.hsl({ format: 'css', includeAlpha: true });
+ expect(color).match(
+ /^(hsl\([0-9]{1,3}deg [0-9]{1,3}% [0-9]{1,3}% \/ \d*\.?\d*\))$/
+ );
+ });
+ });
+
+ describe(`hsl({ format: 'binary' })`, () => {
+ it('should return a random hsl color in binary format', () => {
+ const color = faker.color.hsl({ format: 'binary' });
+ expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/);
+ });
+ });
+
+ describe(`hsl({ format: 'binary', includeAlpha: true })`, () => {
+ it('should return a random hsl color in binary format with an alpha value', () => {
+ const color = faker.color.hsl({
+ format: 'binary',
+ includeAlpha: true,
+ });
+ expect(color).match(
+ /^([01]{8,32} [01]{8,32} [01]{8,32} [01]{8,32})$/
+ );
+ });
+ });
+
+ describe(`hwb()`, () => {
+ it('should return a random hwb color in decimal format', () => {
+ const color = faker.color.hwb();
+ expect(color).length(3);
+ expect(color[0]).toBeGreaterThanOrEqual(0);
+ expect(color[0]).toBeLessThanOrEqual(360);
+ color.slice(1).forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(1);
+ });
+ });
+ });
+
+ describe(`hwb({ format: 'decimal' })`, () => {
+ it('should return a random hwb color in decimal format', () => {
+ const color = faker.color.hwb();
+ expect(color).length(3);
+ expect(color[0]).toBeGreaterThanOrEqual(0);
+ expect(color[0]).toBeLessThanOrEqual(360);
+ color.slice(1).forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(1);
+ });
+ });
+ });
+
+ describe(`hwb({ format: 'css' })`, () => {
+ it('should return a random hwb color in css format', () => {
+ const color = faker.color.hwb({ format: 'css' });
+ expect(color).match(/^(hwb\([0-9]{1,3} [0-9]{1,3}% [0-9]{1,3}%\))$/);
+ });
+ });
+
+ describe(`hwb({ format: 'binary' })`, () => {
+ it('should return a random hwb color in binary format', () => {
+ const color = faker.color.hwb({ format: 'binary' });
+ expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/);
+ });
+ });
+
+ describe(`lab()`, () => {
+ it('should return a random lab color in decimal format', () => {
+ const color = faker.color.lab();
+ expect(color).length(3);
+ expect(color[0]).toBeGreaterThanOrEqual(0);
+ expect(color[0]).toBeLessThanOrEqual(1);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(-100);
+ expect(value).toBeLessThanOrEqual(100);
+ });
+ });
+ });
+
+ describe(`lab({ format: 'decimal' })`, () => {
+ it('should return a random lab color in decimal format', () => {
+ const color = faker.color.lab({ format: 'decimal' });
+ expect(color).length(3);
+ expect(color[0]).toBeGreaterThanOrEqual(0);
+ expect(color[0]).toBeLessThanOrEqual(1);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(-100);
+ expect(value).toBeLessThanOrEqual(100);
+ });
+ });
+ });
+
+ describe(`lab({ format: 'css' })`, () => {
+ it('should return a random lab color in css format', () => {
+ const color = faker.color.lab({ format: 'css' });
+ expect(color).match(
+ /^(lab\((\d*\.?\d*|[0-9]{1,3})% -?\d*\.?\d* -?\d*\.?\d*\))$/
+ );
+ });
+ });
+
+ describe(`lab({ format: 'binary' })`, () => {
+ it('should return a random lab color in binary format', () => {
+ const color = faker.color.lab({ format: 'binary' });
+ expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/);
+ });
+ });
+
+ describe(`lch()`, () => {
+ it('should return a random lch color in decimal format', () => {
+ const color = faker.color.lch();
+ expect(color).length(3);
+ expect(color[0]).toBeGreaterThanOrEqual(0);
+ expect(color[0]).toBeLessThanOrEqual(1);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(230);
+ });
+ });
+ });
+
+ describe(`lch({ format: 'decimal' })`, () => {
+ it('should return a random lch color in decimal format', () => {
+ const color = faker.color.lch({ format: 'decimal' });
+ expect(color).length(3);
+ expect(color[0]).toBeGreaterThanOrEqual(0);
+ expect(color[0]).toBeLessThanOrEqual(1);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(230);
+ });
+ });
+ });
+
+ describe(`lch({ format: 'css' })`, () => {
+ it('should return a random lch color in css format', () => {
+ const color = faker.color.lch({ format: 'css' });
+ expect(color).match(
+ /^(lch\((\d*\.?\d*|[0-9]{1,3})% \d*\.?\d* \d*\.?\d*\))$/
+ );
+ });
+ });
+
+ describe(`lch({ format: 'binary' })`, () => {
+ it('should return a random lch color in binary format', () => {
+ const color = faker.color.lch({ format: 'binary' });
+ expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/);
+ });
+ });
+
+ describe(`colorByCSSColorSpace()`, () => {
+ it('should return a random color for a CSS color space in decimal format', () => {
+ const color = faker.color.colorByCSSColorSpace();
+ expect(color).length(3);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(1);
+ });
+ });
+ });
+
+ describe(`colorByCSSColorSpace({ format: 'decimal' })`, () => {
+ it('should return a random color for a CSS color space in decimal format', () => {
+ const color = faker.color.colorByCSSColorSpace({ format: 'decimal' });
+ expect(color).length(3);
+ color.forEach((value: number) => {
+ expect(value).toBeGreaterThanOrEqual(0);
+ expect(value).toBeLessThanOrEqual(1);
+ });
+ });
+ });
+
+ describe(`colorByCSSColorSpace({ format: 'css' })`, () => {
+ it('should return a random color for a CSS color space in css format', () => {
+ const color = faker.color.colorByCSSColorSpace({
+ format: 'css',
+ space: 'prophoto-rgb',
+ });
+ expect(color).match(
+ /^color\(prophoto-rgb \d*\.?\d* \d*\.?\d* \d*\.?\d*\)$/
+ );
+ });
+ });
+
+ describe(`colorByCSSColorSpace({ format: 'binary' })`, () => {
+ it('should return a random color for a CSS color space in binary format', () => {
+ const color = faker.color.colorByCSSColorSpace({ format: 'binary' });
+ expect(color).match(/^([01]{8,32} [01]{8,32} [01]{8,32})$/);
+ });
+ });
+ }
+ });
+});
diff --git a/test/commerce.spec.ts b/test/commerce.spec.ts
index 87385aa7..383b0a41 100644
--- a/test/commerce.spec.ts
+++ b/test/commerce.spec.ts
@@ -84,7 +84,7 @@ describe('commerce', () => {
describe(`color()`, () => {
it('should return random value from color array', () => {
const actual = faker.commerce.color();
- expect(faker.definitions.commerce.color).toContain(actual);
+ expect(faker.definitions.color.human).toContain(actual);
});
});
diff --git a/test/vehicle.spec.ts b/test/vehicle.spec.ts
index d6440814..a9c69b0a 100644
--- a/test/vehicle.spec.ts
+++ b/test/vehicle.spec.ts
@@ -161,7 +161,7 @@ describe('vehicle', () => {
expect(color).toBeTruthy();
expect(color).toBeTypeOf('string');
- expect(faker.definitions.commerce.color).toContain(color);
+ expect(faker.definitions.color.human).toContain(color);
});
});