-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
123 lines (100 loc) · 3.14 KB
/
test.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
var assert = require('assert');
var lz = require('lz-string');
var tiny = require('./tiny-json');
describe('public', function () {
it('pack', function () {
var pattern = ['any'];
assert.strictEqual(tiny.pack('something', pattern), 'something');
});
it('unpack', function () {
var pattern = ['any'];
assert.strictEqual(tiny.unpack('something', pattern), 'something');
});
});
describe('compress', function () {
it('object', function () {
var pattern = ['object', { key: ['any'] }];
assert.strictEqual(
JSON.stringify(tiny._compress({ key: 'value' }, pattern)),
JSON.stringify(['value'])
);
});
it('array', function () {
var pattern = ['array', ['any']];
assert.strictEqual(
JSON.stringify(tiny._compress([1, 2, 3], pattern)),
JSON.stringify([1, 2, 3])
);
});
it('any', function () {
var pattern = ['any'];
assert.strictEqual(tiny._compress('anything', pattern), 'anything');
});
it('boolean', function () {
var pattern = ['boolean'];
assert.strictEqual(tiny._compress(false, pattern), 0);
assert.strictEqual(tiny._compress(true, pattern), 1);
});
it('enum', function () {
var pattern = ['enum', ['FOO', 'BAR', 'BAZ']];
assert.strictEqual(tiny._compress('FOO', pattern), 0);
assert.strictEqual(tiny._compress('BAR', pattern), 1);
assert.strictEqual(tiny._compress('BAZ', pattern), 2);
assert.throws(function () { tiny._compress('QUX', pattern); });
});
it('string', function () {
var pattern = ['string'];
assert.strictEqual(
tiny._compress('foobar', pattern),
lz.compress('foobar')
);
});
it('number', function () {
var pattern = ['number'];
assert.strictEqual(tiny._compress(1337, pattern), 1337);
});
});
describe('decompress', function () {
it('object', function () {
var pattern = ['object', { key: ['any'] }];
assert.strictEqual(
JSON.stringify(tiny._decompress(['value'], pattern)),
JSON.stringify({ key: 'value' })
);
});
it('array', function () {
var pattern = ['array', ['any']];
assert.strictEqual(
JSON.stringify(tiny._decompress([1, 2, 3], pattern)),
JSON.stringify([1, 2, 3])
);
});
it('any', function () {
var pattern = ['any'];
assert.strictEqual(tiny._decompress('anything', pattern), 'anything');
});
it('boolean', function () {
var pattern = ['boolean'];
assert.strictEqual(tiny._decompress(0, pattern), false);
assert.strictEqual(tiny._decompress(1, pattern), true);
});
it('enum', function () {
var pattern = ['enum', ['FOO', 'BAR', 'BAZ']];
assert.strictEqual(tiny._decompress(0, pattern), 'FOO');
assert.strictEqual(tiny._decompress(1, pattern), 'BAR');
assert.strictEqual(tiny._decompress(2, pattern), 'BAZ');
assert.throws(function () { tiny._decompress(3, pattern); });
});
it('string', function () {
var pattern = ['string'];
assert.strictEqual(
tiny._decompress(lz.compress('foobar'), pattern),
'foobar'
);
});
it('number', function () {
var pattern = ['number'];
assert.strictEqual(tiny._decompress(1337, pattern), 1337);
});
});