Blame view

node_modules/msgpack5/test/ext-custom-encode-check.js 1.67 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  'use strict'
  
  var test = require('tape').test
  var msgpack = require('../')
  
  test('encode/decode ext with a custom object check', function (t) {
    var encoder = msgpack()
    var all = []
  
    function MyType (data) {
      this.data = data
    }
  
    function checkForMyType (obj) {
      return obj instanceof MyType
    }
  
    function mytypeEncode (obj) {
      var buf = new Buffer(2)
      buf.writeUInt8(0x42, 0)
      buf.writeUInt8(obj.data, 1)
      return buf
    }
  
    function mytypeDecode (data) {
      return new MyType(data.readUInt8(0))
    }
  
    encoder.registerEncoder(checkForMyType, mytypeEncode)
    encoder.registerDecoder(0x42, mytypeDecode)
  
    all.push(new MyType(0))
    all.push(new MyType(1))
    all.push(new MyType(42))
  
    all.forEach(function (orig) {
      t.test('encoding a custom obj encoded as ' + orig.data, function (t) {
        var buf = encoder.encode(orig)
        t.equal(buf.length, 3, 'must have the right length')
        t.equal(buf.readUInt8(0), 0xd4, 'must have the fixext header')
        t.equal(buf.readUInt8(1), 0x42, 'must include the custom type id')
        t.equal(buf.readUInt8(2), orig.data, 'must decode correctly')
        t.end()
      })
  
      t.test('decoding a custom obj encoded as ' + orig.data, function (t) {
        var buf = new Buffer(3)
        buf[0] = 0xd4
        buf[1] = 0x42
        buf.writeUInt8(orig.data, 2)
        t.deepEqual(encoder.decode(buf), orig, 'must decode correctly')
        t.ok(encoder.decode(buf) instanceof MyType, 'must have the correct prototype')
        t.end()
      })
  
      t.test('mirror test with a custom obj containing ' + orig.data, function (t) {
        t.deepEqual(encoder.decode(encoder.encode(orig)), orig, 'must stay the same')
        t.end()
      })
    })
  
    t.end()
  })