UNPKG

5.94 kBJavaScriptView Raw
1(function() {
2 var CND, CODEC, alert, badge, debug, echo, help, info, log, rpr, test, urge, warn, whisper, ƒ;
3
4 CND = require('cnd');
5
6 rpr = CND.rpr;
7
8 badge = 'HOLLERITH-CODEC/tests';
9
10 log = CND.get_logger('plain', badge);
11
12 info = CND.get_logger('info', badge);
13
14 whisper = CND.get_logger('whisper', badge);
15
16 alert = CND.get_logger('alert', badge);
17
18 debug = CND.get_logger('debug', badge);
19
20 warn = CND.get_logger('warn', badge);
21
22 help = CND.get_logger('help', badge);
23
24 urge = CND.get_logger('urge', badge);
25
26 echo = CND.echo.bind(CND);
27
28 test = require('guy-test');
29
30 CODEC = require('./main');
31
32 ƒ = CND.format_number;
33
34 this["codec encodes and decodes numbers"] = function(T) {
35 var key, key_bfr;
36 key = ['foo', 1234, 5678];
37 key_bfr = CODEC.encode(key);
38 T.eq(key, CODEC.decode(key_bfr));
39 return whisper("key length: " + key_bfr.length);
40 };
41
42 this["codec encodes and decodes dates"] = function(T) {
43 var key, key_bfr;
44 key = ['foo', new Date(), 5678];
45 key_bfr = CODEC.encode(key);
46 T.eq(key, CODEC.decode(key_bfr));
47 return whisper("key length: " + key_bfr.length);
48 };
49
50 this["codec accepts long numbers"] = function(T) {
51 var i, key, key_bfr;
52 key = [
53 'foo', (function() {
54 var j, results;
55 results = [];
56 for (i = j = 0; j <= 1000; i = ++j) {
57 results.push(i);
58 }
59 return results;
60 })(), 'bar'
61 ];
62 key_bfr = CODEC.encode(key);
63 T.eq(key, CODEC.decode(key_bfr));
64 return whisper("key length: " + key_bfr.length);
65 };
66
67 this["codec accepts long texts"] = function(T) {
68 var key, key_bfr, long_text;
69 long_text = (new Array(1e4)).join('#');
70 key = ['foo', [long_text, long_text, long_text, long_text], 42];
71 key_bfr = CODEC.encode(key);
72 T.eq(key, CODEC.decode(key_bfr));
73 return whisper("key length: " + key_bfr.length);
74 };
75
76 this["codec preserves critical escaped characters (roundtrip) (1)"] = function(T) {
77 var key, key_bfr, text;
78 text = 'abc\x00\x00\x00\x00def';
79 key = ['xxx', [text], 0];
80 key_bfr = CODEC.encode(key);
81 return T.eq(key, CODEC.decode(key_bfr));
82 };
83
84 this["codec preserves critical escaped characters (roundtrip) (2)"] = function(T) {
85 var key, key_bfr, text;
86 text = 'abc\x01\x01\x01\x01def';
87 key = ['xxx', [text], 0];
88 key_bfr = CODEC.encode(key);
89 return T.eq(key, CODEC.decode(key_bfr));
90 };
91
92 this["codec preserves critical escaped characters (roundtrip) (3)"] = function(T) {
93 var key, key_bfr, text;
94 text = 'abc\x00\x01\x00\x01def';
95 key = ['xxx', [text], 0];
96 key_bfr = CODEC.encode(key);
97 return T.eq(key, CODEC.decode(key_bfr));
98 };
99
100 this["codec preserves critical escaped characters (roundtrip) (4)"] = function(T) {
101 var key, key_bfr, text;
102 text = 'abc\x01\x00\x01\x00def';
103 key = ['xxx', [text], 0];
104 key_bfr = CODEC.encode(key);
105 return T.eq(key, CODEC.decode(key_bfr));
106 };
107
108 this["codec accepts private type (1)"] = function(T) {
109 var key, key_bfr;
110 key = [
111 {
112 type: 'price',
113 value: 'abc'
114 }
115 ];
116 key_bfr = CODEC.encode(key);
117 return T.eq(key, CODEC.decode(key_bfr));
118 };
119
120 this["codec accepts private type (2)"] = function(T) {
121 var key, key_bfr;
122 key = [
123 123, 456, {
124 type: 'price',
125 value: 'abc'
126 }, 'xxx'
127 ];
128 key_bfr = CODEC.encode(key);
129 return T.eq(key, CODEC.decode(key_bfr));
130 };
131
132 this["codec decodes private type with custom decoder"] = function(T) {
133 var decoded_key, encoded_value, key, key_bfr, matcher, value;
134 value = '/usr/local/lib/node_modules/coffee-script/README.md';
135 matcher = [value];
136 encoded_value = value.split('/');
137 key = [
138 {
139 type: 'route',
140 value: encoded_value
141 }
142 ];
143 key_bfr = CODEC.encode(key);
144 decoded_key = CODEC.decode(key_bfr, function(type, value) {
145 if (type === 'route') {
146 return value.join('/');
147 }
148 throw new Error("unknown private type " + (rpr(type)));
149 });
150 return T.eq(matcher, decoded_key);
151 };
152
153 this["codec decodes private type with custom encoder and decoder"] = function(T) {
154 var decoded_key_1, decoded_key_2, decoder, encoder, key, key_bfr, matcher_1, matcher_2, parts, route;
155 route = '/usr/local/lib/node_modules/coffee-script/README.md';
156 parts = route.split('/');
157 key = [
158 {
159 type: 'route',
160 value: route
161 }
162 ];
163 matcher_1 = [
164 {
165 type: 'route',
166 value: parts
167 }
168 ];
169 matcher_2 = [route];
170 encoder = function(type, value) {
171 if (type === 'route') {
172 return value.split('/');
173 }
174 throw new Error("unknown private type " + (rpr(type)));
175 };
176 decoder = function(type, value) {
177 if (type === 'route') {
178 return value.join('/');
179 }
180 throw new Error("unknown private type " + (rpr(type)));
181 };
182 key_bfr = CODEC.encode(key, encoder);
183 debug('©T4WKz', CODEC.rpr_of_buffer(key_bfr));
184 decoded_key_1 = CODEC.decode(key_bfr);
185 T.eq(matcher_1, decoded_key_1);
186 decoded_key_2 = CODEC.decode(key_bfr, decoder);
187 return T.eq(matcher_2, decoded_key_2);
188 };
189
190 this["private type takes default shape when handler returns use_fallback"] = function(T) {
191 var decoded_key, key, key_bfr, matcher;
192 matcher = [
193 84, {
194 type: 'bar',
195 value: 108
196 }
197 ];
198 key = [
199 {
200 type: 'foo',
201 value: 42
202 }, {
203 type: 'bar',
204 value: 108
205 }
206 ];
207 key_bfr = CODEC.encode(key);
208 decoded_key = CODEC.decode(key_bfr, function(type, value, use_fallback) {
209 if (type === 'foo') {
210 return value * 2;
211 }
212 return use_fallback;
213 });
214 return T.eq(matcher, decoded_key);
215 };
216
217 this._main = function() {
218 return test(this, {
219 'timeout': 2500
220 });
221 };
222
223}).call(this);
224
225//# sourceMappingURL=../sourcemaps/tests.js.map
\No newline at end of file