From 0f0cba63f7b7e332f5553c3e419588bc4d93ef1d Mon Sep 17 00:00:00 2001 From: Kristjan Tammekivi Date: Fri, 30 Jun 2023 12:49:50 +0300 Subject: [PATCH 1/3] feat: make literals work with null --- src/module.ts | 2 +- src/programmers/LiteralsProgrammer.ts | 6 ++++-- test/features/literals/test_literals_null.ts | 13 +++++++++++++ test/features/literals/test_literals_string.ts | 10 ++++++++++ test/internal/_test_literals.ts | 7 +++++++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/features/literals/test_literals_null.ts create mode 100644 test/features/literals/test_literals_string.ts create mode 100644 test/internal/_test_literals.ts diff --git a/src/module.ts b/src/module.ts index 15c98339b6..032a13a177 100644 --- a/src/module.ts +++ b/src/module.ts @@ -971,7 +971,7 @@ export function literals(): never; * * @author Jeongho Nam - https://github.com/samchon */ -export function literals(): T[]; +export function literals(): T[]; /** * @internal diff --git a/src/programmers/LiteralsProgrammer.ts b/src/programmers/LiteralsProgrammer.ts index f746f7c2bf..e37f98e839 100644 --- a/src/programmers/LiteralsProgrammer.ts +++ b/src/programmers/LiteralsProgrammer.ts @@ -28,16 +28,18 @@ export namespace LiteralsProgrammer { throw new Error(ErrorMessages.ONLY); }, })(new MetadataCollection())(type); - const values: Set = new Set([ + const values: Set = new Set([ ...ArrayUtil.flat(meta.constants.map((c) => c.values)), ...(meta.atomics.filter((type) => type === "boolean").length ? [true, false] : []), + ...meta.nullable ? [null] : [] ]); return ts.factory.createAsExpression( ts.factory.createArrayLiteralExpression( [...values].map((v) => - typeof v === "boolean" + v === null ? ts.factory.createNull() + : typeof v === "boolean" ? v ? ts.factory.createTrue() : ts.factory.createFalse() diff --git a/test/features/literals/test_literals_null.ts b/test/features/literals/test_literals_null.ts new file mode 100644 index 0000000000..523270500f --- /dev/null +++ b/test/features/literals/test_literals_null.ts @@ -0,0 +1,13 @@ +import typia from '../../../src'; +import { _test_literals } from '../../internal/_test_literals'; + +const areArraysEqual = (a: T[], b: T[]): boolean => a.length === b.length && a.every((elem, i) => elem === b[i]); + +export const test_literals_string = _test_literals( + 'ArrayAtomicAlias', + () => 'A' as 'A' | 'B' | null, + (input) => { + const generatedArray = typia.literals(); + return areArraysEqual(generatedArray, ['A', 'B', null]); + } +); \ No newline at end of file diff --git a/test/features/literals/test_literals_string.ts b/test/features/literals/test_literals_string.ts new file mode 100644 index 0000000000..fd0e8d00c4 --- /dev/null +++ b/test/features/literals/test_literals_string.ts @@ -0,0 +1,10 @@ +import typia from '../../../src'; +import { _test_literals } from '../../internal/_test_literals'; + +const areArraysEqual = (a: T[], b: T[]): boolean => a.length === b.length && a.every((elem, i) => elem === b[i]); + +export const test_literals_string = _test_literals( + 'ArrayAtomicAlias', + () => 'A' as 'A' | 'B', + (input) => areArraysEqual(typia.literals(), ['A', 'B']) +); diff --git a/test/internal/_test_literals.ts b/test/internal/_test_literals.ts new file mode 100644 index 0000000000..22fe3e5e08 --- /dev/null +++ b/test/internal/_test_literals.ts @@ -0,0 +1,7 @@ +import { Spoiler } from '../helpers/Spoiler'; + +export function _test_literals(name: string, generator: () => T, validator: (input: T) => boolean): () => void { + return () => { + if (validator(generator()) === false) throw new Error(`Bug on typia.literals(): array is different than expected`); + }; +} From b25de4c5883299a61646b20803967bb4eef8a964 Mon Sep 17 00:00:00 2001 From: Kristjan Tammekivi Date: Fri, 30 Jun 2023 12:57:06 +0300 Subject: [PATCH 2/3] test: use the name property in literals test --- test/features/literals/test_literals_null.ts | 2 +- test/features/literals/test_literals_string.ts | 2 +- test/internal/_test_literals.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/features/literals/test_literals_null.ts b/test/features/literals/test_literals_null.ts index 523270500f..43a8300e01 100644 --- a/test/features/literals/test_literals_null.ts +++ b/test/features/literals/test_literals_null.ts @@ -4,7 +4,7 @@ import { _test_literals } from '../../internal/_test_literals'; const areArraysEqual = (a: T[], b: T[]): boolean => a.length === b.length && a.every((elem, i) => elem === b[i]); export const test_literals_string = _test_literals( - 'ArrayAtomicAlias', + 'LiteralsWithNull', () => 'A' as 'A' | 'B' | null, (input) => { const generatedArray = typia.literals(); diff --git a/test/features/literals/test_literals_string.ts b/test/features/literals/test_literals_string.ts index fd0e8d00c4..e76734725d 100644 --- a/test/features/literals/test_literals_string.ts +++ b/test/features/literals/test_literals_string.ts @@ -4,7 +4,7 @@ import { _test_literals } from '../../internal/_test_literals'; const areArraysEqual = (a: T[], b: T[]): boolean => a.length === b.length && a.every((elem, i) => elem === b[i]); export const test_literals_string = _test_literals( - 'ArrayAtomicAlias', + 'LiteralsWithStrings', () => 'A' as 'A' | 'B', (input) => areArraysEqual(typia.literals(), ['A', 'B']) ); diff --git a/test/internal/_test_literals.ts b/test/internal/_test_literals.ts index 22fe3e5e08..983bb8142d 100644 --- a/test/internal/_test_literals.ts +++ b/test/internal/_test_literals.ts @@ -2,6 +2,6 @@ import { Spoiler } from '../helpers/Spoiler'; export function _test_literals(name: string, generator: () => T, validator: (input: T) => boolean): () => void { return () => { - if (validator(generator()) === false) throw new Error(`Bug on typia.literals(): array is different than expected`); + if (validator(generator()) === false) throw new Error(`Bug on typia.literals() for ${ name }: array is different than expected`); }; } From 6bb3f0d646c464a89b2478f7b5345ec4be0c1728 Mon Sep 17 00:00:00 2001 From: Kristjan Tammekivi Date: Fri, 30 Jun 2023 13:07:40 +0300 Subject: [PATCH 3/3] style: rename tests for literals --- .../{test_literals_null.ts => test_literals_WithNull.ts} | 4 ++-- .../{test_literals_string.ts => test_literals_WithStrings.ts} | 4 ++-- test/internal/_test_literals.ts | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) rename test/features/literals/{test_literals_null.ts => test_literals_WithNull.ts} (84%) rename test/features/literals/{test_literals_string.ts => test_literals_WithStrings.ts} (80%) diff --git a/test/features/literals/test_literals_null.ts b/test/features/literals/test_literals_WithNull.ts similarity index 84% rename from test/features/literals/test_literals_null.ts rename to test/features/literals/test_literals_WithNull.ts index 43a8300e01..5b28604e59 100644 --- a/test/features/literals/test_literals_null.ts +++ b/test/features/literals/test_literals_WithNull.ts @@ -3,8 +3,8 @@ import { _test_literals } from '../../internal/_test_literals'; const areArraysEqual = (a: T[], b: T[]): boolean => a.length === b.length && a.every((elem, i) => elem === b[i]); -export const test_literals_string = _test_literals( - 'LiteralsWithNull', +export const test_literals_WithNull = _test_literals( + 'WithNull', () => 'A' as 'A' | 'B' | null, (input) => { const generatedArray = typia.literals(); diff --git a/test/features/literals/test_literals_string.ts b/test/features/literals/test_literals_WithStrings.ts similarity index 80% rename from test/features/literals/test_literals_string.ts rename to test/features/literals/test_literals_WithStrings.ts index e76734725d..e8a9fd15ad 100644 --- a/test/features/literals/test_literals_string.ts +++ b/test/features/literals/test_literals_WithStrings.ts @@ -3,8 +3,8 @@ import { _test_literals } from '../../internal/_test_literals'; const areArraysEqual = (a: T[], b: T[]): boolean => a.length === b.length && a.every((elem, i) => elem === b[i]); -export const test_literals_string = _test_literals( - 'LiteralsWithStrings', +export const test_literals_WithStrings = _test_literals( + 'WithStrings', () => 'A' as 'A' | 'B', (input) => areArraysEqual(typia.literals(), ['A', 'B']) ); diff --git a/test/internal/_test_literals.ts b/test/internal/_test_literals.ts index 983bb8142d..35e988df7a 100644 --- a/test/internal/_test_literals.ts +++ b/test/internal/_test_literals.ts @@ -1,5 +1,3 @@ -import { Spoiler } from '../helpers/Spoiler'; - export function _test_literals(name: string, generator: () => T, validator: (input: T) => boolean): () => void { return () => { if (validator(generator()) === false) throw new Error(`Bug on typia.literals() for ${ name }: array is different than expected`);