-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_nodejs.py
More file actions
1737 lines (1669 loc) · 63.4 KB
/
test_nodejs.py
File metadata and controls
1737 lines (1669 loc) · 63.4 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is Komodo code.
#
# The Initial Developer of the Original Code is ActiveState Software Inc.
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2011
# ActiveState Software Inc. All Rights Reserved.
#
# Contributor(s):
# ActiveState Software Inc
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
"""Test some Node.js-specific codeintel handling."""
from __future__ import absolute_import
import os
import sys
import re
import operator
from os.path import join, dirname, abspath, exists, basename
from glob import glob
import unittest
import subprocess
import logging
from codeintel2.common import *
from codeintel2.util import indent, dedent, banner, markup_text, unmark_text
from codeintel2.environment import SimplePrefsEnvironment
from testlib import TestError, TestSkipped, TestFailed, tag
from citestsupport import CodeIntelTestCase, run, writefile
from distutils.version import LooseVersion
log = logging.getLogger("test")
def write_files(test_case, manifest={}, name="unnamed", env=None):
"""
Wrapper to write out the files for testing
@param hash of file name to text content
The file "test.js" will be run through unmark_text
@param name the name of the test
@param env Optional environment for test.js file.
@return tuple (buf, positions)
buf is a buffer of the resulting test.js
positions is the positions returned from unmark_text
"""
assert len(manifest) > 0, "No manifest"
assert "test.js" in manifest, "No test.js to run"
test_dir = join(test_case.test_dir, "test_nodejs_%s" % name)
test_js = None
for name, content in manifest.items():
content = dedent(content)
path = join(test_dir, name)
if name == "test.js":
content, positions = unmark_text(content)
test_js = path
writefile(path, content)
buf = test_case.mgr.buf_from_path(test_js, lang="Node.js", encoding="utf-8", env=env)
# Our files may include subdirectories, which won't get scanned by
# default (because curdirlib doesn't want to be recursive). Manually
# ensure everything is scanned here.
curdirlib = buf.libs[0] # XXX: make this not so fragile
dirs = set(curdirlib.dirs)
for name in manifest.keys():
dirname, basename = os.path.split(name)
absdir = join(test_dir, dirname).rstrip(os.path.sep)
if not absdir in dirs:
curdirlib.ensure_dir_scanned(absdir)
dirs.add(absdir)
curdirlib.dirs = tuple(dirs)
return (buf, positions)
class CodeIntelNodeJSTestCase(CodeIntelTestCase):
test_dir = join(os.getcwd(), "tmp")
# Many of these tests absoluately require NodeJS < 0.10. Create a fake
# node executable that outputs such a version number and point to it via
# the _ci_env_prefs_ dictionary.
fake_node = join(test_dir, "fake_node")
if not exists(fake_node):
if not exists(test_dir):
os.makedirs(test_dir)
f = open(fake_node, 'wb')
f.write(b'#!/bin/sh\n\necho v0.8.0')
f.close()
os.chmod(fake_node, 0o755)
_ci_env_prefs_ = {
'nodejsDefaultInterpreter': fake_node
}
class CplnTestCase(CodeIntelNodeJSTestCase):
lang = "Node.js"
def test_require(self):
"""
Check that require() works for relative paths
"""
manifest = {
"http.js": """
/* as generated by node_html_to_js.py */
var http_ = {};
http_.Server = function Server() {}
http_.Server.prototype = {}
/**
* Start a UNIX socket server listening for connections on the given path.
*/
http_.Server.prototype.listen = function() {}
exports = http_;
""",
"fs.js": """
/* possible alternative for manually written files */
exports = {
rename: function() {}
}
""",
"test.js": """
var my_http = require('./http');
var my_fs = require('./fs');
my_http.<1>;
my_fs.<2>;
""",
}
buf, positions = write_files(self, manifest=manifest, name="require")
self.assertCompletionsInclude2(buf, positions[1],
[("class", "Server"), ])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "rename"), ])
def test_require_nonvar(self):
"""
Test require() without intermediate assignment
"""
manifest = {
"test.js": """
require('./dummy').<1>;
""",
"dummy.js": """
exports = {
method: function() {}
};
""",
}
buf, positions = write_files(self, manifest=manifest, name="require_nonvar")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "method"), ])
def test_require_module_exports(self):
"""
Test exporting via module.exports
"""
manifest = {
"test.js": """
require('./dummy').<1>;
""",
"dummy.js": """
module.exports = {
method: function() {}
};
""",
}
buf, positions = write_files(self, manifest=manifest, name="require_module_exports")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "method"), ])
def test_require_not_buf_path(self):
"""
Test require() from a path that is not the buffer path
"""
manifest = {
"test.js": """
require('./subdir/proxy').<1>;
""",
"subdir/proxy.js": """
exports = require('../target');
""",
"target.js": """
exports = {
method: function() {}
};
""",
}
buf, positions = write_files(self, manifest=manifest, name="require_not_buf_path")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "method"), ])
def test_module_simple(self):
"""
Test require() using node_modules (simple case)
"""
manifest = {
"test.js": """
require('simple').<1>;
""",
"node_modules/simple/index.js": """
exports = {
simpleMethod: function() {}
};
""",
}
buf, positions = write_files(self, manifest=manifest, name="module_simple")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "simpleMethod"), ])
def test_module_package_manifest(self):
"""
Test require() on a module using package.json
"""
manifest = {
"test.js": """
require('simple').<1>;
""",
"node_modules/simple/package.json": """
{
"foopy": "pants",
"main": "./lib/file.js",
"name": "sillypants"
}
""",
"node_modules/simple/lib/file.js": """
exports = {
method: function() {}
};
""",
}
buf, positions = write_files(self, manifest=manifest, name="module_package_manifest")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "method"), ])
def test_require_prefer_core(self):
"""
Check that require() prefers core modules where available
"""
manifest = {
"test.js": """
require('http').<1>;
""",
"node_modules/http.js": """
exports = {}
""",
}
buf, positions = write_files(self, manifest=manifest, name="require_prefer_core")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "createServer"), ])
def test_modules_no_repeat_subdir(self):
"""
Check that we don't descend into foo/node_modules/node_modules
"""
manifest = {
"test.js": """
require('module').good.<1>;
require('module').bad.<2>;
""",
"node_modules/module.js": """
exports.good = require('good');
exports.bad = require('bad');
""",
"node_modules/good.js": """
exports = {
"other": function() {}
}
""",
"node_modules/node_modules/bad.js": """
exports = {
method: function() {}
}
""",
}
buf, positions = write_files(self, manifest=manifest, name="modules_no_repeat_subdir")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "other"), ])
self.assertCompletionsDoNotInclude2(buf, positions[2],
[("function", "method"), ])
def test_modules_updir(self):
"""
Test finding modules up the directory tree
This also tests that multiple files with the same base name works
"""
manifest = {
"test.js": """
require('entry').<1>;
""",
"node_modules/entry/index.js": """
exports = require('trampoline');
""",
"node_modules/entry/node_modules/trampoline/index.js": """
exports = require('bounce');
""",
"node_modules/entry/node_modules/trampoline/node_modules/bounce/index.js": """
exports = require('target');
""",
"node_modules/entry/node_modules/target.js": """
exports = {
method: function() {}
}
""",
}
buf, positions = write_files(self, manifest=manifest, name="modules_updir")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "method"), ])
@tag("bug90331")
def test_require_extras(self):
"""
Check that we can tack extra properties onto require()d objects
"""
manifest = {
"test.js": """
require('./foo').<1>;
""",
"foo.js": """
exports = require('./bar');
exports.foo = function() {};
""",
"bar.js": """
exports = {
bar: function() {}
}
""",
}
buf, positions = write_files(self, manifest=manifest, name="require_extras")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "foo"),
("function", "bar"),
])
def test_globals(self):
"""
Test that the documented globals are available
"""
manifest = {
"test.js": """
con<1>;
pro<2>;
req<3>;
__f<4>;
cle<5>;
set<6>;
__d<7>;
glo<8>;
Buf<9>;
mod<10>;
exp<11>;
""",
}
buf, positions = write_files(self, manifest=manifest, name="globals")
self.assertCompletionsInclude2(buf, positions[1],
[("variable", "console"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("variable", "process"),
])
self.assertCompletionsInclude2(buf, positions[3],
[("function", "require"),
])
self.assertCompletionsInclude2(buf, positions[4],
[("variable", "__filename"),
])
self.assertCompletionsInclude2(buf, positions[5],
[("variable", "clearTimeout"),
("variable", "clearInterval"),
])
self.assertCompletionsInclude2(buf, positions[6],
[("variable", "setTimeout"),
("variable", "setInterval"),
])
self.assertCompletionsInclude2(buf, positions[7],
[("variable", "__dirname"),
])
self.assertCompletionsInclude2(buf, positions[8],
[("variable", "global"),
])
self.assertCompletionsInclude2(buf, positions[9],
[("variable", "Buffer"),
])
self.assertCompletionsInclude2(buf, positions[10],
[("namespace", "module"),
])
self.assertCompletionsInclude2(buf, positions[11],
[("variable", "exports"),
])
def test_globals_props(self):
"""
Test that the imported globals have the right properties
"""
manifest = {
"test.js": """
require.<1>;
process.<2>;
console.<3>;
Buffer.<4>;
module.<5>;
""",
}
buf, positions = write_files(self, manifest=manifest, name="globals")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "resolve"),
("variable", "cache"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("variable", "stdout"),
("variable", "stderr"),
("variable", "stdin"),
("function", "exit"),
# the rest are tested in test_nodejs_process
])
self.assertCompletionsInclude2(buf, positions[3],
[("function", "log"),
("function", "info"),
("function", "warn"),
# the rest are tested in test_nodejs_console
])
self.assertCompletionsInclude2(buf, positions[4],
[("function", "isBuffer"),
("function", "byteLength"),
# the rest are tested in test_nodejs_buffer
])
self.assertCompletionsInclude2(buf, positions[5],
[("namespace", "exports"),
])
def test_global_accessor(self):
"""
Test that the Node.js global accessor, |global|, is usable
"""
manifest = {
"test.js": """
global.foo = new Array();
foo.<1>;
""",
}
buf, positions = write_files(self, manifest=manifest, name="global_accessor")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "concat"),
])
def test_globals_no_pollute(self):
"""
Test that the modules don't pollute the global namespace
"""
manifest = {
"test.js": """
tim<1>;
""",
}
buf, positions = write_files(self, manifest=manifest, name="globals_no_pollute")
self.assertCompletionsDoNotInclude2(buf, positions[1],
[("variable", "timers"),
])
@tag("bug90485")
def test_callback_types(self):
"""
Test for completion of callback arguments
"""
manifest = {
"test.js": """
var http = require('http');
http.createServer(function(req, res) {
req.<1>;
res.<2>;
})
""",
}
buf, positions = write_files(self, manifest=manifest, name="callback_types")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "pause"),
])
self.assertCompletionsDoNotInclude2(buf, positions[1],
[("function", "writeHead"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "writeContinue"),
("function", "writeHead"),
])
def test_anon_func_call_this_module(self):
"""
Test for handling anonymous function call wrapper around a module.
(This construct is used to prevent global namespace pollution.)
"""
manifest = {
"test.js": """
require('./dummy').<1>;
""",
"dummy.js": """
(function() {
module.exports = {
method: function() {}
};
}).call(this);
""",
}
buf, positions = write_files(self, manifest=manifest, name="anon_func_call_this_module")
self.assertCompletionsAre2(buf, positions[1],
[("function", "method"), ])
def test_namespace_mapping(self):
"""Test namespace mapping support."""
manifest = {
"test.js": """
require('ko/editor').<1>;
require('ko/menu').<2>;
require('ko/benchmark').<3>;
require('ko/dom').<4>;
""",
"sdk/editor.js": """
/**
* The editor sdk.
*
* @module ko/editor
*/
var sdkEditor = function(_scintilla, _scimoz) {
this.scimoz = function() { }
this.scintilla = function() { }
};
module.exports = new sdkEditor();
""",
"sdk/menu.js": """
/**
* The menu SDK allows you to easily register new menu items
*
* @module ko/menu
*/
(function() {
this.register = function() {}
this.unregister = function() {}
}).apply(module.exports)
""",
"sdk/benchmark.js": """
exports.startTiming = function() {}
exports.endTiming = function() {}
""",
"sdk/dom.js": """
(function() {
var $ = function(query, parent) {}
$.createElement = function() {}
$.create = function() {}
module.exports = $;
})();
"""
}
ns_mapping = {
"ko": join(os.getcwd(), "tmp", "test_nodejs_namespace_mapping", "sdk")
}
ns_mapping = "::".join(["##".join([k, v]) for k, v in ns_mapping.items()])
buf, positions = write_files(self, manifest=manifest, name="namespace_mapping",
env=SimplePrefsEnvironment(nodejsNamespaceMapping=ns_mapping))
self.assertCompletionsInclude2(buf, positions[1],
[("function", "scimoz"),
("function", "scintilla")])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "register"),
("function", "unregister")])
self.assertCompletionsInclude2(buf, positions[3],
[("function", "startTiming"),
("function", "endTiming")])
self.assertCompletionsInclude2(buf, positions[4],
[("function", "create"),
("function", "createElement")])
class StdLibTestCase(CodeIntelNodeJSTestCase):
""" Code Completion test cases for the Node.js standard library"""
lang = "Node.js"
@property
def version(self):
if not hasattr(self, "_version"):
langintel = self.mgr.langintel_from_lang(self.lang)
v = langintel._get_nodejs_version_from_env(self.mgr.env) or "99999"
setattr(self, "_version", LooseVersion(v))
return self._version
def assertCompletionsInclude2(self, buf, pos, completions, implicit=True):
"""
Override CodeIntelTestCase.assertCompletionsInclude2 to support versions
This is same as the original, except the completions can have an
optional third argument, a {str} that is the condition, e.g.
">= 0.8" or ">= 0.6 and < 0.7"
(currently, only comparison operators and "and" are supported)
"""
cplns = []
for cpln in completions:
if len(cpln) > 2:
condition = cpln[2]
tokens = condition.split()
comp = {"<": operator.lt,
"<=": operator.le,
">": operator.gt,
">=": operator.ge,
"==": operator.eq,
"!=": operator.ne,
}
i = 0
match = True
while i < len(tokens):
try:
if tokens[i] in comp:
if not comp.get(tokens[i])(self.version, tokens[i + 1]):
match = False
break
i += 1 # skip the version
continue
assert tokens[i] == "and", \
"Can't parse condition %s" % (condition,)
finally:
i += 1
if not match:
continue
cplns.append((cpln[0], cpln[1]))
return super(StdLibTestCase, self).assertCompletionsInclude2(
buf, pos, cplns, implicit)
def test__version(self):
"""
This is a debugging test; it's really just used to print out the
Node.js version in use
"""
raise TestSkipped("Using node.js version %s" % (self.version,))
def test_console(self):
"""
Test the Node.js console module
"""
manifest = {"test.js": """
require('console').<1>;
"""}
buf, positions = write_files(self, manifest=manifest, name="console")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "log"),
("function", "info"),
("function", "warn"),
("function", "error"),
("function", "dir"),
("function", "time"),
("function", "timeEnd"),
("function", "trace"),
("function", "assert"),
])
def test_timers(self):
"""
Test the Node.js timers module
"""
manifest = {"test.js": "require('timers').<1>;"}
buf, positions = write_files(self, manifest=manifest, name="timers")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "setTimeout"),
("function", "clearTimeout"),
("function", "setInterval"),
("function", "clearInterval"),
])
def test_process(self):
"""
Test the Node.js process module
"""
manifest = {"test.js": """
process.<1>;
process.stdin.<2>;
process.stdout.<3>;
process.stderr.<4>;
"""}
buf, positions = write_files(self, manifest=manifest, name="process")
self.assertCompletionsInclude2(buf, positions[1],
[("variable", "stdout"),
("variable", "stderr"),
("variable", "stdin"),
("variable", "argv"),
("variable", "execPath"),
("function", "abort", ">= 0.8"),
("function", "chdir"),
("function", "cwd"),
("variable", "env"),
("function", "exit"),
("function", "getgid"),
("function", "setgid"),
("function", "getuid"),
("function", "setuid"),
("variable", "version"),
("variable", "versions"),
("variable", "installPrefix", ">= 0.6 and < 0.7"),
("variable", "config", ">= 0.8"),
("function", "kill"),
("variable", "pid"),
("variable", "title"),
("variable", "arch"),
("variable", "platform"),
("function", "memoryUsage"),
("function", "nextTick"),
("function", "umask"),
("function", "uptime"),
("function", "hrtime", ">= 0.8"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("variable", "isRaw", ">= 0.8"), # tty.ReadStream
("function", "setRawMode", ">= 0.8"), # tty.ReadStream
("function", "setKeepAlive", ">= 0.8"), # net.Socket
("function", "pipe"), # stream.ReadStream
("function", "on"), # EventEmitter
])
self.assertCompletionsInclude2(buf, positions[3],
[("variable", "columns", ">= 0.8"), # tty.WriteStream
("variable", "rows", ">= 0.8"), # tty.WriteStream
("function", "setKeepAlive", ">= 0.8"), # net.Socket
("function", "write"), # stream.WriteStream
("function", "on"), # EventEmitter
])
self.assertCompletionsInclude2(buf, positions[4],
[("function", "write")])
def test_util(self):
"""
Test the Node.js util module
"""
manifest = {"test.js": "require('util').<1>;"}
buf, positions = write_files(self, manifest=manifest, name="util")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "format"),
("function", "debug"),
("function", "error", ">= 0.8"),
("function", "puts", ">= 0.8"),
("function", "print", ">= 0.8"),
("function", "log"),
("function", "inspect"),
("function", "isArray"),
("function", "isRegExp"),
("function", "isDate"),
("function", "isError"),
("function", "pump"),
("function", "inherits"),
])
def test_events(self):
"""
Test the Node.js events module
"""
manifest = {"test.js": """
var events = require('events');
events.<1>;
var emitter = new events.EventEmitter();
emitter.<2>;
"""}
buf, positions = write_files(self, manifest=manifest, name="events")
self.assertCompletionsInclude2(buf, positions[1],
[("class", "EventEmitter")])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "addListener"),
("function", "on"),
("function", "once"),
("function", "removeListener"),
("function", "removeAllListeners"),
("function", "setMaxListeners"),
("function", "listeners"),
("function", "emit"),
])
def test_buffer(self):
"""
Test the Node.js buffer module
"""
manifest = {"test.js": """
var buffer = require('buffer');
buffer.<1>;
buffer.Buffer.<2>;
var buf = new buffer.Buffer();
buf.<3>;
"""}
buf, positions = write_files(self, manifest=manifest, name="buffer")
self.assertCompletionsInclude2(buf, positions[1],
[("class", "Buffer"),
("variable", "INSPECT_MAX_BYTES"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "isBuffer"),
("function", "byteLength"),
("function", "concat", ">= 0.8"),
])
self.assertCompletionsInclude2(buf, positions[3],
[("function", "write"),
("function", "toString"),
# can't test array accessor []
("variable", "length"),
("function", "copy"),
("function", "slice"),
("function", "readUInt8"),
("function", "readUInt16LE"),
("function", "readUInt16BE"),
("function", "readUInt32LE"),
("function", "readUInt32BE"),
("function", "readInt8"),
("function", "readInt16LE"),
("function", "readInt16BE"),
("function", "readInt32LE"),
("function", "readInt32BE"),
("function", "readFloatLE"),
("function", "readFloatBE"),
("function", "readDoubleLE"),
("function", "readDoubleBE"),
("function", "writeUInt8"),
("function", "writeUInt16LE"),
("function", "writeUInt16BE"),
("function", "writeUInt32LE"),
("function", "writeUInt32BE"),
("function", "writeInt8"),
("function", "writeInt16LE"),
("function", "writeInt16BE"),
("function", "writeInt32LE"),
("function", "writeInt32BE"),
("function", "writeFloatLE"),
("function", "writeFloatBE"),
("function", "writeDoubleLE"),
("function", "writeDoubleBE"),
("function", "fill"),
])
def test_stream(self):
"""
Test the Node.js stream module
"""
manifest = {"test.js": """
var stream = require('stream');
stream.<1>;
var readStream = new stream.ReadableStream();
readStream.<2>;
var writeStream = new stream.WritableStream();
writeStream.<3>;
"""}
buf, positions = write_files(self, manifest=manifest, name="buffer")
self.assertCompletionsInclude2(buf, positions[1],
[("class", "ReadableStream"),
("class", "WritableStream"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "on"), # from EventEmitter
("variable", "readable"),
("function", "setEncoding"),
("function", "pause"),
("function", "resume"),
("function", "destroy"),
("function", "destroySoon", ">= 0.6 and < 0.7"),
("function", "pipe"),
])
self.assertCompletionsInclude2(buf, positions[3],
[("function", "on"), # from EventEmitter
("variable", "writable"),
("function", "write"),
("function", "end"),
("function", "destroy"),
("function", "destroySoon"),
])
def test_string_decoder(self):
"""
Test the Node.js string_decoder module
"""
if self.version < "0.8":
raise TestSkipped("Node.js %s is not at least 0.8" % (self.version,))
manifest = {"test.js": """
var string_decoder = require('string_decoder');
string_decoder.<1>;
new string_decoder.StringDecoder().<2>;
"""}
buf, positions = write_files(self, manifest=manifest, name="buffer")
self.assertCompletionsInclude2(buf, positions[1],
[("class", "StringDecoder"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "write"),
])
def test_crypto(self):
"""
Test the Node.js crypto module
"""
manifest = {"test.js": """
var crypto = require('crypto');
crypto.<1>;
crypto.createHash("md5").<2>;
crypto.createHmac("md5", null).<3>;
crypto.createCipher("aes192", null).<4>;
crypto.createDecipher("aes192", null).<5>;
crypto.createSign("RSA-SHA256").<6>;
crypto.createVerify("RSA-SHA256").<7>;
crypto.createDiffieHellman(0).<8>;
"""}
buf, positions = write_files(self, manifest=manifest, name="crypto")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "createCredentials"),
("function", "createHash"),
("function", "createHmac"),
("function", "createCipher"),
("function", "createCipheriv"),
("function", "createDecipher"),
("function", "createDecipheriv"),
("function", "createSign"),
("function", "createVerify"),
("function", "createDiffieHellman"),
("function", "getDiffieHellman", ">= 0.8"),
("function", "pbkdf2"),
("function", "randomBytes"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "update"),
("function", "digest"),
])
self.assertCompletionsInclude2(buf, positions[3],
[("function", "update"),
("function", "digest"),
])
self.assertCompletionsInclude2(buf, positions[4],
[("function", "update"),
("function", "final"),
("function", "setAutoPadding", ">= 0.8"),
])
self.assertCompletionsInclude2(buf, positions[5],
[("function", "update"),
("function", "final"),
("function", "setAutoPadding", ">= 0.8"),
])
self.assertCompletionsInclude2(buf, positions[6],
[("function", "update"),
("function", "sign"),
])
self.assertCompletionsInclude2(buf, positions[7],
[("function", "update"),
("function", "verify"),
])
self.assertCompletionsInclude2(buf, positions[8],
[("function", "generateKeys"),
("function", "computeSecret"),
("function", "getPrime"),
("function", "getGenerator"),
("function", "getPublicKey"),
("function", "getPrivateKey"),
("function", "setPublicKey"),
("function", "setPrivateKey"),
])
def test_tls(self):
"""
Test the Node.js tls module
"""
manifest = {"test.js": """
require('tls').<1>;
require('tls').createServer({}, function(s){}).<2>;
require('tls').connect(80).<3>;
require('tls').createSecurePair().<4>;
"""}
buf, positions = write_files(self, manifest=manifest, name="tls")
self.assertCompletionsInclude2(buf, positions[1],
[("function", "connect"),
("function", "createServer"),
("function", "createSecurePair"),
])
self.assertCompletionsInclude2(buf, positions[2],
[("function", "listen"),
("function", "close"),
("function", "address"),
("function", "addContext"),
("variable", "maxConnections"),
("variable", "connections"),
("function", "on"), # from EventEmitter
])
self.assertCompletionsInclude2(buf, positions[3],
[("variable", "authorized"),
("variable", "authorizationError"),
("function", "getPeerCertificate"),
("function", "getCipher", ">= 0.8"),