Projekt

Obecné

Profil

Stáhnout (6.16 KB) Statistiky
| Větev: | Revize:
1
var assert = require('assert');
2
var ourProcess = require('./browser');
3
describe('test against our process', function () {
4
    test(ourProcess);
5
});
6
if (!process.browser) {
7
  describe('test against node', function () {
8
    test(process);
9
  });
10
  vmtest();
11
}
12
function test (ourProcess) {
13
    describe('test arguments', function () {
14
        it ('works', function (done) {
15
          var order = 0;
16

    
17

    
18
          ourProcess.nextTick(function (num) {
19
              assert.equal(num, order++, 'first one works');
20
              ourProcess.nextTick(function (num) {
21
                assert.equal(num, order++, 'recursive one is 4th');
22
              }, 3);
23
          }, 0);
24
          ourProcess.nextTick(function (num) {
25
              assert.equal(num, order++, 'second one starts');
26
              ourProcess.nextTick(function (num) {
27
                assert.equal(num, order++, 'this is third');
28
                ourProcess.nextTick(function (num) {
29
                    assert.equal(num, order++, 'this is last');
30
                    done();
31
                }, 5);
32
            }, 4);
33
          }, 1);
34
          ourProcess.nextTick(function (num) {
35

    
36
              assert.equal(num, order++, '3rd schedualed happens after the error');
37
          }, 2);
38
        });
39
    });
40
if (!process.browser) {
41
    describe('test errors', function (t) {
42
        it ('works', function (done) {
43
        var order = 0;
44
        process.removeAllListeners('uncaughtException');
45
        process.once('uncaughtException', function(err) {
46
            assert.equal(2, order++, 'error is third');
47
            ourProcess.nextTick(function () {
48
                assert.equal(5, order++, 'schedualed in error is last');
49
                done();
50
            });
51
        });
52
        ourProcess.nextTick(function () {
53
            assert.equal(0, order++, 'first one works');
54
            ourProcess.nextTick(function () {
55
            assert.equal(4, order++, 'recursive one is 4th');
56
            });
57
        });
58
        ourProcess.nextTick(function () {
59
            assert.equal(1, order++, 'second one starts');
60
            throw(new Error('an error is thrown'));
61
        });
62
        ourProcess.nextTick(function () {
63
            assert.equal(3, order++, '3rd schedualed happens after the error');
64
        });
65
        });
66
    });
67
}
68
    describe('rename globals', function (t) {
69
      var oldTimeout = setTimeout;
70
      var oldClear = clearTimeout;
71

    
72
      it('clearTimeout', function (done){
73

    
74
        var ok = true;
75
        clearTimeout = function () {
76
          ok = false;
77
        }
78
        var ran = false;
79
        function cleanup() {
80
          clearTimeout = oldClear;
81
          var err;
82
          try {
83
            assert.ok(ok, 'fake clearTimeout ran');
84
            assert.ok(ran, 'should have run');
85
          } catch (e) {
86
            err = e;
87
          }
88
          done(err);
89
        }
90
        setTimeout(cleanup, 1000);
91
        ourProcess.nextTick(function () {
92
          ran = true;
93
        });
94
      });
95
      it('just setTimeout', function (done){
96

    
97

    
98
        setTimeout = function () {
99
          setTimeout = oldTimeout;
100
          try {
101
            assert.ok(false, 'fake setTimeout called')
102
          } catch (e) {
103
            done(e);
104
          }
105

    
106
        }
107

    
108
        ourProcess.nextTick(function () {
109
          setTimeout = oldTimeout;
110
          done();
111
        });
112
      });
113
    });
114
}
115
function vmtest() {
116
  var vm = require('vm');
117
  var fs = require('fs');
118
  var process =  fs.readFileSync('./browser.js', {encoding: 'utf8'});
119

    
120

    
121
  describe('should work in vm in strict mode with no globals', function () {
122
    it('should parse', function (done) {
123
      var str = '"use strict";var module = {exports:{}};';
124
      str += process;
125
      str += 'this.works = process.browser;';
126
      var script = new vm.Script(str);
127
      var context = {
128
        works: false
129
      };
130
      script.runInNewContext(context);
131
      assert.ok(context.works);
132
      done();
133
    });
134
    it('setTimeout throws error', function (done) {
135
      var str = '"use strict";var module = {exports:{}};';
136
      str += process;
137
      str += 'try {process.nextTick(function () {})} catch (e){this.works = e;}';
138
      var script = new vm.Script(str);
139
      var context = {
140
        works: false
141
      };
142
      script.runInNewContext(context);
143
      assert.ok(context.works);
144
      done();
145
    });
146
    it('should generally work', function (done) {
147
      var str = '"use strict";var module = {exports:{}};';
148
      str += process;
149
      str += 'process.nextTick(function () {assert.ok(true);done();})';
150
      var script = new vm.Script(str);
151
      var context = {
152
        clearTimeout: clearTimeout,
153
        setTimeout: setTimeout,
154
        done: done,
155
        assert: assert
156
      };
157
      script.runInNewContext(context);
158
    });
159
    it('late defs setTimeout', function (done) {
160
      var str = '"use strict";var module = {exports:{}};';
161
      str += process;
162
      str += 'var setTimeout = hiddenSetTimeout;process.nextTick(function () {assert.ok(true);done();})';
163
      var script = new vm.Script(str);
164
      var context = {
165
        clearTimeout: clearTimeout,
166
        hiddenSetTimeout: setTimeout,
167
        done: done,
168
        assert: assert
169
      };
170
      script.runInNewContext(context);
171
    });
172
    it('late defs clearTimeout', function (done) {
173
      var str = '"use strict";var module = {exports:{}};';
174
      str += process;
175
      str += 'var clearTimeout = hiddenClearTimeout;process.nextTick(function () {assert.ok(true);done();})';
176
      var script = new vm.Script(str);
177
      var context = {
178
        hiddenClearTimeout: clearTimeout,
179
        setTimeout: setTimeout,
180
        done: done,
181
        assert: assert
182
      };
183
      script.runInNewContext(context);
184
    });
185
    it('late defs setTimeout and then redefine', function (done) {
186
      var str = '"use strict";var module = {exports:{}};';
187
      str += process;
188
      str += 'var setTimeout = hiddenSetTimeout;process.nextTick(function () {setTimeout = function (){throw new Error("foo")};hiddenSetTimeout(function(){process.nextTick(function (){assert.ok(true);done();});});});';
189
      var script = new vm.Script(str);
190
      var context = {
191
        clearTimeout: clearTimeout,
192
        hiddenSetTimeout: setTimeout,
193
        done: done,
194
        assert: assert
195
      };
196
      script.runInNewContext(context);
197
    });
198
  });
199
}
(7-7/7)