Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1a968e6

Browse filesBrowse files
targosrvagg
authored andcommitted
test: improve tests for util.inherits
inherits is used in lib and tests but its functionality itself is not tested yet. PR-URL: #3507 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 2bb1475 commit 1a968e6
Copy full SHA for 1a968e6

File tree

Expand file treeCollapse file tree

2 files changed

+46
-7
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+46
-7
lines changed
Open diff view settings
Collapse file
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const inherits = require('util').inherits;
6+
7+
// super constructor
8+
function A() {
9+
this._a = 'a';
10+
}
11+
A.prototype.a = function() { return this._a; };
12+
13+
// one level of inheritance
14+
function B(value) {
15+
A.call(this);
16+
this._b = value;
17+
}
18+
inherits(B, A);
19+
B.prototype.b = function() { return this._b; };
20+
21+
assert.strictEqual(B.super_, A);
22+
23+
const b = new B('b');
24+
assert.strictEqual(b.a(), 'a');
25+
assert.strictEqual(b.b(), 'b');
26+
assert.strictEqual(b.constructor, B);
27+
28+
// two levels of inheritance
29+
function C() {
30+
B.call(this, 'b');
31+
this._c = 'c';
32+
}
33+
inherits(C, B);
34+
C.prototype.c = function() { return this._c; };
35+
C.prototype.getValue = function() { return this.a() + this.b() + this.c(); };
36+
37+
assert.strictEqual(C.super_, B);
38+
39+
const c = new C();
40+
assert.strictEqual(c.getValue(), 'abc');
41+
assert.strictEqual(c.constructor, C);
42+
43+
// should throw with invalid arguments
44+
assert.throws(function() { inherits(A, {}); }, TypeError);
45+
assert.throws(function() { inherits(A, null); }, TypeError);
46+
assert.throws(function() { inherits(null, A); }, TypeError);
Collapse file

‎test/parallel/test-util.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util.js
-7Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,3 @@ assert.deepEqual(util._extend({a:1}, true), {a:1});
8383
assert.deepEqual(util._extend({a:1}, false), {a:1});
8484
assert.deepEqual(util._extend({a:1}, {b:2}), {a:1, b:2});
8585
assert.deepEqual(util._extend({a:1, b:2}, {b:3}), {a:1, b:3});
86-
87-
// inherits
88-
var ctor = function() {};
89-
assert.throws(function() { util.inherits(ctor, {}); }, TypeError);
90-
assert.throws(function() { util.inherits(ctor, null); }, TypeError);
91-
assert.throws(function() { util.inherits(null, ctor); }, TypeError);
92-
assert.doesNotThrow(function() { util.inherits(ctor, ctor); }, TypeError);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.