This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
/
stereotype.spec.js
69 lines (54 loc) · 1.82 KB
/
stereotype.spec.js
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
62
63
64
65
66
67
68
69
/* globals describe, it, expect */
'use strict';
var stereotype = require('..');
describe('stereotype(obj)', function() {
describe('should cast the string to', function() {
it('undefined', function() {
expect(stereotype('undefined')).toBe(undefined);
});
it('null', function() {
expect(stereotype('null')).toBe(null);
});
it('NaN', function() {
expect(isNaN(stereotype('NaN'))).toBe(true);
});
it('Infinity', function() {
expect(stereotype('Infinity')).toBe(Infinity);
});
it('a boolean', function() {
expect(stereotype('true')).toBe(true);
expect(stereotype('false')).toBe(false);
});
it('a number', function() {
expect(stereotype('-42')).toBe(-42);
expect(stereotype('0')).toBe(0);
expect(stereotype('42')).toBe(42);
expect(stereotype('+42')).toBe(42);
expect(stereotype('0xFF')).toBe(255);
expect(stereotype('-3.14')).toBe(-3.14);
expect(stereotype('0.00')).toBe(0);
expect(stereotype('3.14')).toBe(3.14);
expect(stereotype('+3.14')).toBe(+3.14);
});
});
describe('should not cast', function() {
var arr = [
[{}, 'any non-string'],
['', 'the empty string'],
[' ', 'a single space'],
['\r\n', 'whitespace'],
['2014.12.31', 'a date'],
['123foo', 'an integer preceded with a word'],
['foo123', 'an integer trailed by a word'],
['foo0x123', 'a hexadecimal number preceded with a word'],
['0x123foo', 'a hexadecimal number trailed by a word'],
['1.23foo', 'a float preceded with a word'],
['foo1.23', 'a float trailed by a word']
];
arr.forEach(function(elem) {
it(elem[1], function() {
expect(stereotype(elem[0])).toBe(elem[0]);
});
});
});
});