Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken a big file into smaller files. Added .test suffix for test files. #208

Merged
merged 4 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"repository": "github:rhys-vdw/ts-auto-guard",
"main": "lib/index.js",
"scripts": {
"test": "cross-env NODE_ENV=test && npm run lint && npm run format:check && tape -r ts-node/register tests/**/*.ts | tap-diff",
"test": "cross-env NODE_ENV=test && npm run lint && npm run format:check && tape -r ts-node/register tests/**/*.test.ts | tap-diff",
"build": "tsc",
"prepare": "npm run build",
"lint": "eslint .",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { testProcessProject } from '../generate'

testProcessProject(
'adds type guard import to source file and also exports',
{
// NOTE: This file is not automatically cleaned up with `formatText` after
// being modified so it requires this funky indentation to ensure that it is
// conforms to ts-morph's formatting.
'test.ts': `
/** @see {isEmpty} ts-auto-guard:type-guard */
export interface Empty { }
`,
},
{
'test.ts': `
import * as CustomGuardAlias from "./test.guard";

/** @see {isEmpty} ts-auto-guard:type-guard */
export interface Empty {}
export { CustomGuardAlias };`,
'test.guard.ts': `
import { Empty } from "./test";

export function isEmpty(obj: unknown): obj is Empty {
const typedObj = obj as Empty
return (
(typedObj !== null &&
typeof typedObj === "object" ||
typeof typedObj === "function")
)
}`,
},
{ options: { importGuards: 'CustomGuardAlias' } }
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { testProcessProject } from '../generate'

testProcessProject(
'allows the name of the guard file file to be specified',
{
'test.ts': `
/** @see {isFoo} ts-auto-guard:type-guard */
export interface Foo {
foo: number,
bar: string
}`,
},
{
'test.ts': null,
'test.debug.ts': `
import { Foo } from "./test";

export function isFoo(obj: unknown): obj is Foo {
const typedObj = obj as Foo
return (
(typedObj !== null &&
typeof typedObj === "object" ||
typeof typedObj === "function") &&
typeof typedObj["foo"] === "number" &&
typeof typedObj["bar"] === "string"
)
}`,
},
{
options: {
guardFileName: 'debug',
},
}
)
41 changes: 41 additions & 0 deletions tests/features/any_and_unknown_work_in_interesction_types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { testProcessProject } from '../generate'

testProcessProject(
'any and unknown work in interesction types',
{
'test.ts': `
type anyType = any
type unknownType = unknown

export type AnyAndString = string & anyType
export type UnknownAndString = string & unknownType
export type AnyAndUnknownAndString = string & anyType & unknownType`,
},
{
'test.ts': null,
'test.guard.ts': `
import { AnyAndString, UnknownAndString, AnyAndUnknownAndString } from "./test";

export function isAnyAndString(obj: unknown): obj is AnyAndString {
const typedObj = obj as AnyAndString
return (
true
)
}

export function isUnknownAndString(obj: unknown): obj is UnknownAndString {
const typedObj = obj as UnknownAndString
return (
typeof typedObj === "string"
)
}

export function isAnyAndUnknownAndString(obj: unknown): obj is AnyAndUnknownAndString {
const typedObj = obj as AnyAndUnknownAndString
return (
true
)
}`,
},
{ options: { exportAll: true } }
)
41 changes: 41 additions & 0 deletions tests/features/any_and_unknown_work_in_union_types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { testProcessProject } from '../generate'

testProcessProject(
'any and unknown work in union types',
{
'test.ts': `
type anyType = any
type unknownType = unknown

export type AnyOrString = string | anyType
export type UnknownOrString = string | unknownType
export type AnyOrUnknownOrString = string | anyType | unknownType`,
},
{
'test.ts': null,
'test.guard.ts': `
import { AnyOrString, UnknownOrString, AnyOrUnknownOrString } from "./test";

export function isAnyOrString(obj: unknown): obj is AnyOrString {
const typedObj = obj as AnyOrString
return (
true
)
}

export function isUnknownOrString(obj: unknown): obj is UnknownOrString {
const typedObj = obj as UnknownOrString
return (
true
)
}

export function isAnyOrUnknownOrString(obj: unknown): obj is AnyOrUnknownOrString {
const typedObj = obj as AnyOrUnknownOrString
return (
true
)
}`,
},
{ options: { exportAll: true } }
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { testProcessProject } from '../generate'

testProcessProject(
'Check if any callable properties is a function',
// should also emit a warning about how it is not possible to check function type at runtime.
{
'test.ts': `
/** @see {isTestType} ts-auto-guard:type-guard */
export interface TestType {
test: (() => void)
// ts-auto-guard-suppress function-type
test2(someArg: number): boolean
// some other comments
test3: {
(someArg: string): number
test3Arg: number;
}
}
`,
},
{
'test.ts': null,
'test.guard.ts': `
import { TestType } from "./test";

export function isTestType(obj: unknown): obj is TestType {
const typedObj = obj as TestType
return (
(typedObj !== null &&
typeof typedObj === "object" ||
typeof typedObj === "function") &&
typeof typedObj["test"] === "function" &&
typeof typedObj["test3"] === "function" &&
typeof typedObj["test3"]["test3Arg"] === "number" &&
typeof typedObj["test2"] === "function"
)
}
`,
}
)
29 changes: 29 additions & 0 deletions tests/features/check_if_callable_interface_is_a_function.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { testProcessProject } from '../generate'

testProcessProject(
'Check if callable interface is a function',
// should also emit a warning about how it is not possible to check function type at runtime.
{
'test.ts': `
/** @see {isTestType} ts-auto-guard:type-guard */
export interface TestType {
(someArg: string): number
arg: number;
}
`,
},
{
'test.ts': null,
'test.guard.ts': `
import { TestType } from "./test";

export function isTestType(obj: unknown): obj is TestType {
const typedObj = obj as TestType
return (
typeof typedObj === "function" &&
typeof typedObj["arg"] === "number"
)
}
`,
}
)
31 changes: 31 additions & 0 deletions tests/features/correctly_handles_default_export.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { testProcessProject } from '../generate'

testProcessProject(
'correctly handles default export',
{
'test.ts': `
/** @see {isFoo} ts-auto-guard:type-guard */
interface Foo {
foo: number,
bar: string
}

export default Foo`,
},
{
'test.ts': null,
'test.guard.ts': `
import Foo from "./test";

export function isFoo(obj: unknown): obj is Foo {
const typedObj = obj as Foo
return (
(typedObj !== null &&
typeof typedObj === "object" ||
typeof typedObj === "function") &&
typeof typedObj["foo"] === "number" &&
typeof typedObj["bar"] === "string"
)
}`,
}
)
30 changes: 30 additions & 0 deletions tests/features/deals_with_unknown_type_as_it_would_any.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { testProcessProject } from '../generate'

testProcessProject(
'Deals with unknown type as it would any',
{
'test.ts': `
/** @see {isTestType} ts-auto-guard:type-guard */
export interface TestType {
[index: string]: unknown
}
`,
},
{
'test.ts': null,
'test.guard.ts': `
import { TestType } from "./test";

export function isTestType(obj: unknown): obj is TestType {
const typedObj = obj as TestType
return (
(typedObj !== null &&
typeof typedObj === "object" ||
typeof typedObj === "function") &&
Object.entries<any>(typedObj)
.every(([key, _value]) => (typeof key === "string"))
)
}
`,
}
)
9 changes: 9 additions & 0 deletions tests/features/does_not_generate_empty_guard_files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { testProcessProject } from '../generate'

testProcessProject(
'Does not generate empty guard files',
{
'test.ts': '',
},
{ 'test.ts': null }
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { testProcessProject } from '../generate'

testProcessProject(
'does not touch .guard.ts files that are not autogenerated',
{ 'test.guard.ts': `alert("hello")` },
{ 'test.guard.ts': null }
)
28 changes: 28 additions & 0 deletions tests/features/generated_type_guards_for_arrays_of_any.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { testProcessProject } from '../generate'

testProcessProject(
'generated type guards for arrays of any',
{
'test.ts': `
export interface Foo {
value: any[]
}
`,
},
{
'test.ts': null,
'test.guard.ts': `
import { Foo } from "./test";

export function isFoo(obj: unknown): obj is Foo {
const typedObj = obj as Foo
return (
(typedObj !== null &&
typeof typedObj === "object" ||
typeof typedObj === "function") &&
Array.isArray(typedObj["value"])
)
}`,
},
{ options: { exportAll: true } }
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { testProcessProject } from '../generate'

testProcessProject(
'generated type guards for discriminated unions',
{
'test.ts': `
export type X = { type: 'a', value: number } | { type: 'b', value: string }
`,
},
{
'test.ts': null,
'test.guard.ts': `
import { X } from "./test";

export function isX(obj: unknown): obj is X {
const typedObj = obj as X
return (
((typedObj !== null &&
typeof typedObj === "object" ||
typeof typedObj === "function") &&
typedObj["type"] === "a" &&
typeof typedObj["value"] === "number" ||
(typedObj !== null &&
typeof typedObj === "object" ||
typeof typedObj === "function") &&
typedObj["type"] === "b" &&
typeof typedObj["value"] === "string")
)
}`,
},
{ options: { exportAll: true } }
)
Loading