1 | 'use strict';
|
---|
2 |
|
---|
3 | var test = require('tape');
|
---|
4 | var isSymbol = require('../index');
|
---|
5 |
|
---|
6 | var forEach = function (arr, func) {
|
---|
7 | var i;
|
---|
8 | for (i = 0; i < arr.length; ++i) {
|
---|
9 | func(arr[i], i, arr);
|
---|
10 | }
|
---|
11 | };
|
---|
12 |
|
---|
13 | var hasSymbols = require('has-symbols')();
|
---|
14 | var inspect = require('object-inspect');
|
---|
15 | var debug = function (v, m) { return inspect(v) + ' ' + m; };
|
---|
16 |
|
---|
17 | test('non-symbol values', function (t) {
|
---|
18 | var nonSymbols = [
|
---|
19 | true,
|
---|
20 | false,
|
---|
21 | Object(true),
|
---|
22 | Object(false),
|
---|
23 | null,
|
---|
24 | undefined,
|
---|
25 | {},
|
---|
26 | [],
|
---|
27 | /a/g,
|
---|
28 | 'string',
|
---|
29 | 42,
|
---|
30 | new Date(),
|
---|
31 | function () {},
|
---|
32 | NaN
|
---|
33 | ];
|
---|
34 | t.plan(nonSymbols.length);
|
---|
35 | forEach(nonSymbols, function (nonSymbol) {
|
---|
36 | t.equal(false, isSymbol(nonSymbol), debug(nonSymbol, 'is not a symbol'));
|
---|
37 | });
|
---|
38 | t.end();
|
---|
39 | });
|
---|
40 |
|
---|
41 | test('faked symbol values', function (t) {
|
---|
42 | t.test('real symbol valueOf', { skip: !hasSymbols }, function (st) {
|
---|
43 | var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
|
---|
44 | st.equal(false, isSymbol(fakeSymbol), 'object with valueOf returning a symbol is not a symbol');
|
---|
45 | st.end();
|
---|
46 | });
|
---|
47 |
|
---|
48 | t.test('faked @@toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (st) {
|
---|
49 | var fakeSymbol = { valueOf: function () { return Symbol('foo'); } };
|
---|
50 | fakeSymbol[Symbol.toStringTag] = 'Symbol';
|
---|
51 | st.equal(false, isSymbol(fakeSymbol), 'object with fake Symbol @@toStringTag and valueOf returning a symbol is not a symbol');
|
---|
52 | var notSoFakeSymbol = { valueOf: function () { return 42; } };
|
---|
53 | notSoFakeSymbol[Symbol.toStringTag] = 'Symbol';
|
---|
54 | st.equal(false, isSymbol(notSoFakeSymbol), 'object with fake Symbol @@toStringTag and valueOf not returning a symbol is not a symbol');
|
---|
55 | st.end();
|
---|
56 | });
|
---|
57 |
|
---|
58 | var fakeSymbolString = { toString: function () { return 'Symbol(foo)'; } };
|
---|
59 | t.equal(false, isSymbol(fakeSymbolString), 'object with toString returning Symbol(foo) is not a symbol');
|
---|
60 |
|
---|
61 | t.end();
|
---|
62 | });
|
---|
63 |
|
---|
64 | test('Symbol support', { skip: !hasSymbols }, function (t) {
|
---|
65 | t.test('well-known Symbols', function (st) {
|
---|
66 | var isWellKnown = function filterer(name) {
|
---|
67 | return name !== 'for' && name !== 'keyFor' && !(name in filterer);
|
---|
68 | };
|
---|
69 | var wellKnownSymbols = Object.getOwnPropertyNames(Symbol).filter(isWellKnown);
|
---|
70 | wellKnownSymbols.forEach(function (name) {
|
---|
71 | var sym = Symbol[name];
|
---|
72 | st.equal(true, isSymbol(sym), debug(sym, ' is a symbol'));
|
---|
73 | });
|
---|
74 | st.end();
|
---|
75 | });
|
---|
76 |
|
---|
77 | t.test('user-created symbols', function (st) {
|
---|
78 | var symbols = [
|
---|
79 | Symbol(),
|
---|
80 | Symbol('foo'),
|
---|
81 | Symbol['for']('foo'),
|
---|
82 | Object(Symbol('object'))
|
---|
83 | ];
|
---|
84 | symbols.forEach(function (sym) {
|
---|
85 | st.equal(true, isSymbol(sym), debug(sym, ' is a symbol'));
|
---|
86 | });
|
---|
87 | st.end();
|
---|
88 | });
|
---|
89 |
|
---|
90 | t.end();
|
---|
91 | });
|
---|
92 |
|
---|