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