Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e380173

Browse files
committed
Fix bug when testing Symbol equality with should syntax (#669)
1 parent 816d526 commit e380173

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

lib/chai/interface/should.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ module.exports = function (chai, util) {
1010
function loadShould () {
1111
// explicitly define this method as function as to have it's name to include as `ssfi`
1212
function shouldGetter() {
13-
if (this instanceof String || this instanceof Number || this instanceof Boolean ) {
13+
if (this instanceof String
14+
|| this instanceof Number
15+
|| this instanceof Boolean
16+
|| typeof Symbol === 'function' && this instanceof Symbol) {
1417
return new Assertion(this.valueOf(), null, shouldGetter);
1518
}
1619
return new Assertion(this, null, shouldGetter);

test/assert.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,22 @@ describe('assert', function () {
107107
it('equal', function () {
108108
var foo;
109109
assert.equal(foo, undefined);
110+
111+
if (typeof Symbol === 'function') {
112+
var sym = Symbol();
113+
assert.equal(sym, sym);
114+
}
110115
});
111116

112117
it('typeof', function () {
113118
assert.typeOf('test', 'string');
114119
assert.typeOf(true, 'boolean');
115120
assert.typeOf(5, 'number');
116121

122+
if (typeof Symbol === 'function') {
123+
assert.typeOf(Symbol(), 'symbol');
124+
}
125+
117126
err(function () {
118127
assert.typeOf(5, 'string');
119128
}, "expected 5 to be a string");
@@ -182,6 +191,12 @@ describe('assert', function () {
182191
it('notEqual', function() {
183192
assert.notEqual(3, 4);
184193

194+
if (typeof Symbol === 'function') {
195+
var sym1 = Symbol()
196+
, sym2 = Symbol();
197+
assert.notEqual(sym1, sym2);
198+
}
199+
185200
err(function () {
186201
assert.notEqual(5, 5);
187202
}, "expected 5 to not equal 5");
@@ -190,6 +205,11 @@ describe('assert', function () {
190205
it('strictEqual', function() {
191206
assert.strictEqual('foo', 'foo');
192207

208+
if (typeof Symbol === 'function') {
209+
var sym = Symbol();
210+
assert.strictEqual(sym, sym);
211+
}
212+
193213
err(function () {
194214
assert.strictEqual('5', 5);
195215
}, "expected \'5\' to equal 5");
@@ -198,6 +218,12 @@ describe('assert', function () {
198218
it('notStrictEqual', function() {
199219
assert.notStrictEqual(5, '5');
200220

221+
if (typeof Symbol === 'function') {
222+
var sym1 = Symbol()
223+
, sym2 = Symbol();
224+
assert.notStrictEqual(sym1, sym2);
225+
}
226+
201227
err(function () {
202228
assert.notStrictEqual(5, 5);
203229
}, "expected 5 to not equal 5");
@@ -435,6 +461,12 @@ describe('assert', function () {
435461
assert.include([ 1, 2, 3], 3);
436462
assert.include({a:1, b:2}, {b:2});
437463

464+
if (typeof Symbol === 'function') {
465+
var sym1 = Symbol()
466+
, sym2 = Symbol();
467+
assert.include([sym1, sym2], sym1);
468+
}
469+
438470
err(function () {
439471
assert.include('foobar', 'baz');
440472
}, "expected \'foobar\' to include \'baz\'");
@@ -460,6 +492,13 @@ describe('assert', function () {
460492
assert.notInclude('foobar', 'baz');
461493
assert.notInclude([ 1, 2, 3 ], 4);
462494

495+
if (typeof Symbol === 'function') {
496+
var sym1 = Symbol()
497+
, sym2 = Symbol()
498+
, sym3 = Symbol();
499+
assert.notInclude([sym1, sym2], sym3);
500+
}
501+
463502
err(function(){
464503
assert.notInclude(true, true);
465504
}, "object tested must be an array, an object, or a string, but boolean given");
@@ -1256,6 +1295,10 @@ describe('assert', function () {
12561295
assert[isNotExtensible]('foo');
12571296
assert[isNotExtensible](false);
12581297
assert[isNotExtensible](undefined);
1298+
1299+
if (typeof Symbol === 'function') {
1300+
assert[isNotExtensible](Symbol());
1301+
}
12591302
});
12601303
});
12611304

@@ -1276,6 +1319,10 @@ describe('assert', function () {
12761319
assert[isSealed]('foo');
12771320
assert[isSealed](false);
12781321
assert[isSealed](undefined);
1322+
1323+
if (typeof Symbol === 'function') {
1324+
assert[isSealed](Symbol());
1325+
}
12791326
});
12801327
});
12811328

@@ -1330,6 +1377,10 @@ describe('assert', function () {
13301377
assert[isFrozen]('foo');
13311378
assert[isFrozen](false);
13321379
assert[isFrozen](undefined);
1380+
1381+
if (typeof Symbol === 'function') {
1382+
assert[isFrozen](Symbol());
1383+
}
13331384
});
13341385
});
13351386

test/expect.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ describe('expect', function () {
116116
expect(function() {}).to.be.a('function');
117117
expect(null).to.be.a('null');
118118

119+
if (typeof Symbol === 'function') {
120+
expect(Symbol()).to.be.a('symbol');
121+
}
122+
119123
err(function(){
120124
expect(5).to.not.be.a('number', 'blah');
121125
}, "blah: expected 5 not to be a number");
@@ -300,6 +304,11 @@ describe('expect', function () {
300304
expect(1).to.eql(1);
301305
expect('4').to.not.eql(4);
302306

307+
if (typeof Symbol === 'function') {
308+
var sym = Symbol();
309+
expect(sym).to.eql(sym);
310+
}
311+
303312
err(function(){
304313
expect(4).to.eql(3, 'blah');
305314
}, 'blah: expected 4 to deeply equal 3');
@@ -319,6 +328,11 @@ describe('expect', function () {
319328
expect('test').to.equal('test');
320329
expect(1).to.equal(1);
321330

331+
if (typeof Symbol === 'function') {
332+
var sym = Symbol();
333+
expect(sym).to.equal(sym);
334+
}
335+
322336
err(function(){
323337
expect(4).to.equal(3, 'blah');
324338
}, 'blah: expected 4 to equal 3');
@@ -624,6 +638,14 @@ describe('expect', function () {
624638
expect([{a:1}]).to.include({a:1});
625639
expect([{a:1}]).to.not.include({b:1});
626640

641+
if (typeof Symbol === 'function') {
642+
var sym1 = Symbol()
643+
, sym2 = Symbol()
644+
, sym3 = Symbol();
645+
expect([sym1, sym2]).to.include(sym1);
646+
expect([sym1, sym2]).to.not.include(sym3);
647+
}
648+
627649
err(function(){
628650
expect(['foo']).to.include('bar', 'blah');
629651
}, "blah: expected [ 'foo' ] to include 'bar'");
@@ -1283,6 +1305,10 @@ describe('expect', function () {
12831305
expect(false).to.not.be.extensible;
12841306
expect(undefined).to.not.be.extensible;
12851307

1308+
if (typeof Symbol === 'function') {
1309+
expect(Symbol()).to.not.be.extensible;
1310+
}
1311+
12861312
err(function() {
12871313
expect(42).to.be.extensible;
12881314
}, 'expected 42 to be extensible');
@@ -1326,6 +1352,10 @@ describe('expect', function () {
13261352
expect(false).to.be.sealed;
13271353
expect(undefined).to.be.sealed;
13281354

1355+
if (typeof Symbol === 'function') {
1356+
expect(Symbol()).to.be.sealed;
1357+
}
1358+
13291359
err(function() {
13301360
expect(42).to.not.be.sealed;
13311361
}, 'expected 42 to not be sealed');
@@ -1369,6 +1399,10 @@ describe('expect', function () {
13691399
expect(false).to.be.frozen;
13701400
expect(undefined).to.be.frozen;
13711401

1402+
if (typeof Symbol === 'function') {
1403+
expect(Symbol()).to.be.frozen;
1404+
}
1405+
13721406
err(function() {
13731407
expect(42).to.not.be.frozen;
13741408
}, 'expected 42 to not be frozen');

test/should.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ describe('should', function() {
146146
([]).should.be.a('array');
147147
(function() {}).should.be.a('function');
148148

149+
if (typeof Symbol === 'function') {
150+
Symbol().should.be.a('symbol');
151+
}
152+
149153
(5).should.be.a('number');
150154

151155
err(function(){
@@ -301,6 +305,11 @@ describe('should', function() {
301305
(1).should.eql(1);
302306
'4'.should.not.eql(4);
303307

308+
if (typeof Symbol === 'function') {
309+
var sym = Symbol();
310+
sym.should.eql(sym);
311+
}
312+
304313
err(function(){
305314
(4).should.eql(3, 'blah');
306315
}, 'blah: expected 4 to deeply equal 3');
@@ -310,6 +319,11 @@ describe('should', function() {
310319
'test'.should.equal('test');
311320
(1).should.equal(1);
312321

322+
if (typeof Symbol === 'function') {
323+
var sym = Symbol();
324+
sym.should.equal(sym);
325+
}
326+
313327
err(function(){
314328
(4).should.equal(3, 'blah');
315329
}, 'blah: expected 4 to equal 3');
@@ -484,6 +498,14 @@ describe('should', function() {
484498
({a:1,b:2}).should.include({b:2});
485499
({a:1,b:2}).should.not.include({b:3});
486500

501+
if (typeof Symbol === 'function') {
502+
var sym1 = Symbol()
503+
, sym2 = Symbol()
504+
, sym3 = Symbol();
505+
[sym1, sym2].should.include(sym1);
506+
[sym1, sym2].should.not.include(sym3);
507+
}
508+
487509
err(function(){
488510
['foo'].should.include('bar', 'blah');
489511
}, "blah: expected [ 'foo' ] to include 'bar'");
@@ -1132,6 +1154,10 @@ describe('should', function() {
11321154
'foo'.should.not.be.extensible;
11331155
false.should.not.be.extensible;
11341156

1157+
if (typeof Symbol === 'function') {
1158+
Symbol().should.not.be.extensible;
1159+
}
1160+
11351161
err(function() {
11361162
(42).should.be.extensible;
11371163
}, 'expected 42 to be extensible');
@@ -1165,6 +1191,10 @@ describe('should', function() {
11651191
'foo'.should.be.sealed;
11661192
false.should.be.sealed;
11671193

1194+
if (typeof Symbol === 'function') {
1195+
Symbol().should.be.sealed;
1196+
}
1197+
11681198
err(function() {
11691199
(42).should.not.be.sealed;
11701200
}, 'expected 42 to not be sealed');
@@ -1198,6 +1228,10 @@ describe('should', function() {
11981228
'foo'.should.be.frozen;
11991229
false.should.be.frozen;
12001230

1231+
if (typeof Symbol === 'function') {
1232+
Symbol().should.be.frozen;
1233+
}
1234+
12011235
err(function() {
12021236
(42).should.not.be.frozen;
12031237
}, 'expected 42 to not be frozen');

0 commit comments

Comments
 (0)