This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathassertType.ts
61 lines (54 loc) · 1.52 KB
/
assertType.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import AssertionError = require('assertion-error');
import { TypeWriterResult } from './TypeWriter';
import { find } from '@dojo/shim/array';
function assert(expr: any, message?: string, expected?: any, actual?: any, showDiff?: boolean, ssi?: any): void {
if (!expr) {
throw new AssertionError(
message || '',
{
actual,
expected,
showDiff
},
ssi
);
}
}
function contains(typeBundle: TypeWriterResult[], name: string, description?: string): void {
assert(typeBundle.some((result) => result.sourceText === name), description);
}
function isType(
typeBundle: TypeWriterResult[],
name: string,
type: string,
description?: string,
ssi: any = isType
): void {
const typeResult = find(typeBundle, (result) => result.sourceText === name);
assert(
typeResult && typeResult.type === type,
`Unexpected type. Expected: "${type}" Actual: "${typeResult && typeResult.type}"${
description ? `. ${description}` : ''
}`,
type,
typeResult && typeResult.type,
true,
ssi
);
}
function isBoolean(typeBundle: TypeWriterResult[], name: string, description?: string): void {
isType(typeBundle, name, 'boolean', description, isBoolean);
}
function isString(typeBundle: TypeWriterResult[], name: string, description?: string): void {
isType(typeBundle, name, 'string', description, isString);
}
function isError(typeBundle: TypeWriterResult[], name: string, description?: string): void {
isType(typeBundle, name, 'Error', description, isError);
}
export default {
contains,
isBoolean,
isError,
isString,
isType
};