Mercurial > hg > beaglert
comparison resources/osc/node_modules/osc-min/test/test-osc-utilities.coffee @ 271:fb9c28a4676b prerelease
Added osc example project and node script for testing
author | Liam Donovan <l.b.donovan@qmul.ac.uk> |
---|---|
date | Tue, 17 May 2016 16:01:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
270:de37582ce6f3 | 271:fb9c28a4676b |
---|---|
1 # | |
2 # This file was used for TDD and as such probably has limited utility as | |
3 # actual unit tests. | |
4 # | |
5 try | |
6 osc = require '../lib-cov/osc-utilities' | |
7 catch e | |
8 osc = require '../lib/osc-utilities' | |
9 | |
10 assert = require "assert" | |
11 | |
12 # Basic string tests. | |
13 | |
14 testString = (str, expected_len) -> | |
15 str : str | |
16 len : expected_len | |
17 | |
18 testData = [ | |
19 testString("abc", 4) | |
20 testString("abcd", 8) | |
21 testString("abcde", 8) | |
22 testString("abcdef", 8) | |
23 testString("abcdefg", 8) | |
24 ] | |
25 | |
26 testStringLength = (str, expected_len) -> | |
27 oscstr = osc.toOscString(str) | |
28 assert.strictEqual(oscstr.length, expected_len) | |
29 | |
30 test 'basic strings length', -> | |
31 for data in testData | |
32 testStringLength data.str, data.len | |
33 | |
34 | |
35 testStringRoundTrip = (str, strict) -> | |
36 oscstr = osc.toOscString(str) | |
37 str2 = osc.splitOscString(oscstr, strict)?.string | |
38 assert.strictEqual(str, str2) | |
39 | |
40 test 'basic strings round trip', -> | |
41 for data in testData | |
42 testStringRoundTrip data.str | |
43 | |
44 | |
45 test 'non strings fail toOscString', -> | |
46 assert.throws -> osc.toOscString(7) | |
47 | |
48 | |
49 test 'strings with null characters don\'t fail toOscString by default', -> | |
50 assert.notEqual(osc.toOscString("\u0000"), null) | |
51 | |
52 | |
53 test 'strings with null characters fail toOscString in strict mode', -> | |
54 assert.throws -> osc.toOscString("\u0000", true) | |
55 | |
56 | |
57 test 'osc buffers with no null characters fail splitOscString in strict mode', -> | |
58 assert.throws -> osc.splitOscString new Buffer("abc"), true | |
59 | |
60 | |
61 test 'osc buffers with non-null characters after a null character fail fromOscString in strict mode', -> | |
62 assert.throws -> osc.fromOscString new Buffer("abc\u0000abcd"), true | |
63 | |
64 | |
65 test 'basic strings pass fromOscString in strict mode', -> | |
66 for data in testData | |
67 testStringRoundTrip data.str, true | |
68 | |
69 | |
70 test 'osc buffers with non-four length fail in strict mode', -> | |
71 assert.throws -> osc.fromOscString new Buffer("abcd\u0000\u0000"), true | |
72 | |
73 test 'splitOscString throws when passed a non-buffer', -> | |
74 assert.throws -> osc.splitOscString "test" | |
75 | |
76 test 'splitOscString of an osc-string matches the string', -> | |
77 split = osc.splitOscString osc.toOscString "testing it" | |
78 assert.strictEqual(split?.string, "testing it") | |
79 assert.strictEqual(split?.rest?.length, 0) | |
80 | |
81 | |
82 test 'splitOscString works with an over-allocated buffer', -> | |
83 buffer = osc.toOscString "testing it" | |
84 overallocated = new Buffer(16) | |
85 buffer.copy(overallocated) | |
86 split = osc.splitOscString overallocated | |
87 assert.strictEqual(split?.string, "testing it") | |
88 assert.strictEqual(split?.rest?.length, 4) | |
89 | |
90 | |
91 test 'splitOscString works with just a string by default', -> | |
92 split = osc.splitOscString (new Buffer "testing it") | |
93 assert.strictEqual(split?.string, "testing it") | |
94 assert.strictEqual(split?.rest?.length, 0) | |
95 | |
96 | |
97 test 'splitOscString strict fails for just a string', -> | |
98 assert.throws -> osc.splitOscString (new Buffer "testing it"), true | |
99 | |
100 | |
101 test 'splitOscString strict fails for string with not enough padding', -> | |
102 assert.throws -> osc.splitOscString (new Buffer "testing \u0000\u0000"), true | |
103 | |
104 | |
105 test 'splitOscString strict succeeds for strings with valid padding', -> | |
106 split = osc.splitOscString (new Buffer "testing it\u0000\u0000aaaa"), true | |
107 assert.strictEqual(split?.string, "testing it") | |
108 assert.strictEqual(split?.rest?.length, 4) | |
109 | |
110 | |
111 test 'splitOscString strict fails for string with invalid padding', -> | |
112 assert.throws -> osc.splitOscString (new Buffer "testing it\u0000aaaaa"), true | |
113 | |
114 test 'concat throws when passed a single buffer', -> | |
115 assert.throws -> osc.concat new Buffer "test" | |
116 | |
117 test 'concat throws when passed an array of non-buffers', -> | |
118 assert.throws -> osc.concat ["bleh"] | |
119 | |
120 test 'toIntegerBuffer throws when passed a non-number', -> | |
121 assert.throws -> osc.toIntegerBuffer "abcdefg" | |
122 | |
123 test 'splitInteger fails when sent a buffer that\'s too small', -> | |
124 assert.throws -> osc.splitInteger new Buffer 3, "Int32" | |
125 | |
126 test 'splitOscArgument fails when given a bogus type', -> | |
127 assert.throws -> osc.splitOscArgument new Buffer 8, "bogus" | |
128 | |
129 test 'fromOscMessage with no type string works', -> | |
130 translate = osc.fromOscMessage osc.toOscString "/stuff" | |
131 assert.strictEqual translate?.address, "/stuff" | |
132 assert.deepEqual translate?.args, [] | |
133 | |
134 test 'fromOscMessage with type string and no args works', -> | |
135 oscaddr = osc.toOscString "/stuff" | |
136 osctype = osc.toOscString "," | |
137 oscmessage = new Buffer(oscaddr.length + osctype.length) | |
138 oscaddr.copy oscmessage | |
139 osctype.copy oscmessage, oscaddr.length | |
140 translate = osc.fromOscMessage oscmessage | |
141 assert.strictEqual translate?.address, "/stuff" | |
142 assert.deepEqual translate?.args, [] | |
143 | |
144 test 'fromOscMessage with string argument works', -> | |
145 oscaddr = osc.toOscString "/stuff" | |
146 osctype = osc.toOscString ",s" | |
147 oscarg = osc.toOscString "argu" | |
148 translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] | |
149 assert.strictEqual translate?.address, "/stuff" | |
150 assert.strictEqual translate?.args?[0]?.type, "string" | |
151 assert.strictEqual translate?.args?[0]?.value, "argu" | |
152 | |
153 test 'fromOscMessage with true argument works', -> | |
154 oscaddr = osc.toOscString "/stuff" | |
155 osctype = osc.toOscString ",T" | |
156 translate = osc.fromOscMessage osc.concat [oscaddr, osctype] | |
157 assert.strictEqual translate?.address, "/stuff" | |
158 assert.strictEqual translate?.args?[0]?.type, "true" | |
159 assert.strictEqual translate?.args?[0]?.value, true | |
160 | |
161 test 'fromOscMessage with false argument works', -> | |
162 oscaddr = osc.toOscString "/stuff" | |
163 osctype = osc.toOscString ",F" | |
164 translate = osc.fromOscMessage osc.concat [oscaddr, osctype] | |
165 assert.strictEqual translate?.address, "/stuff" | |
166 assert.strictEqual translate?.args?[0]?.type, "false" | |
167 assert.strictEqual translate?.args?[0]?.value, false | |
168 | |
169 test 'fromOscMessage with null argument works', -> | |
170 oscaddr = osc.toOscString "/stuff" | |
171 osctype = osc.toOscString ",N" | |
172 translate = osc.fromOscMessage osc.concat [oscaddr, osctype] | |
173 assert.strictEqual translate?.address, "/stuff" | |
174 assert.strictEqual translate?.args?[0]?.type, "null" | |
175 assert.strictEqual translate?.args?[0]?.value, null | |
176 | |
177 test 'fromOscMessage with bang argument works', -> | |
178 oscaddr = osc.toOscString "/stuff" | |
179 osctype = osc.toOscString ",I" | |
180 translate = osc.fromOscMessage osc.concat [oscaddr, osctype] | |
181 assert.strictEqual translate?.address, "/stuff" | |
182 assert.strictEqual translate?.args?[0]?.type, "bang" | |
183 assert.strictEqual translate?.args?[0]?.value, "bang" | |
184 | |
185 test 'fromOscMessage with blob argument works', -> | |
186 oscaddr = osc.toOscString "/stuff" | |
187 osctype = osc.toOscString ",b" | |
188 oscarg = osc.concat [(osc.toIntegerBuffer 4), new Buffer "argu"] | |
189 translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] | |
190 assert.strictEqual translate?.address, "/stuff" | |
191 assert.strictEqual translate?.args?[0]?.type, "blob" | |
192 assert.strictEqual (translate?.args?[0]?.value?.toString "utf8"), "argu" | |
193 | |
194 test 'fromOscMessage with integer argument works', -> | |
195 oscaddr = osc.toOscString "/stuff" | |
196 osctype = osc.toOscString ",i" | |
197 oscarg = osc.toIntegerBuffer 888 | |
198 translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] | |
199 assert.strictEqual translate?.address, "/stuff" | |
200 assert.strictEqual translate?.args?[0]?.type, "integer" | |
201 assert.strictEqual (translate?.args?[0]?.value), 888 | |
202 | |
203 test 'fromOscMessage with timetag argument works', -> | |
204 oscaddr = osc.toOscString "/stuff" | |
205 osctype = osc.toOscString ",t" | |
206 timetag = [8888, 9999] | |
207 oscarg = osc.toTimetagBuffer timetag | |
208 translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] | |
209 assert.strictEqual translate?.address, "/stuff" | |
210 assert.strictEqual translate?.args?[0]?.type, "timetag" | |
211 assert.deepEqual (translate?.args?[0]?.value), timetag | |
212 | |
213 test 'fromOscMessage with mismatched array doesn\'t throw', -> | |
214 oscaddr = osc.toOscString "/stuff" | |
215 assert.doesNotThrow (-> osc.fromOscMessage osc.concat( | |
216 [oscaddr, osc.toOscString ",["])) | |
217 assert.doesNotThrow (-> osc.fromOscMessage osc.concat( | |
218 [oscaddr, osc.toOscString ",["])) | |
219 | |
220 test 'fromOscMessage with mismatched array throws in strict', -> | |
221 oscaddr = osc.toOscString "/stuff" | |
222 assert.throws (-> osc.fromOscMessage (osc.concat( | |
223 [oscaddr, osc.toOscString ",["])), true) | |
224 assert.throws (-> osc.fromOscMessage (osc.concat( | |
225 [oscaddr, osc.toOscString ",]"])), true) | |
226 | |
227 test 'fromOscMessage with empty array argument works', -> | |
228 oscaddr = osc.toOscString "/stuff" | |
229 osctype = osc.toOscString ",[]" | |
230 translate = osc.fromOscMessage osc.concat [oscaddr, osctype] | |
231 assert.strictEqual translate?.address, "/stuff" | |
232 assert.strictEqual translate?.args?[0]?.type, "array" | |
233 assert.strictEqual (translate?.args?[0]?.value?.length), 0 | |
234 assert.deepEqual (translate?.args?[0]?.value), [] | |
235 | |
236 test 'fromOscMessage with bang array argument works', -> | |
237 oscaddr = osc.toOscString "/stuff" | |
238 osctype = osc.toOscString ",[I]" | |
239 translate = osc.fromOscMessage osc.concat [oscaddr, osctype] | |
240 assert.strictEqual translate?.address, "/stuff" | |
241 assert.strictEqual translate?.args?[0]?.type, "array" | |
242 assert.strictEqual (translate?.args?[0]?.value?.length), 1 | |
243 assert.strictEqual (translate?.args?[0]?.value?[0]?.type), "bang" | |
244 assert.strictEqual (translate?.args?[0]?.value?[0]?.value), "bang" | |
245 | |
246 test 'fromOscMessage with string array argument works', -> | |
247 oscaddr = osc.toOscString "/stuff" | |
248 osctype = osc.toOscString ",[s]" | |
249 oscarg = osc.toOscString "argu" | |
250 translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] | |
251 assert.strictEqual translate?.address, "/stuff" | |
252 assert.strictEqual translate?.args?[0]?.type, "array" | |
253 assert.strictEqual (translate?.args?[0]?.value?.length), 1 | |
254 assert.strictEqual (translate?.args?[0]?.value?[0]?.type), "string" | |
255 assert.strictEqual (translate?.args?[0]?.value?[0]?.value), "argu" | |
256 | |
257 test 'fromOscMessage with nested array argument works', -> | |
258 oscaddr = osc.toOscString "/stuff" | |
259 osctype = osc.toOscString ",[[I]]" | |
260 translate = osc.fromOscMessage osc.concat [oscaddr, osctype] | |
261 assert.strictEqual translate?.address, "/stuff" | |
262 assert.strictEqual translate?.args?[0]?.type, "array" | |
263 assert.strictEqual translate?.args?[0]?.value?.length, 1 | |
264 assert.strictEqual (translate?.args?[0]?.value?[0]?.type), "array" | |
265 assert.strictEqual (translate?.args?[0]?.value?[0]?.value?.length), 1 | |
266 assert.strictEqual (translate?.args?[0]?.value?[0]?.value?[0]?.type), "bang" | |
267 assert.strictEqual (translate?.args?[0]?.value?[0]?.value?[0]?.value), "bang" | |
268 | |
269 test 'fromOscMessage with multiple args works', -> | |
270 oscaddr = osc.toOscString "/stuff" | |
271 osctype = osc.toOscString ",sbi" | |
272 oscargs = [ | |
273 (osc.toOscString "argu") | |
274 (osc.concat [(osc.toIntegerBuffer 4), new Buffer "argu"]) | |
275 (osc.toIntegerBuffer 888) | |
276 ] | |
277 | |
278 oscbuffer = osc.concat [oscaddr, osctype, (osc.concat oscargs)] | |
279 translate = osc.fromOscMessage oscbuffer | |
280 assert.strictEqual translate?.address, "/stuff" | |
281 assert.strictEqual translate?.args?[0]?.type, "string" | |
282 assert.strictEqual (translate?.args?[0]?.value), "argu" | |
283 | |
284 test 'fromOscMessage strict fails if type string has no comma', -> | |
285 oscaddr = osc.toOscString "/stuff" | |
286 osctype = osc.toOscString "fake" | |
287 assert.throws -> | |
288 osc.fromOscMessage (osc.concat [oscaddr, osctype]), true | |
289 | |
290 test 'fromOscMessage non-strict works if type string has no comma', -> | |
291 oscaddr = osc.toOscString "/stuff" | |
292 osctype = osc.toOscString "fake" | |
293 message = osc.fromOscMessage (osc.concat [oscaddr, osctype]) | |
294 assert.strictEqual message.address, "/stuff" | |
295 assert.strictEqual message.args.length, 0 | |
296 | |
297 test 'fromOscMessage strict fails if type address doesn\'t begin with /', -> | |
298 oscaddr = osc.toOscString "stuff" | |
299 osctype = osc.toOscString "," | |
300 assert.throws -> | |
301 osc.fromOscMessage (osc.concat [oscaddr, osctype]), true | |
302 | |
303 test 'fromOscBundle works with no messages', -> | |
304 oscbundle = osc.toOscString "#bundle" | |
305 timetag = [0, 0] | |
306 osctimetag = osc.toTimetagBuffer timetag | |
307 buffer = osc.concat [oscbundle, osctimetag] | |
308 translate = osc.fromOscBundle buffer | |
309 assert.deepEqual translate?.timetag, timetag | |
310 assert.deepEqual translate?.elements, [] | |
311 | |
312 test 'fromOscBundle works with single message', -> | |
313 oscbundle = osc.toOscString "#bundle" | |
314 timetag = [0, 0] | |
315 osctimetag = osc.toTimetagBuffer timetag | |
316 oscaddr = osc.toOscString "/addr" | |
317 osctype = osc.toOscString "," | |
318 oscmessage = osc.concat [oscaddr, osctype] | |
319 osclen = osc.toIntegerBuffer oscmessage.length | |
320 buffer = osc.concat [oscbundle, osctimetag, osclen, oscmessage] | |
321 translate = osc.fromOscBundle buffer | |
322 assert.deepEqual translate?.timetag, timetag | |
323 assert.strictEqual translate?.elements?.length, 1 | |
324 assert.strictEqual translate?.elements?[0]?.address, "/addr" | |
325 | |
326 test 'fromOscBundle works with multiple messages', -> | |
327 oscbundle = osc.toOscString "#bundle" | |
328 timetag = [0, 0] | |
329 osctimetag = osc.toTimetagBuffer timetag | |
330 oscaddr1 = osc.toOscString "/addr" | |
331 osctype1 = osc.toOscString "," | |
332 oscmessage1 = osc.concat [oscaddr1, osctype1] | |
333 osclen1 = osc.toIntegerBuffer oscmessage1.length | |
334 oscaddr2 = osc.toOscString "/addr2" | |
335 osctype2 = osc.toOscString "," | |
336 oscmessage2 = osc.concat [oscaddr2, osctype2] | |
337 osclen2 = osc.toIntegerBuffer oscmessage2.length | |
338 buffer = osc.concat [oscbundle, osctimetag, osclen1, oscmessage1, osclen2, oscmessage2] | |
339 translate = osc.fromOscBundle buffer | |
340 assert.deepEqual translate?.timetag, timetag | |
341 assert.strictEqual translate?.elements?.length, 2 | |
342 assert.strictEqual translate?.elements?[0]?.address, "/addr" | |
343 assert.strictEqual translate?.elements?[1]?.address, "/addr2" | |
344 | |
345 test 'fromOscBundle works with nested bundles', -> | |
346 oscbundle = osc.toOscString "#bundle" | |
347 timetag = [0, 0] | |
348 osctimetag = osc.toTimetagBuffer timetag | |
349 oscaddr1 = osc.toOscString "/addr" | |
350 osctype1 = osc.toOscString "," | |
351 oscmessage1 = osc.concat [oscaddr1, osctype1] | |
352 osclen1 = osc.toIntegerBuffer oscmessage1.length | |
353 oscbundle2 = osc.toOscString "#bundle" | |
354 timetag2 = [0, 0] | |
355 osctimetag2 = osc.toTimetagBuffer timetag2 | |
356 oscmessage2 = osc.concat [oscbundle2, osctimetag2] | |
357 osclen2 = osc.toIntegerBuffer oscmessage2.length | |
358 buffer = osc.concat [oscbundle, osctimetag, osclen1, oscmessage1, osclen2, oscmessage2] | |
359 translate = osc.fromOscBundle buffer | |
360 assert.deepEqual translate?.timetag, timetag | |
361 assert.strictEqual translate?.elements?.length, 2 | |
362 assert.strictEqual translate?.elements?[0]?.address, "/addr" | |
363 assert.deepEqual translate?.elements?[1]?.timetag, timetag2 | |
364 | |
365 test 'fromOscBundle works with non-understood messages', -> | |
366 oscbundle = osc.toOscString "#bundle" | |
367 timetag = [0, 0] | |
368 osctimetag = osc.toTimetagBuffer timetag | |
369 oscaddr1 = osc.toOscString "/addr" | |
370 osctype1 = osc.toOscString "," | |
371 oscmessage1 = osc.concat [oscaddr1, osctype1] | |
372 osclen1 = osc.toIntegerBuffer oscmessage1.length | |
373 oscaddr2 = osc.toOscString "/addr2" | |
374 osctype2 = osc.toOscString ",α" | |
375 oscmessage2 = osc.concat [oscaddr2, osctype2] | |
376 osclen2 = osc.toIntegerBuffer oscmessage2.length | |
377 buffer = osc.concat [oscbundle, osctimetag, osclen1, oscmessage1, osclen2, oscmessage2] | |
378 translate = osc.fromOscBundle buffer | |
379 assert.deepEqual translate?.timetag, timetag | |
380 assert.strictEqual translate?.elements?.length, 1 | |
381 assert.strictEqual translate?.elements?[0]?.address, "/addr" | |
382 | |
383 test 'fromOscBundle fails with bad bundle ID', -> | |
384 oscbundle = osc.toOscString "#blunder" | |
385 assert.throws -> osc.fromOscBundle oscbundle | |
386 | |
387 test 'fromOscBundle fails with ridiculous sizes', -> | |
388 timetag = [0, 0] | |
389 oscbundle = osc.concat [ | |
390 osc.toOscString "#bundle" | |
391 osc.toTimetagBuffer timetag | |
392 osc.toIntegerBuffer 999999 | |
393 ] | |
394 assert.throws -> osc.fromOscBundle oscbundle | |
395 | |
396 roundTripMessage = (args) -> | |
397 oscMessage = { | |
398 address : "/addr" | |
399 args : args | |
400 } | |
401 roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage), true | |
402 assert.strictEqual roundTrip?.address, "/addr" | |
403 assert.strictEqual roundTrip?.args?.length, args.length | |
404 for i in [0...args.length] | |
405 comp = if args[i]?.value? then args[i].value else args[i] | |
406 assert.strictEqual roundTrip?.args?[i]?.type, args[i].type if args[i]?.type? | |
407 if Buffer.isBuffer comp | |
408 for j in [0...comp.length] | |
409 assert.deepEqual roundTrip?.args?[i]?.value?[j], comp[j] | |
410 else | |
411 assert.deepEqual roundTrip?.args?[i]?.value, comp | |
412 | |
413 test 'toOscArgument fails when given bogus type', -> | |
414 assert.throws -> osc.toOscArgument "bleh", "bogus" | |
415 | |
416 # we tested fromOsc* manually, so just use roundtrip testing for toOsc* | |
417 test 'toOscMessage with no args works', -> | |
418 roundTripMessage [] | |
419 | |
420 test 'toOscMessage strict with null argument throws', -> | |
421 assert.throws -> osc.toOscMessage {address : "/addr", args : [null]}, true | |
422 | |
423 test 'toOscMessage with string argument works', -> | |
424 roundTripMessage ["strr"] | |
425 | |
426 test 'toOscMessage with empty array argument works', -> | |
427 roundTripMessage [[]] | |
428 | |
429 test 'toOscMessage with array value works', -> | |
430 roundTripMessage [{value:[]}] | |
431 | |
432 test 'toOscMessage with string array argument works', -> | |
433 roundTripMessage [[{type:"string", value:"hello"}, | |
434 {type:"string", value:"goodbye"}]] | |
435 | |
436 test 'toOscMessage with multi-type array argument works', -> | |
437 roundTripMessage [[{type:"string", value:"hello"}, | |
438 {type:"integer", value:7}]] | |
439 | |
440 test 'toOscMessage with nested array argument works', -> | |
441 roundTripMessage [[{type:"array", value:[{type:"string", value:"hello"}]}]] | |
442 | |
443 buffeq = (buff, exp_buff) -> | |
444 assert.strictEqual buff.length, exp_buff.length | |
445 for i in [0...exp_buff.length] | |
446 assert.equal buff[i], exp_buff[i] | |
447 | |
448 test 'toOscMessage with bad layout works', -> | |
449 oscMessage = { | |
450 address : "/addr" | |
451 args : [ | |
452 "strr" | |
453 ] | |
454 } | |
455 roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage), true | |
456 assert.strictEqual roundTrip?.address, "/addr" | |
457 assert.strictEqual roundTrip?.args?.length, 1 | |
458 assert.strictEqual roundTrip?.args?[0]?.value, "strr" | |
459 | |
460 test 'toOscMessage with single numeric argument works', -> | |
461 oscMessage = { | |
462 address : "/addr" | |
463 args : 13 | |
464 } | |
465 roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage) | |
466 assert.strictEqual roundTrip?.address, "/addr" | |
467 assert.strictEqual roundTrip?.args?.length, 1 | |
468 assert.strictEqual roundTrip?.args?[0]?.value, 13 | |
469 assert.strictEqual roundTrip?.args?[0]?.type, "float" | |
470 | |
471 test 'toOscMessage with args shortcut works', -> | |
472 oscMessage = { | |
473 address : "/addr" | |
474 args : 13 | |
475 } | |
476 roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage) | |
477 assert.strictEqual roundTrip?.address, "/addr" | |
478 assert.strictEqual roundTrip?.args?.length, 1 | |
479 assert.strictEqual roundTrip?.args?[0]?.value, 13 | |
480 assert.strictEqual roundTrip?.args?[0]?.type, "float" | |
481 | |
482 test 'toOscMessage with single blob argument works', -> | |
483 buff = new Buffer 18 | |
484 oscMessage = { | |
485 address : "/addr" | |
486 args : buff | |
487 } | |
488 roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage) | |
489 assert.strictEqual roundTrip?.address, "/addr" | |
490 assert.strictEqual roundTrip?.args?.length, 1 | |
491 buffeq roundTrip?.args?[0]?.value, buff | |
492 assert.strictEqual roundTrip?.args?[0]?.type, "blob" | |
493 | |
494 test 'toOscMessage with single string argument works', -> | |
495 oscMessage = { | |
496 address : "/addr" | |
497 args : "strr" | |
498 } | |
499 roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage) | |
500 assert.strictEqual roundTrip?.address, "/addr" | |
501 assert.strictEqual roundTrip?.args?.length, 1 | |
502 assert.strictEqual roundTrip?.args?[0]?.value, "strr" | |
503 assert.strictEqual roundTrip?.args?[0]?.type, "string" | |
504 | |
505 test 'toOscMessage with integer argument works', -> | |
506 roundTripMessage [8] | |
507 | |
508 test 'toOscMessage with buffer argument works', -> | |
509 # buffer will have random contents, but that's okay. | |
510 roundTripMessage [new Buffer 16] | |
511 | |
512 test 'toOscMessage strict with type true and value false throws', -> | |
513 assert.throws -> osc.toOscMessage {address: "/addr/", args: {type : "true", value : false}}, true | |
514 | |
515 test 'toOscMessage strict with type false with value true throws', -> | |
516 assert.throws -> osc.toOscMessage {address: "/addr/", args: {type : "false", value : true}}, true | |
517 | |
518 test 'toOscMessage with type true works', -> | |
519 roundTrip = osc.fromOscMessage osc.toOscMessage {address: "/addr", args : true} | |
520 assert.strictEqual roundTrip.args.length, 1 | |
521 assert.strictEqual roundTrip.args[0].value, true | |
522 assert.strictEqual roundTrip.args[0].type, "true" | |
523 | |
524 test 'toOscMessage with type false works', -> | |
525 roundTrip = osc.fromOscMessage osc.toOscMessage {address: "/addr", args : false} | |
526 assert.strictEqual roundTrip.args.length, 1 | |
527 assert.strictEqual roundTrip.args[0].value, false | |
528 assert.strictEqual roundTrip.args[0].type, "false" | |
529 | |
530 test 'toOscMessage with type bang argument works', -> | |
531 roundTrip = osc.fromOscMessage osc.toOscMessage {address: "/addr", args : {type:"bang"}} | |
532 assert.strictEqual roundTrip.args.length, 1 | |
533 assert.strictEqual roundTrip.args[0].value, "bang" | |
534 assert.strictEqual roundTrip.args[0].type, "bang" | |
535 | |
536 test 'toOscMessage with type timetag argument works', -> | |
537 roundTripMessage [{type: "timetag", value: [8888, 9999]}] | |
538 | |
539 test 'toOscMessage with type double argument works', -> | |
540 roundTripMessage [{type: "double", value: 8888}] | |
541 | |
542 test 'toOscMessage strict with type null with value true throws', -> | |
543 assert.throws -> osc.toOscMessage({address: "/addr/", args: {type : "null", value : true}}, true) | |
544 | |
545 test 'toOscMessage with type null works', -> | |
546 roundTrip = osc.fromOscMessage osc.toOscMessage {address: "/addr", args : null} | |
547 assert.strictEqual roundTrip.args.length, 1 | |
548 assert.strictEqual roundTrip.args[0].value, null | |
549 assert.strictEqual roundTrip.args[0].type, "null" | |
550 | |
551 test 'toOscMessage with float argument works', -> | |
552 roundTripMessage [{value : 6, type : "float"}] | |
553 | |
554 test 'toOscMessage just a string works', -> | |
555 message = osc.fromOscMessage osc.toOscMessage "bleh" | |
556 assert.strictEqual message.address, "bleh" | |
557 assert.strictEqual message.args.length, 0 | |
558 | |
559 test 'toOscMessage with multiple args works', -> | |
560 roundTripMessage ["str", 7, (new Buffer 30), 6] | |
561 | |
562 test 'toOscMessage with integer argument works', -> | |
563 roundTripMessage [{value : 7, type: "integer"}] | |
564 | |
565 test 'toOscMessage fails with no address', -> | |
566 assert.throws -> osc.toOscMessage {args : []} | |
567 | |
568 toOscMessageThrowsHelper = (arg) -> | |
569 assert.throws -> osc.toOscMessage( | |
570 address : "/addr" | |
571 args : [arg] | |
572 ) | |
573 | |
574 test 'toOscMessage fails when string type is specified but wrong', -> | |
575 toOscMessageThrowsHelper( | |
576 value : 7 | |
577 type : "string" | |
578 ) | |
579 | |
580 test 'toOscMessage fails when integer type is specified but wrong', -> | |
581 toOscMessageThrowsHelper( | |
582 value : "blah blah" | |
583 type : "integer" | |
584 ) | |
585 | |
586 test 'toOscMessage fails when float type is specified but wrong', -> | |
587 toOscMessageThrowsHelper( | |
588 value : "blah blah" | |
589 type : "float" | |
590 ) | |
591 | |
592 test 'toOscMessage fails when timetag type is specified but wrong', -> | |
593 toOscMessageThrowsHelper( | |
594 value : "blah blah" | |
595 type : "timetag" | |
596 ) | |
597 | |
598 test 'toOscMessage fails when double type is specified but wrong', -> | |
599 toOscMessageThrowsHelper( | |
600 value : "blah blah" | |
601 type : "double" | |
602 ) | |
603 | |
604 test 'toOscMessage fails when blob type is specified but wrong', -> | |
605 toOscMessageThrowsHelper( | |
606 value : "blah blah" | |
607 type : "blob" | |
608 ) | |
609 | |
610 test 'toOscMessage fails argument is a random type', -> | |
611 toOscMessageThrowsHelper( | |
612 random_field : 42 | |
613 "is pretty random" : 888 | |
614 ) | |
615 | |
616 roundTripBundle = (elems) -> | |
617 oscMessage = { | |
618 timetag : [0, 0] | |
619 elements : elems | |
620 } | |
621 roundTrip = osc.fromOscBundle (osc.toOscBundle oscMessage), true | |
622 assert.deepEqual roundTrip?.timetag, [0, 0] | |
623 length = if typeof elems is "object" then elems.length else 1 | |
624 assert.strictEqual roundTrip?.elements?.length, length | |
625 for i in [0...length] | |
626 if typeof elems is "object" | |
627 assert.deepEqual roundTrip?.elements?[i]?.timetag, elems[i].timetag | |
628 assert.strictEqual roundTrip?.elements?[i]?.address, elems[i].address | |
629 else | |
630 assert.strictEqual roundTrip?.elements?[i]?.address, elems | |
631 | |
632 test 'toOscBundle with no elements works', -> | |
633 roundTripBundle [] | |
634 | |
635 test 'toOscBundle with just a string works', -> | |
636 roundTripBundle "/address" | |
637 | |
638 test 'toOscBundle with just a number fails', -> | |
639 assert.throws -> roundTripBundle 78 | |
640 | |
641 test 'toOscBundle with one message works', -> | |
642 roundTripBundle [{address : "/addr"}] | |
643 | |
644 test 'toOscBundle with nested bundles works', -> | |
645 roundTripBundle [{address : "/addr"}, {timetag : [8888, 9999]}] | |
646 | |
647 test 'toOscBundle with bogus packets works', -> | |
648 roundTrip = osc.fromOscBundle osc.toOscBundle { | |
649 timetag : [0, 0] | |
650 elements : [{timetag : [0, 0]}, {maddress : "/addr"}] | |
651 } | |
652 assert.strictEqual roundTrip.elements.length, 1 | |
653 assert.deepEqual roundTrip.elements[0].timetag, [0, 0] | |
654 | |
655 test 'toOscBundle strict fails without timetags', -> | |
656 assert.throws -> osc.toOscBundle {elements :[]}, true | |
657 | |
658 test 'identity applyTransform works with single message', -> | |
659 testBuffer = osc.toOscString "/message" | |
660 assert.strictEqual (osc.applyTransform testBuffer, (a) -> a), testBuffer | |
661 | |
662 test 'nullary applyTransform works with single message', -> | |
663 testBuffer = osc.toOscString "/message" | |
664 assert.strictEqual (osc.applyTransform testBuffer, (a) -> new Buffer 0).length, 0 | |
665 | |
666 test 'toOscPacket works when explicitly set to bundle', -> | |
667 roundTrip = osc.fromOscBundle osc.toOscPacket {timetag: 0, oscType:"bundle", elements :[]}, true | |
668 assert.strictEqual roundTrip.elements.length, 0 | |
669 | |
670 test 'toOscPacket works when explicitly set to message', -> | |
671 roundTrip = osc.fromOscPacket osc.toOscPacket {address: "/bleh", oscType:"message", args :[]}, true | |
672 assert.strictEqual roundTrip.args.length, 0 | |
673 assert.strictEqual roundTrip.address, "/bleh" | |
674 | |
675 test 'identity applyTransform works with a simple bundle', -> | |
676 base = { | |
677 timetag : [0, 0] | |
678 elements : [ | |
679 {address : "test1"} | |
680 {address : "test2"} | |
681 ] | |
682 } | |
683 transformed = osc.fromOscPacket (osc.applyTransform (osc.toOscPacket base), (a) -> a) | |
684 | |
685 assert.deepEqual transformed?.timetag, [0, 0] | |
686 assert.strictEqual transformed?.elements?.length, base.elements.length | |
687 for i in [0...base.elements.length] | |
688 assert.equal transformed?.elements?[i]?.timetag, base.elements[i].timetag | |
689 assert.strictEqual transformed?.elements?[i]?.address, base.elements[i].address | |
690 | |
691 test 'applyMessageTranformerToBundle fails on bundle without tag', -> | |
692 func = osc.applyMessageTranformerToBundle ((a) -> a) | |
693 assert.throws -> func osc.concat [osc.toOscString "#grundle", osc.toIntegerBuffer 0, "Int64"] | |
694 | |
695 test 'addressTransform works with identity', -> | |
696 testBuffer = osc.concat [ | |
697 osc.toOscString "/message" | |
698 new Buffer "gobblegobblewillsnever\u0000parse blah lbha" | |
699 ] | |
700 transformed = osc.applyTransform testBuffer, osc.addressTransform((a) -> a) | |
701 for i in [0...testBuffer.length] | |
702 assert.equal transformed[i], testBuffer[i] | |
703 | |
704 | |
705 test 'addressTransform works with bundles', -> | |
706 base = { | |
707 timetag : [0, 0] | |
708 elements : [ | |
709 {address : "test1"} | |
710 {address : "test2"} | |
711 ] | |
712 } | |
713 transformed = osc.fromOscPacket (osc.applyTransform (osc.toOscPacket base), osc.addressTransform((a) -> "/prelude/" + a)) | |
714 | |
715 assert.deepEqual transformed?.timetag, [0, 0] | |
716 assert.strictEqual transformed?.elements?.length, base.elements.length | |
717 for i in [0...base.elements.length] | |
718 assert.equal transformed?.elements?[i]?.timetag, base.elements[i].timetag | |
719 assert.strictEqual transformed?.elements?[i]?.address, "/prelude/" + base.elements[i].address | |
720 | |
721 test 'messageTransform works with identity function for single message', -> | |
722 message = | |
723 address: "/addr" | |
724 args: [] | |
725 buff = osc.toOscPacket message | |
726 buffeq (osc.applyTransform buff, osc.messageTransform (a) -> a), buff | |
727 | |
728 | |
729 test 'messageTransform works with bundles', -> | |
730 message = { | |
731 timetag : [0, 0] | |
732 elements : [ | |
733 {address : "test1"} | |
734 {address : "test2"} | |
735 ] | |
736 } | |
737 buff = osc.toOscPacket message | |
738 buffeq (osc.applyTransform buff, osc.messageTransform (a) -> a), buff | |
739 | |
740 test 'toTimetagBuffer works with a delta number', -> | |
741 delta = 1.2345 | |
742 buf = osc.toTimetagBuffer delta | |
743 | |
744 # assert dates are equal to within floating point conversion error | |
745 assertDatesEqual = (date1, date2) -> | |
746 assert Math.abs(date1.getTime() - date2.getTime()) <= 1, '' + date1 + ' != ' + date2 | |
747 | |
748 test 'toTimetagBuffer works with a Date', -> | |
749 date = new Date() | |
750 buf = osc.toTimetagBuffer date | |
751 | |
752 test 'toTimetagBuffer works with a timetag array', -> | |
753 timetag = [1000, 10001] | |
754 buf = osc.toTimetagBuffer timetag | |
755 | |
756 test 'toTimetagBuffer throws with invalid', -> | |
757 assert.throws -> osc.toTimetagBuffer "some bullshit" | |
758 | |
759 test 'deltaTimetag makes array from a delta', -> | |
760 delta = 1.2345 | |
761 ntp = osc.deltaTimetag(delta) | |
762 | |
763 test 'timetagToDate converts timetag to a Date', -> | |
764 date = new Date() | |
765 timetag = osc.dateToTimetag(date) | |
766 date2 = osc.timetagToDate(timetag) | |
767 assertDatesEqual date, date2 | |
768 | |
769 test 'timestampToTimetag converts a unix time to ntp array', -> | |
770 date = new Date() | |
771 timetag = osc.timestampToTimetag(date.getTime() / 1000) | |
772 date2 = osc.timetagToDate(timetag) | |
773 assertDatesEqual date, date2 | |
774 | |
775 test 'dateToTimetag converts date to ntp array', -> | |
776 date = new Date() | |
777 timetag = osc.dateToTimetag(date) | |
778 date2 = osc.timetagToDate(timetag) | |
779 assertDatesEqual date, date2 | |
780 | |
781 test 'timestamp <-> timeTag round trip', -> | |
782 now = (new Date()).getTime() / 1000 | |
783 near = (a, b) -> Math.abs(a - b) < 1e-6 | |
784 assert near(osc.timetagToTimestamp(osc.timestampToTimetag(now)), now) | |
785 | |
786 test 'splitTimetag returns timetag from a buffer', -> | |
787 timetag = [1000, 1001] | |
788 rest = "the rest" | |
789 buf = osc.concat [ | |
790 osc.toTimetagBuffer(timetag), | |
791 new Buffer(rest) | |
792 ] | |
793 {timetag: timetag2, rest: rest2} = osc.splitTimetag buf | |
794 assert.deepEqual timetag2, timetag |