File size: 2,536 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';

var should = require('should');
var Message = require('../../source/message');

var callbacks = {
  fn: function () {},
  register: function () {
    return 0;
  },
  getFn: function () {
    return callbacks.fn;
  }
};

describe('Message', function () {

  describe(':constructor', function () {
  });

  describe(':toString', function () {

    var message;

    before(function () {
      message = new Message(callbacks);
    });

    it('should serialize messages', function () {

      var string = message.serialize('test', 'one', 'two', 'three');
      string.should.equal('test("one","two","three")');

      string = message.serialize('test', 'one', function () {});
      string.should.equal('test("one").fn(0)');

      string = message.serialize(
        'something.amazing', ['woo', 'loo'], {oh: 'yeah'}, function () {}
      );
      string.should.equal('something.amazing(["woo","loo"],{"oh":"yeah"}).fn(0)');
    });

  });

  describe(':parse', function () {
    var message;

    before(function () {
      message = new Message(callbacks);
    });

    it('should parse messages', function () {

      var string = 'socket.test("one","two","three")';

      message.parse(string).should.eql({
        event: 'socket.test',
        arg1: 'one',
        arg2: 'two',
        arg3: 'three'
      });

      string = 'test("one","two")';
      message.parse(string).should.eql({
        event: 'test',
        arg1: 'one',
        arg2: 'two',
        arg3: undefined
      });

      string = 'test().fn(0)';
      message.parse(string).should.eql({
        event: 'test',
        arg1: callbacks.fn,
        arg2: undefined,
        arg3: undefined
      });

      string = 'test("arg1").fn(0)';
      message.parse(string).should.eql({
        event: 'test',
        arg1: 'arg1',
        arg2: callbacks.fn,
        arg3: undefined
      });

    });

    it('should handle invalid messages', function () {

      var string = 'socket.test(one,two,three)';
      message.parse(string).should.equal(false);

      string = 'foo bar';
      message.parse(string).should.equal(false);

      string = 'not.a.message';
      message.parse(string).should.equal(false);

      string = 'what_could ({happen:20}).fn( 2 )';
      message.parse(string).should.equal(false);

      string = '()';
      message.parse(string).should.equal(false);

      string = 2;
      message.parse(string).should.equal(false);

      string = undefined;
      message.parse(string).should.equal(false);

    });

  });
});