l@271: # l@271: # This file was used for TDD and as such probably has limited utility as l@271: # actual unit tests. l@271: # l@271: try l@271: osc = require '../lib-cov/osc-utilities' l@271: catch e l@271: osc = require '../lib/osc-utilities' l@271: l@271: assert = require "assert" l@271: l@271: # Basic string tests. l@271: l@271: testString = (str, expected_len) -> l@271: str : str l@271: len : expected_len l@271: l@271: testData = [ l@271: testString("abc", 4) l@271: testString("abcd", 8) l@271: testString("abcde", 8) l@271: testString("abcdef", 8) l@271: testString("abcdefg", 8) l@271: ] l@271: l@271: testStringLength = (str, expected_len) -> l@271: oscstr = osc.toOscString(str) l@271: assert.strictEqual(oscstr.length, expected_len) l@271: l@271: test 'basic strings length', -> l@271: for data in testData l@271: testStringLength data.str, data.len l@271: l@271: l@271: testStringRoundTrip = (str, strict) -> l@271: oscstr = osc.toOscString(str) l@271: str2 = osc.splitOscString(oscstr, strict)?.string l@271: assert.strictEqual(str, str2) l@271: l@271: test 'basic strings round trip', -> l@271: for data in testData l@271: testStringRoundTrip data.str l@271: l@271: l@271: test 'non strings fail toOscString', -> l@271: assert.throws -> osc.toOscString(7) l@271: l@271: l@271: test 'strings with null characters don\'t fail toOscString by default', -> l@271: assert.notEqual(osc.toOscString("\u0000"), null) l@271: l@271: l@271: test 'strings with null characters fail toOscString in strict mode', -> l@271: assert.throws -> osc.toOscString("\u0000", true) l@271: l@271: l@271: test 'osc buffers with no null characters fail splitOscString in strict mode', -> l@271: assert.throws -> osc.splitOscString new Buffer("abc"), true l@271: l@271: l@271: test 'osc buffers with non-null characters after a null character fail fromOscString in strict mode', -> l@271: assert.throws -> osc.fromOscString new Buffer("abc\u0000abcd"), true l@271: l@271: l@271: test 'basic strings pass fromOscString in strict mode', -> l@271: for data in testData l@271: testStringRoundTrip data.str, true l@271: l@271: l@271: test 'osc buffers with non-four length fail in strict mode', -> l@271: assert.throws -> osc.fromOscString new Buffer("abcd\u0000\u0000"), true l@271: l@271: test 'splitOscString throws when passed a non-buffer', -> l@271: assert.throws -> osc.splitOscString "test" l@271: l@271: test 'splitOscString of an osc-string matches the string', -> l@271: split = osc.splitOscString osc.toOscString "testing it" l@271: assert.strictEqual(split?.string, "testing it") l@271: assert.strictEqual(split?.rest?.length, 0) l@271: l@271: l@271: test 'splitOscString works with an over-allocated buffer', -> l@271: buffer = osc.toOscString "testing it" l@271: overallocated = new Buffer(16) l@271: buffer.copy(overallocated) l@271: split = osc.splitOscString overallocated l@271: assert.strictEqual(split?.string, "testing it") l@271: assert.strictEqual(split?.rest?.length, 4) l@271: l@271: l@271: test 'splitOscString works with just a string by default', -> l@271: split = osc.splitOscString (new Buffer "testing it") l@271: assert.strictEqual(split?.string, "testing it") l@271: assert.strictEqual(split?.rest?.length, 0) l@271: l@271: l@271: test 'splitOscString strict fails for just a string', -> l@271: assert.throws -> osc.splitOscString (new Buffer "testing it"), true l@271: l@271: l@271: test 'splitOscString strict fails for string with not enough padding', -> l@271: assert.throws -> osc.splitOscString (new Buffer "testing \u0000\u0000"), true l@271: l@271: l@271: test 'splitOscString strict succeeds for strings with valid padding', -> l@271: split = osc.splitOscString (new Buffer "testing it\u0000\u0000aaaa"), true l@271: assert.strictEqual(split?.string, "testing it") l@271: assert.strictEqual(split?.rest?.length, 4) l@271: l@271: l@271: test 'splitOscString strict fails for string with invalid padding', -> l@271: assert.throws -> osc.splitOscString (new Buffer "testing it\u0000aaaaa"), true l@271: l@271: test 'concat throws when passed a single buffer', -> l@271: assert.throws -> osc.concat new Buffer "test" l@271: l@271: test 'concat throws when passed an array of non-buffers', -> l@271: assert.throws -> osc.concat ["bleh"] l@271: l@271: test 'toIntegerBuffer throws when passed a non-number', -> l@271: assert.throws -> osc.toIntegerBuffer "abcdefg" l@271: l@271: test 'splitInteger fails when sent a buffer that\'s too small', -> l@271: assert.throws -> osc.splitInteger new Buffer 3, "Int32" l@271: l@271: test 'splitOscArgument fails when given a bogus type', -> l@271: assert.throws -> osc.splitOscArgument new Buffer 8, "bogus" l@271: l@271: test 'fromOscMessage with no type string works', -> l@271: translate = osc.fromOscMessage osc.toOscString "/stuff" l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.deepEqual translate?.args, [] l@271: l@271: test 'fromOscMessage with type string and no args works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString "," l@271: oscmessage = new Buffer(oscaddr.length + osctype.length) l@271: oscaddr.copy oscmessage l@271: osctype.copy oscmessage, oscaddr.length l@271: translate = osc.fromOscMessage oscmessage l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.deepEqual translate?.args, [] l@271: l@271: test 'fromOscMessage with string argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",s" l@271: oscarg = osc.toOscString "argu" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "string" l@271: assert.strictEqual translate?.args?[0]?.value, "argu" l@271: l@271: test 'fromOscMessage with true argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",T" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "true" l@271: assert.strictEqual translate?.args?[0]?.value, true l@271: l@271: test 'fromOscMessage with false argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",F" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "false" l@271: assert.strictEqual translate?.args?[0]?.value, false l@271: l@271: test 'fromOscMessage with null argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",N" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "null" l@271: assert.strictEqual translate?.args?[0]?.value, null l@271: l@271: test 'fromOscMessage with bang argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",I" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "bang" l@271: assert.strictEqual translate?.args?[0]?.value, "bang" l@271: l@271: test 'fromOscMessage with blob argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",b" l@271: oscarg = osc.concat [(osc.toIntegerBuffer 4), new Buffer "argu"] l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "blob" l@271: assert.strictEqual (translate?.args?[0]?.value?.toString "utf8"), "argu" l@271: l@271: test 'fromOscMessage with integer argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",i" l@271: oscarg = osc.toIntegerBuffer 888 l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "integer" l@271: assert.strictEqual (translate?.args?[0]?.value), 888 l@271: l@271: test 'fromOscMessage with timetag argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",t" l@271: timetag = [8888, 9999] l@271: oscarg = osc.toTimetagBuffer timetag l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "timetag" l@271: assert.deepEqual (translate?.args?[0]?.value), timetag l@271: l@271: test 'fromOscMessage with mismatched array doesn\'t throw', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: assert.doesNotThrow (-> osc.fromOscMessage osc.concat( l@271: [oscaddr, osc.toOscString ",["])) l@271: assert.doesNotThrow (-> osc.fromOscMessage osc.concat( l@271: [oscaddr, osc.toOscString ",["])) l@271: l@271: test 'fromOscMessage with mismatched array throws in strict', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: assert.throws (-> osc.fromOscMessage (osc.concat( l@271: [oscaddr, osc.toOscString ",["])), true) l@271: assert.throws (-> osc.fromOscMessage (osc.concat( l@271: [oscaddr, osc.toOscString ",]"])), true) l@271: l@271: test 'fromOscMessage with empty array argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",[]" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "array" l@271: assert.strictEqual (translate?.args?[0]?.value?.length), 0 l@271: assert.deepEqual (translate?.args?[0]?.value), [] l@271: l@271: test 'fromOscMessage with bang array argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",[I]" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "array" l@271: assert.strictEqual (translate?.args?[0]?.value?.length), 1 l@271: assert.strictEqual (translate?.args?[0]?.value?[0]?.type), "bang" l@271: assert.strictEqual (translate?.args?[0]?.value?[0]?.value), "bang" l@271: l@271: test 'fromOscMessage with string array argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",[s]" l@271: oscarg = osc.toOscString "argu" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype, oscarg] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "array" l@271: assert.strictEqual (translate?.args?[0]?.value?.length), 1 l@271: assert.strictEqual (translate?.args?[0]?.value?[0]?.type), "string" l@271: assert.strictEqual (translate?.args?[0]?.value?[0]?.value), "argu" l@271: l@271: test 'fromOscMessage with nested array argument works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",[[I]]" l@271: translate = osc.fromOscMessage osc.concat [oscaddr, osctype] l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "array" l@271: assert.strictEqual translate?.args?[0]?.value?.length, 1 l@271: assert.strictEqual (translate?.args?[0]?.value?[0]?.type), "array" l@271: assert.strictEqual (translate?.args?[0]?.value?[0]?.value?.length), 1 l@271: assert.strictEqual (translate?.args?[0]?.value?[0]?.value?[0]?.type), "bang" l@271: assert.strictEqual (translate?.args?[0]?.value?[0]?.value?[0]?.value), "bang" l@271: l@271: test 'fromOscMessage with multiple args works', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString ",sbi" l@271: oscargs = [ l@271: (osc.toOscString "argu") l@271: (osc.concat [(osc.toIntegerBuffer 4), new Buffer "argu"]) l@271: (osc.toIntegerBuffer 888) l@271: ] l@271: l@271: oscbuffer = osc.concat [oscaddr, osctype, (osc.concat oscargs)] l@271: translate = osc.fromOscMessage oscbuffer l@271: assert.strictEqual translate?.address, "/stuff" l@271: assert.strictEqual translate?.args?[0]?.type, "string" l@271: assert.strictEqual (translate?.args?[0]?.value), "argu" l@271: l@271: test 'fromOscMessage strict fails if type string has no comma', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString "fake" l@271: assert.throws -> l@271: osc.fromOscMessage (osc.concat [oscaddr, osctype]), true l@271: l@271: test 'fromOscMessage non-strict works if type string has no comma', -> l@271: oscaddr = osc.toOscString "/stuff" l@271: osctype = osc.toOscString "fake" l@271: message = osc.fromOscMessage (osc.concat [oscaddr, osctype]) l@271: assert.strictEqual message.address, "/stuff" l@271: assert.strictEqual message.args.length, 0 l@271: l@271: test 'fromOscMessage strict fails if type address doesn\'t begin with /', -> l@271: oscaddr = osc.toOscString "stuff" l@271: osctype = osc.toOscString "," l@271: assert.throws -> l@271: osc.fromOscMessage (osc.concat [oscaddr, osctype]), true l@271: l@271: test 'fromOscBundle works with no messages', -> l@271: oscbundle = osc.toOscString "#bundle" l@271: timetag = [0, 0] l@271: osctimetag = osc.toTimetagBuffer timetag l@271: buffer = osc.concat [oscbundle, osctimetag] l@271: translate = osc.fromOscBundle buffer l@271: assert.deepEqual translate?.timetag, timetag l@271: assert.deepEqual translate?.elements, [] l@271: l@271: test 'fromOscBundle works with single message', -> l@271: oscbundle = osc.toOscString "#bundle" l@271: timetag = [0, 0] l@271: osctimetag = osc.toTimetagBuffer timetag l@271: oscaddr = osc.toOscString "/addr" l@271: osctype = osc.toOscString "," l@271: oscmessage = osc.concat [oscaddr, osctype] l@271: osclen = osc.toIntegerBuffer oscmessage.length l@271: buffer = osc.concat [oscbundle, osctimetag, osclen, oscmessage] l@271: translate = osc.fromOscBundle buffer l@271: assert.deepEqual translate?.timetag, timetag l@271: assert.strictEqual translate?.elements?.length, 1 l@271: assert.strictEqual translate?.elements?[0]?.address, "/addr" l@271: l@271: test 'fromOscBundle works with multiple messages', -> l@271: oscbundle = osc.toOscString "#bundle" l@271: timetag = [0, 0] l@271: osctimetag = osc.toTimetagBuffer timetag l@271: oscaddr1 = osc.toOscString "/addr" l@271: osctype1 = osc.toOscString "," l@271: oscmessage1 = osc.concat [oscaddr1, osctype1] l@271: osclen1 = osc.toIntegerBuffer oscmessage1.length l@271: oscaddr2 = osc.toOscString "/addr2" l@271: osctype2 = osc.toOscString "," l@271: oscmessage2 = osc.concat [oscaddr2, osctype2] l@271: osclen2 = osc.toIntegerBuffer oscmessage2.length l@271: buffer = osc.concat [oscbundle, osctimetag, osclen1, oscmessage1, osclen2, oscmessage2] l@271: translate = osc.fromOscBundle buffer l@271: assert.deepEqual translate?.timetag, timetag l@271: assert.strictEqual translate?.elements?.length, 2 l@271: assert.strictEqual translate?.elements?[0]?.address, "/addr" l@271: assert.strictEqual translate?.elements?[1]?.address, "/addr2" l@271: l@271: test 'fromOscBundle works with nested bundles', -> l@271: oscbundle = osc.toOscString "#bundle" l@271: timetag = [0, 0] l@271: osctimetag = osc.toTimetagBuffer timetag l@271: oscaddr1 = osc.toOscString "/addr" l@271: osctype1 = osc.toOscString "," l@271: oscmessage1 = osc.concat [oscaddr1, osctype1] l@271: osclen1 = osc.toIntegerBuffer oscmessage1.length l@271: oscbundle2 = osc.toOscString "#bundle" l@271: timetag2 = [0, 0] l@271: osctimetag2 = osc.toTimetagBuffer timetag2 l@271: oscmessage2 = osc.concat [oscbundle2, osctimetag2] l@271: osclen2 = osc.toIntegerBuffer oscmessage2.length l@271: buffer = osc.concat [oscbundle, osctimetag, osclen1, oscmessage1, osclen2, oscmessage2] l@271: translate = osc.fromOscBundle buffer l@271: assert.deepEqual translate?.timetag, timetag l@271: assert.strictEqual translate?.elements?.length, 2 l@271: assert.strictEqual translate?.elements?[0]?.address, "/addr" l@271: assert.deepEqual translate?.elements?[1]?.timetag, timetag2 l@271: l@271: test 'fromOscBundle works with non-understood messages', -> l@271: oscbundle = osc.toOscString "#bundle" l@271: timetag = [0, 0] l@271: osctimetag = osc.toTimetagBuffer timetag l@271: oscaddr1 = osc.toOscString "/addr" l@271: osctype1 = osc.toOscString "," l@271: oscmessage1 = osc.concat [oscaddr1, osctype1] l@271: osclen1 = osc.toIntegerBuffer oscmessage1.length l@271: oscaddr2 = osc.toOscString "/addr2" l@271: osctype2 = osc.toOscString ",α" l@271: oscmessage2 = osc.concat [oscaddr2, osctype2] l@271: osclen2 = osc.toIntegerBuffer oscmessage2.length l@271: buffer = osc.concat [oscbundle, osctimetag, osclen1, oscmessage1, osclen2, oscmessage2] l@271: translate = osc.fromOscBundle buffer l@271: assert.deepEqual translate?.timetag, timetag l@271: assert.strictEqual translate?.elements?.length, 1 l@271: assert.strictEqual translate?.elements?[0]?.address, "/addr" l@271: l@271: test 'fromOscBundle fails with bad bundle ID', -> l@271: oscbundle = osc.toOscString "#blunder" l@271: assert.throws -> osc.fromOscBundle oscbundle l@271: l@271: test 'fromOscBundle fails with ridiculous sizes', -> l@271: timetag = [0, 0] l@271: oscbundle = osc.concat [ l@271: osc.toOscString "#bundle" l@271: osc.toTimetagBuffer timetag l@271: osc.toIntegerBuffer 999999 l@271: ] l@271: assert.throws -> osc.fromOscBundle oscbundle l@271: l@271: roundTripMessage = (args) -> l@271: oscMessage = { l@271: address : "/addr" l@271: args : args l@271: } l@271: roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage), true l@271: assert.strictEqual roundTrip?.address, "/addr" l@271: assert.strictEqual roundTrip?.args?.length, args.length l@271: for i in [0...args.length] l@271: comp = if args[i]?.value? then args[i].value else args[i] l@271: assert.strictEqual roundTrip?.args?[i]?.type, args[i].type if args[i]?.type? l@271: if Buffer.isBuffer comp l@271: for j in [0...comp.length] l@271: assert.deepEqual roundTrip?.args?[i]?.value?[j], comp[j] l@271: else l@271: assert.deepEqual roundTrip?.args?[i]?.value, comp l@271: l@271: test 'toOscArgument fails when given bogus type', -> l@271: assert.throws -> osc.toOscArgument "bleh", "bogus" l@271: l@271: # we tested fromOsc* manually, so just use roundtrip testing for toOsc* l@271: test 'toOscMessage with no args works', -> l@271: roundTripMessage [] l@271: l@271: test 'toOscMessage strict with null argument throws', -> l@271: assert.throws -> osc.toOscMessage {address : "/addr", args : [null]}, true l@271: l@271: test 'toOscMessage with string argument works', -> l@271: roundTripMessage ["strr"] l@271: l@271: test 'toOscMessage with empty array argument works', -> l@271: roundTripMessage [[]] l@271: l@271: test 'toOscMessage with array value works', -> l@271: roundTripMessage [{value:[]}] l@271: l@271: test 'toOscMessage with string array argument works', -> l@271: roundTripMessage [[{type:"string", value:"hello"}, l@271: {type:"string", value:"goodbye"}]] l@271: l@271: test 'toOscMessage with multi-type array argument works', -> l@271: roundTripMessage [[{type:"string", value:"hello"}, l@271: {type:"integer", value:7}]] l@271: l@271: test 'toOscMessage with nested array argument works', -> l@271: roundTripMessage [[{type:"array", value:[{type:"string", value:"hello"}]}]] l@271: l@271: buffeq = (buff, exp_buff) -> l@271: assert.strictEqual buff.length, exp_buff.length l@271: for i in [0...exp_buff.length] l@271: assert.equal buff[i], exp_buff[i] l@271: l@271: test 'toOscMessage with bad layout works', -> l@271: oscMessage = { l@271: address : "/addr" l@271: args : [ l@271: "strr" l@271: ] l@271: } l@271: roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage), true l@271: assert.strictEqual roundTrip?.address, "/addr" l@271: assert.strictEqual roundTrip?.args?.length, 1 l@271: assert.strictEqual roundTrip?.args?[0]?.value, "strr" l@271: l@271: test 'toOscMessage with single numeric argument works', -> l@271: oscMessage = { l@271: address : "/addr" l@271: args : 13 l@271: } l@271: roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage) l@271: assert.strictEqual roundTrip?.address, "/addr" l@271: assert.strictEqual roundTrip?.args?.length, 1 l@271: assert.strictEqual roundTrip?.args?[0]?.value, 13 l@271: assert.strictEqual roundTrip?.args?[0]?.type, "float" l@271: l@271: test 'toOscMessage with args shortcut works', -> l@271: oscMessage = { l@271: address : "/addr" l@271: args : 13 l@271: } l@271: roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage) l@271: assert.strictEqual roundTrip?.address, "/addr" l@271: assert.strictEqual roundTrip?.args?.length, 1 l@271: assert.strictEqual roundTrip?.args?[0]?.value, 13 l@271: assert.strictEqual roundTrip?.args?[0]?.type, "float" l@271: l@271: test 'toOscMessage with single blob argument works', -> l@271: buff = new Buffer 18 l@271: oscMessage = { l@271: address : "/addr" l@271: args : buff l@271: } l@271: roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage) l@271: assert.strictEqual roundTrip?.address, "/addr" l@271: assert.strictEqual roundTrip?.args?.length, 1 l@271: buffeq roundTrip?.args?[0]?.value, buff l@271: assert.strictEqual roundTrip?.args?[0]?.type, "blob" l@271: l@271: test 'toOscMessage with single string argument works', -> l@271: oscMessage = { l@271: address : "/addr" l@271: args : "strr" l@271: } l@271: roundTrip = osc.fromOscMessage (osc.toOscMessage oscMessage) l@271: assert.strictEqual roundTrip?.address, "/addr" l@271: assert.strictEqual roundTrip?.args?.length, 1 l@271: assert.strictEqual roundTrip?.args?[0]?.value, "strr" l@271: assert.strictEqual roundTrip?.args?[0]?.type, "string" l@271: l@271: test 'toOscMessage with integer argument works', -> l@271: roundTripMessage [8] l@271: l@271: test 'toOscMessage with buffer argument works', -> l@271: # buffer will have random contents, but that's okay. l@271: roundTripMessage [new Buffer 16] l@271: l@271: test 'toOscMessage strict with type true and value false throws', -> l@271: assert.throws -> osc.toOscMessage {address: "/addr/", args: {type : "true", value : false}}, true l@271: l@271: test 'toOscMessage strict with type false with value true throws', -> l@271: assert.throws -> osc.toOscMessage {address: "/addr/", args: {type : "false", value : true}}, true l@271: l@271: test 'toOscMessage with type true works', -> l@271: roundTrip = osc.fromOscMessage osc.toOscMessage {address: "/addr", args : true} l@271: assert.strictEqual roundTrip.args.length, 1 l@271: assert.strictEqual roundTrip.args[0].value, true l@271: assert.strictEqual roundTrip.args[0].type, "true" l@271: l@271: test 'toOscMessage with type false works', -> l@271: roundTrip = osc.fromOscMessage osc.toOscMessage {address: "/addr", args : false} l@271: assert.strictEqual roundTrip.args.length, 1 l@271: assert.strictEqual roundTrip.args[0].value, false l@271: assert.strictEqual roundTrip.args[0].type, "false" l@271: l@271: test 'toOscMessage with type bang argument works', -> l@271: roundTrip = osc.fromOscMessage osc.toOscMessage {address: "/addr", args : {type:"bang"}} l@271: assert.strictEqual roundTrip.args.length, 1 l@271: assert.strictEqual roundTrip.args[0].value, "bang" l@271: assert.strictEqual roundTrip.args[0].type, "bang" l@271: l@271: test 'toOscMessage with type timetag argument works', -> l@271: roundTripMessage [{type: "timetag", value: [8888, 9999]}] l@271: l@271: test 'toOscMessage with type double argument works', -> l@271: roundTripMessage [{type: "double", value: 8888}] l@271: l@271: test 'toOscMessage strict with type null with value true throws', -> l@271: assert.throws -> osc.toOscMessage({address: "/addr/", args: {type : "null", value : true}}, true) l@271: l@271: test 'toOscMessage with type null works', -> l@271: roundTrip = osc.fromOscMessage osc.toOscMessage {address: "/addr", args : null} l@271: assert.strictEqual roundTrip.args.length, 1 l@271: assert.strictEqual roundTrip.args[0].value, null l@271: assert.strictEqual roundTrip.args[0].type, "null" l@271: l@271: test 'toOscMessage with float argument works', -> l@271: roundTripMessage [{value : 6, type : "float"}] l@271: l@271: test 'toOscMessage just a string works', -> l@271: message = osc.fromOscMessage osc.toOscMessage "bleh" l@271: assert.strictEqual message.address, "bleh" l@271: assert.strictEqual message.args.length, 0 l@271: l@271: test 'toOscMessage with multiple args works', -> l@271: roundTripMessage ["str", 7, (new Buffer 30), 6] l@271: l@271: test 'toOscMessage with integer argument works', -> l@271: roundTripMessage [{value : 7, type: "integer"}] l@271: l@271: test 'toOscMessage fails with no address', -> l@271: assert.throws -> osc.toOscMessage {args : []} l@271: l@271: toOscMessageThrowsHelper = (arg) -> l@271: assert.throws -> osc.toOscMessage( l@271: address : "/addr" l@271: args : [arg] l@271: ) l@271: l@271: test 'toOscMessage fails when string type is specified but wrong', -> l@271: toOscMessageThrowsHelper( l@271: value : 7 l@271: type : "string" l@271: ) l@271: l@271: test 'toOscMessage fails when integer type is specified but wrong', -> l@271: toOscMessageThrowsHelper( l@271: value : "blah blah" l@271: type : "integer" l@271: ) l@271: l@271: test 'toOscMessage fails when float type is specified but wrong', -> l@271: toOscMessageThrowsHelper( l@271: value : "blah blah" l@271: type : "float" l@271: ) l@271: l@271: test 'toOscMessage fails when timetag type is specified but wrong', -> l@271: toOscMessageThrowsHelper( l@271: value : "blah blah" l@271: type : "timetag" l@271: ) l@271: l@271: test 'toOscMessage fails when double type is specified but wrong', -> l@271: toOscMessageThrowsHelper( l@271: value : "blah blah" l@271: type : "double" l@271: ) l@271: l@271: test 'toOscMessage fails when blob type is specified but wrong', -> l@271: toOscMessageThrowsHelper( l@271: value : "blah blah" l@271: type : "blob" l@271: ) l@271: l@271: test 'toOscMessage fails argument is a random type', -> l@271: toOscMessageThrowsHelper( l@271: random_field : 42 l@271: "is pretty random" : 888 l@271: ) l@271: l@271: roundTripBundle = (elems) -> l@271: oscMessage = { l@271: timetag : [0, 0] l@271: elements : elems l@271: } l@271: roundTrip = osc.fromOscBundle (osc.toOscBundle oscMessage), true l@271: assert.deepEqual roundTrip?.timetag, [0, 0] l@271: length = if typeof elems is "object" then elems.length else 1 l@271: assert.strictEqual roundTrip?.elements?.length, length l@271: for i in [0...length] l@271: if typeof elems is "object" l@271: assert.deepEqual roundTrip?.elements?[i]?.timetag, elems[i].timetag l@271: assert.strictEqual roundTrip?.elements?[i]?.address, elems[i].address l@271: else l@271: assert.strictEqual roundTrip?.elements?[i]?.address, elems l@271: l@271: test 'toOscBundle with no elements works', -> l@271: roundTripBundle [] l@271: l@271: test 'toOscBundle with just a string works', -> l@271: roundTripBundle "/address" l@271: l@271: test 'toOscBundle with just a number fails', -> l@271: assert.throws -> roundTripBundle 78 l@271: l@271: test 'toOscBundle with one message works', -> l@271: roundTripBundle [{address : "/addr"}] l@271: l@271: test 'toOscBundle with nested bundles works', -> l@271: roundTripBundle [{address : "/addr"}, {timetag : [8888, 9999]}] l@271: l@271: test 'toOscBundle with bogus packets works', -> l@271: roundTrip = osc.fromOscBundle osc.toOscBundle { l@271: timetag : [0, 0] l@271: elements : [{timetag : [0, 0]}, {maddress : "/addr"}] l@271: } l@271: assert.strictEqual roundTrip.elements.length, 1 l@271: assert.deepEqual roundTrip.elements[0].timetag, [0, 0] l@271: l@271: test 'toOscBundle strict fails without timetags', -> l@271: assert.throws -> osc.toOscBundle {elements :[]}, true l@271: l@271: test 'identity applyTransform works with single message', -> l@271: testBuffer = osc.toOscString "/message" l@271: assert.strictEqual (osc.applyTransform testBuffer, (a) -> a), testBuffer l@271: l@271: test 'nullary applyTransform works with single message', -> l@271: testBuffer = osc.toOscString "/message" l@271: assert.strictEqual (osc.applyTransform testBuffer, (a) -> new Buffer 0).length, 0 l@271: l@271: test 'toOscPacket works when explicitly set to bundle', -> l@271: roundTrip = osc.fromOscBundle osc.toOscPacket {timetag: 0, oscType:"bundle", elements :[]}, true l@271: assert.strictEqual roundTrip.elements.length, 0 l@271: l@271: test 'toOscPacket works when explicitly set to message', -> l@271: roundTrip = osc.fromOscPacket osc.toOscPacket {address: "/bleh", oscType:"message", args :[]}, true l@271: assert.strictEqual roundTrip.args.length, 0 l@271: assert.strictEqual roundTrip.address, "/bleh" l@271: l@271: test 'identity applyTransform works with a simple bundle', -> l@271: base = { l@271: timetag : [0, 0] l@271: elements : [ l@271: {address : "test1"} l@271: {address : "test2"} l@271: ] l@271: } l@271: transformed = osc.fromOscPacket (osc.applyTransform (osc.toOscPacket base), (a) -> a) l@271: l@271: assert.deepEqual transformed?.timetag, [0, 0] l@271: assert.strictEqual transformed?.elements?.length, base.elements.length l@271: for i in [0...base.elements.length] l@271: assert.equal transformed?.elements?[i]?.timetag, base.elements[i].timetag l@271: assert.strictEqual transformed?.elements?[i]?.address, base.elements[i].address l@271: l@271: test 'applyMessageTranformerToBundle fails on bundle without tag', -> l@271: func = osc.applyMessageTranformerToBundle ((a) -> a) l@271: assert.throws -> func osc.concat [osc.toOscString "#grundle", osc.toIntegerBuffer 0, "Int64"] l@271: l@271: test 'addressTransform works with identity', -> l@271: testBuffer = osc.concat [ l@271: osc.toOscString "/message" l@271: new Buffer "gobblegobblewillsnever\u0000parse blah lbha" l@271: ] l@271: transformed = osc.applyTransform testBuffer, osc.addressTransform((a) -> a) l@271: for i in [0...testBuffer.length] l@271: assert.equal transformed[i], testBuffer[i] l@271: l@271: l@271: test 'addressTransform works with bundles', -> l@271: base = { l@271: timetag : [0, 0] l@271: elements : [ l@271: {address : "test1"} l@271: {address : "test2"} l@271: ] l@271: } l@271: transformed = osc.fromOscPacket (osc.applyTransform (osc.toOscPacket base), osc.addressTransform((a) -> "/prelude/" + a)) l@271: l@271: assert.deepEqual transformed?.timetag, [0, 0] l@271: assert.strictEqual transformed?.elements?.length, base.elements.length l@271: for i in [0...base.elements.length] l@271: assert.equal transformed?.elements?[i]?.timetag, base.elements[i].timetag l@271: assert.strictEqual transformed?.elements?[i]?.address, "/prelude/" + base.elements[i].address l@271: l@271: test 'messageTransform works with identity function for single message', -> l@271: message = l@271: address: "/addr" l@271: args: [] l@271: buff = osc.toOscPacket message l@271: buffeq (osc.applyTransform buff, osc.messageTransform (a) -> a), buff l@271: l@271: l@271: test 'messageTransform works with bundles', -> l@271: message = { l@271: timetag : [0, 0] l@271: elements : [ l@271: {address : "test1"} l@271: {address : "test2"} l@271: ] l@271: } l@271: buff = osc.toOscPacket message l@271: buffeq (osc.applyTransform buff, osc.messageTransform (a) -> a), buff l@271: l@271: test 'toTimetagBuffer works with a delta number', -> l@271: delta = 1.2345 l@271: buf = osc.toTimetagBuffer delta l@271: l@271: # assert dates are equal to within floating point conversion error l@271: assertDatesEqual = (date1, date2) -> l@271: assert Math.abs(date1.getTime() - date2.getTime()) <= 1, '' + date1 + ' != ' + date2 l@271: l@271: test 'toTimetagBuffer works with a Date', -> l@271: date = new Date() l@271: buf = osc.toTimetagBuffer date l@271: l@271: test 'toTimetagBuffer works with a timetag array', -> l@271: timetag = [1000, 10001] l@271: buf = osc.toTimetagBuffer timetag l@271: l@271: test 'toTimetagBuffer throws with invalid', -> l@271: assert.throws -> osc.toTimetagBuffer "some bullshit" l@271: l@271: test 'deltaTimetag makes array from a delta', -> l@271: delta = 1.2345 l@271: ntp = osc.deltaTimetag(delta) l@271: l@271: test 'timetagToDate converts timetag to a Date', -> l@271: date = new Date() l@271: timetag = osc.dateToTimetag(date) l@271: date2 = osc.timetagToDate(timetag) l@271: assertDatesEqual date, date2 l@271: l@271: test 'timestampToTimetag converts a unix time to ntp array', -> l@271: date = new Date() l@271: timetag = osc.timestampToTimetag(date.getTime() / 1000) l@271: date2 = osc.timetagToDate(timetag) l@271: assertDatesEqual date, date2 l@271: l@271: test 'dateToTimetag converts date to ntp array', -> l@271: date = new Date() l@271: timetag = osc.dateToTimetag(date) l@271: date2 = osc.timetagToDate(timetag) l@271: assertDatesEqual date, date2 l@271: l@271: test 'timestamp <-> timeTag round trip', -> l@271: now = (new Date()).getTime() / 1000 l@271: near = (a, b) -> Math.abs(a - b) < 1e-6 l@271: assert near(osc.timetagToTimestamp(osc.timestampToTimetag(now)), now) l@271: l@271: test 'splitTimetag returns timetag from a buffer', -> l@271: timetag = [1000, 1001] l@271: rest = "the rest" l@271: buf = osc.concat [ l@271: osc.toTimetagBuffer(timetag), l@271: new Buffer(rest) l@271: ] l@271: {timetag: timetag2, rest: rest2} = osc.splitTimetag buf l@271: assert.deepEqual timetag2, timetag