Welcome to mirror list, hosted at ThFree Co, Russian Federation.

utils.spec.js « test « mocha-3.1.2 « lib « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ccfd861a78b0f780c8c59e7644d3fa989cba3f3a (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
'use strict';

var mocha = require('..');
var utils = mocha.utils;
var path = require('path');
var JSON = require('json3');

describe('utils', function () {
  describe('.clean()', function () {
    var clean = utils.clean;
    it('should remove the wrapping function declaration', function () {
      clean('function  (one, two, three)  {\n//code\n}').should.equal('//code');
    });

    it('should handle newlines in the function declaration', function () {
      clean('function  (one, two, three)\n  {\n//code\n}').should.equal('//code');
    });

    it('should remove space character indentation from the function body', function () {
      clean('  //line1\n    //line2').should.equal('//line1\n  //line2');
    });

    it('should remove tab character indentation from the function body', function () {
      clean('\t//line1\n\t\t//line2').should.equal('//line1\n\t//line2');
    });

    it('should handle functions with tabs in their declarations', function () {
      clean('function\t(\t)\t{\n//code\n}').should.equal('//code');
    });

    it('should handle named functions without space after name', function () {
      clean('function withName() {\n//code\n}').should.equal('//code');
    });

    it('should handle named functions with space after name', function () {
      clean('function withName () {\n//code\n}').should.equal('//code');
    });

    it('should handle functions with no space between the end and the closing brace', function () {
      clean('function() {/*code*/}').should.equal('/*code*/');
    });

    it('should handle functions with parentheses in the same line', function () {
      clean('function() { if (true) { /* code */ } }').should.equal('if (true) { /* code */ }');
    });

    it('should handle empty functions', function () {
      clean('function() {}').should.equal('');
    });
  });

  describe('.isBuffer()', function () {
    var isBuffer = utils.isBuffer;
    it('should test if object is a Buffer', function () {
      isBuffer(new Buffer([0x01])).should.equal(true);
      isBuffer({}).should.equal(false);
    });
  });

  describe('.map()', function () {
    var map = utils.map;
    it('should behave same as Array.prototype.map', function () {
      var arr = [1, 2, 3];
      map(arr, JSON.stringify).should.eql(arr.map(JSON.stringify));
    });

    it('should call the callback with 3 arguments[currentValue, index, array]', function () {
      var index = 0;
      map([1, 2, 3], function (e, i, arr) {
        e.should.equal(arr[index]);
        i.should.equal(index++);
      });
    });

    it('should apply with the given scope', function () {
      var scope = {};
      map(['a', 'b', 'c'], function () {
        this.should.equal(scope);
      }, scope);
    });
  });

  describe('.parseQuery()', function () {
    var parseQuery = utils.parseQuery;
    it('should get queryString and return key-value object', function () {
      parseQuery('?foo=1&bar=2&baz=3').should.eql({
        foo: '1',
        bar: '2',
        baz: '3'
      });

      parseQuery('?r1=^@(?!.*\\)$)&r2=m{2}&r3=^co.*').should.eql({
        r1: '^@(?!.*\\)$)',
        r2: 'm{2}',
        r3: '^co.*'
      });
    });
  });

  describe('.stackTraceFilter()', function () {
    describe('on node', function () {
      var filter = utils.stackTraceFilter();

      describe('on POSIX OS', function () {
        before(function () {
          if (path.sep !== '/') {
            this.skip();
          }
        });

        it('should get a stack-trace as a string and prettify it', function () {
          var stack = [
            'AssertionError: foo bar',
            'at EventEmitter.<anonymous> (/usr/local/dev/test.js:16:12)',
            'at Context.<anonymous> (/usr/local/dev/test.js:19:5)',
            'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)',
            'Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)',
            '/usr/local/lib/node_modules/mocha/lib/runner.js:452:12',
            'next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)',
            '/usr/local/lib/node_modules/mocha/lib/runner.js:309:7',
            'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)',
            'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)',
            'at processImmediate [as _immediateCallback] (timers.js:321:17)'
          ];
          filter(stack.join('\n'))
            .should
            .equal(stack.slice(0, 3)
              .join('\n'));

          stack = [
            'AssertionError: bar baz',
            'at /usr/local/dev/some-test-file.js:25:8',
            'at tryCatcher (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/util.js:24:31)',
            'at Promise._resolveFromResolver (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:439:31)',
            'at new Promise (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:53:37)',
            'at yourFunction (/usr/local/dev/own/tmp/test1.js:24:13)',
            'at Context.<anonymous> (/usr/local/dev/some-test-file:30:4)',
            'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)',
            'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)',
            'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)',
            'at processImmediate [as _immediateCallback] (timers.js:321:17)'
          ];

          filter(stack.join('\n'))
            .should
            .equal(stack.slice(0, 7)
              .join('\n'));
        });

        it('does not ignore other bower_components and components',
          function () {
            var stack = [
              'Error: failed',
              'at assert (index.html:11:26)',
              'at Context.<anonymous> (test.js:17:18)',
              'at bower_components/should/should.js:4827:7',
              'at next (file:///.../bower_components/should/should.js:4766:23)',
              'at components/should/5.0.0/should.js:4827:7',
              'at next (file:///.../components/should/5.0.0/should.js:4766:23)',
              'at file:///.../bower_components/mocha/mocha.js:4794:5',
              'at timeslice (.../components/mocha/mocha.js:6218:27)',
              'at Test.require.register.Runnable.run (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4463:15)',
              'at Runner.require.register.Runner.runTest (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4892:10)',
              'at file:///.../components/mochajs/mocha/2.1.0/mocha.js:4970:12',
              'at next (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4817:14)'
            ];
            filter(stack.join('\n'))
              .should
              .equal(stack.slice(0, 7)
                .join('\n'));
          });

        it('should replace absolute with relative paths', function () {
          var stack = [
            'Error: ' + process.cwd() + '/bla.js has a problem',
            'at foo (' + process.cwd() + '/foo/index.js:13:226)',
            'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'
          ];

          var expected = [
            'Error: ' + process.cwd() + '/bla.js has a problem',
            'at foo (foo/index.js:13:226)',
            'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'
          ];

          filter(stack.join('\n'))
            .should
            .equal(expected.join('\n'));
        });
      });

      describe('on Windows', function () {
        before(function () {
          if (path.sep === '/') {
            this.skip();
          }
        });

        it('should work on Windows', function () {
          var stack = [
            'Error: failed',
            'at Context.<anonymous> (C:\\Users\\ishida\\src\\test\\test\\mytest.js:5:9)',
            'at callFn (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runnable.js:326:21)',
            'at Test.Runnable.run (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runnable.js:319:7)',
            'at Runner.runTest (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:422:10)',
            'at C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:528:12',
            'at next (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:342:14)',
            'at C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:352:7',
            'at next (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:284:14)',
            'at Immediate._onImmediate (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:320:5)'
          ];
          filter(stack.join('\n'))
            .should
            .equal(stack.slice(0, 2)
              .join('\n'));
        });
      });
    });

    describe('on browser', function () {
      var filter;
      before(function () {
        global.document = true;
        global.location = { href: 'localhost:3000/foo/bar/index.html' };
        filter = utils.stackTraceFilter();
      });
      it('does not strip out other bower_components', function () {
        var stack = [
          'Error: failed',
          'at assert (index.html:11:26)',
          'at Context.<anonymous> (test.js:17:18)',
          'at bower_components/should/should.js:4827:7',
          'at next (bower_components/should/should.js:4766:23)',
          'at components/should/5.0.0/should.js:4827:7',
          'at next (components/should/5.0.0/should.js:4766:23)',
          'at Runner.require.register.Runner.runTest (node_modules/mocha.js:4892:10)',
          'at localhost:3000/foo/bar/node_modules/mocha.js:4970:12',
          'at next (node_modules/mocha.js:4817:14)'
        ];
        filter(stack.join('\n')).should.equal(stack.slice(0, 7).join('\n'));
      });

      after(function () {
        delete global.document;
        delete global.location;
      });
    });
  });

  describe('.isPromise', function () {
    it('should return true if the value is Promise-ish', function () {
      utils.isPromise({then: function () {}}).should.be.true;
    });

    it('should return false if the value is not an object', function () {
      utils.isPromise(1).should.be.false;
    });

    it('should return false if the value is an object w/o a "then" function', function () {
      utils.isPromise({}).should.be.false;
    });
  });
});