Skip to content

Commit 1759ade

Browse files
test(mapper): tests for mapper/util detectType(val)
1 parent a27e8e7 commit 1759ade

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/mapper/util.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { NullType } from './type/null.type'
33
import { UndefinedType } from './type/undefined.type'
44
import {
55
detectCollectionTypeFromValue,
6+
detectType,
67
isCollection,
78
isHomogeneous,
9+
isNode,
810
isSet,
911
typeName,
1012
typeOf,
@@ -88,6 +90,49 @@ describe('Util', () => {
8890
})
8991
})
9092

93+
describe('detect type', () => {
94+
it('detects string', () => {
95+
expect(detectType('aString')).toBe('S')
96+
expect(detectType(String('aString'))).toBe('S')
97+
// tslint:disable-next-line:no-construct
98+
expect(detectType(new String('aString'))).toBe('S')
99+
})
100+
it('detects number', () => {
101+
expect(detectType(3)).toBe('N')
102+
expect(detectType(Number(-5))).toBe('N')
103+
// tslint:disable-next-line:no-construct
104+
expect(detectType(new Number(83))).toBe('N')
105+
})
106+
it('detects binary', () => {
107+
let buffer: any
108+
if (isNode()) {
109+
buffer = Buffer.alloc(5)
110+
} else {
111+
buffer = new ArrayBuffer(8)
112+
}
113+
expect(detectType(buffer)).toBe('B')
114+
})
115+
it('detects null', () => {
116+
expect(detectType(null)).toBe('NULL')
117+
})
118+
it('detects bool', () => {
119+
expect(detectType(true)).toBe('BOOL')
120+
expect(detectType(false)).toBe('BOOL')
121+
})
122+
it('detects collection', () => {
123+
expect(detectType(new Set(['a']))).toBe('SS')
124+
expect(detectType(new Set([2]))).toBe('NS')
125+
expect(detectType([0, 1, 1, 2, 3, 5])).toBe('L')
126+
})
127+
it('detects object', () => {
128+
expect(detectType({})).toBe('M')
129+
expect(detectType({ foo: 'bar' })).toBe('M')
130+
})
131+
it('throws if not such a type', () => {
132+
expect(() => detectType(undefined)).toThrow()
133+
})
134+
})
135+
91136
describe('type name', () => {
92137
it('String', () => {
93138
expect(typeName(String)).toBe('String')

0 commit comments

Comments
 (0)