Skip to content

Commit f3e6f45

Browse files
authored
Merge pull request #301 from testdouble/fix-symbol-to-string
Symbols must be stringified explicitly
2 parents 42d9c76 + a9ffe0a commit f3e6f45

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

regression/src/constructor-test.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ describe 'td.constructor', ->
7272
And -> @fakeInstance.foo.toString() == '[test double for "#foo"]'
7373

7474

75+
if (global.Symbol)
76+
describe 'edge case: being given a Symbol as function name', ->
77+
Given -> @symbolFoo = Symbol('foo')
78+
Given -> @fakeConstructor = td.constructor([@symbolFoo])
79+
Given -> @fakeInstance = new @fakeConstructor('biz')
80+
Then -> @fakeConstructor.prototype[@symbolFoo] == @fakeInstance[@symbolFoo]
81+
And -> td.verify(new @fakeConstructor('biz'))
82+
And -> td.explain(@fakeInstance[@symbolFoo]).isTestDouble == true
83+
And -> @fakeConstructor.toString() == '[test double for "(unnamed constructor)"]'
84+
And -> @fakeInstance.toString() == '[test double instance of constructor]'
85+
And -> @fakeInstance[@symbolFoo].toString() == '[test double for "#Symbol(foo)"]'
7586

7687
describe 'edge case: being given a function without prototypal methods', ->
7788
Given -> @boringFunc = ->

regression/src/object-test.coffee

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ describe 'td.object', ->
2525
And -> @testDouble.toString() == '[test double object]'
2626
And -> @testDouble.bam.toString() == '[test double for ".bam"]'
2727

28+
if (global.Symbol)
29+
context 'making a test double based on a Symbol', ->
30+
Given -> @symbolFoo = Symbol('foo')
31+
Given -> @testDouble = td.object([@symbolFoo])
32+
When -> td.when(@testDouble[@symbolFoo]()).thenReturn('zing!')
33+
Then -> @testDouble[@symbolFoo]() == 'zing!'
34+
And -> @testDouble.toString() == '[test double object]'
35+
And -> @testDouble[@symbolFoo].toString() == '[test double for ".Symbol(foo)"]'
36+
2837
describe 'passing a function to td.object erroneously (1.x)', ->
2938
When -> try td.object(->) catch e then @result = e
3039
Then -> expect(@result.message).to.contain(
@@ -56,6 +65,11 @@ describe 'td.object', ->
5665
Given -> @testDouble = td.object()
5766
Then -> @testDouble.toString() == '[test double object]'
5867
Then -> @testDouble.lol.toString() == '[test double for ".lol"]'
68+
69+
if (global.Symbol)
70+
context 'with Symbol propKey', ->
71+
And -> @testDouble[Symbol('foo')].toString() == '[test double for "thing.Symbol(foo)"]'
72+
5973
else
6074
describe 'getting an error message', ->
6175
When -> try

src/constructor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var fakeConstructorFromNames = (funcNames) => {
1313
'[test double instance of constructor]'
1414

1515
_.each(funcNames, (funcName) => {
16-
fakeConstructor.prototype[funcName] = tdFunction(`#${funcName}`)
16+
fakeConstructor.prototype[funcName] = tdFunction(`#${String(funcName)}`)
1717
})
1818
})
1919
}

src/imitate/create-imitation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default (original, names) => {
1111
return original
1212
} else {
1313
// TODO: this will become src/function/create and include parent reference instead of name joining here
14-
return tdFunction(names.join('') || '(anonymous function)')
14+
return tdFunction(_.map(names, String).join('') || '(anonymous function)')
1515
}
1616
} else {
1717
return _.clone(original)

src/object.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var fakeObject = function (nameOrType, config, argCount) {
2727

2828
var createTestDoublesForFunctionNames = (names) =>
2929
_.transform(names, (acc, funcName) => {
30-
acc[funcName] = tdFunction(`.${funcName}`)
30+
acc[funcName] = tdFunction(`.${String(funcName)}`)
3131
})
3232

3333
var createTestDoubleViaProxy = (name, config) => {
@@ -36,7 +36,7 @@ var createTestDoubleViaProxy = (name, config) => {
3636
return new Proxy(obj, {
3737
get (target, propKey, receiver) {
3838
if (!obj.hasOwnProperty(propKey) && !_.includes(config.excludeMethods, propKey)) {
39-
obj[propKey] = tdFunction(`${nameOf(name)}.${propKey}`)
39+
obj[propKey] = tdFunction(`${nameOf(name)}.${String(propKey)}`)
4040
}
4141
return obj[propKey]
4242
}

test/unit/imitate/create-imitation.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ module.exports = {
3030

3131
assert.deepEqual(result, 'fake thing')
3232
},
33+
'a function with symbols' () {
34+
if (!global.Symbol) return
35+
36+
const someFunc = () => {}
37+
const symFoo = Symbol('foo')
38+
const symBar = Symbol('bar')
39+
td.when(tdFunction('Symbol(foo)Symbol(bar)'))
40+
.thenReturn('fake thing')
41+
td.when(isGenerator(someFunc)).thenReturn(false)
42+
43+
const result = subject(someFunc, [symFoo, symBar])
44+
45+
assert.deepEqual(result, 'fake thing')
46+
},
3347
'other instances': () => {
3448
const original = {a: 'b'}
3549

0 commit comments

Comments
 (0)