Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit af60601

Browse files
committedNov 18, 2017
Improve friendly string name of tdFunctions
Instead of merely Stringifying the function names, we can intelligently emit the string name in a form that's idiomatic to the type of key the function is defined under. Methods are prefixed with `.` or `#`; Symbol keys are wrapped in brackets `[]`.
1 parent b6dd645 commit af60601

File tree

6 files changed

+13
-8
lines changed

6 files changed

+13
-8
lines changed
 

‎regression/src/constructor-test.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe 'td.constructor', ->
8181
And -> td.explain(@fakeInstance[@symbolFoo]).isTestDouble == true
8282
And -> @fakeConstructor.toString() == '[test double for "(unnamed constructor)"]'
8383
And -> @fakeInstance.toString() == '[test double instance of constructor]'
84-
And -> @fakeInstance[@symbolFoo].toString() == '[test double for "#Symbol(foo)"]'
84+
And -> @fakeInstance[@symbolFoo].toString() == '[test double for "#[Symbol(foo)]"]'
8585

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

‎regression/src/object-test.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe 'td.object', ->
3131
When -> td.when(@testDouble[@symbolFoo]()).thenReturn('zing!')
3232
Then -> @testDouble[@symbolFoo]() == 'zing!'
3333
And -> @testDouble.toString() == '[test double object]'
34-
And -> @testDouble[@symbolFoo].toString() == '[test double for ".Symbol(foo)"]'
34+
And -> @testDouble[@symbolFoo].toString() == '[test double for "[Symbol(foo)]"]'
3535

3636
describe 'passing a function to td.object erroneously (1.x)', ->
3737
When -> try td.object(->) catch e then @result = e
@@ -62,7 +62,7 @@ describe 'td.object', ->
6262
Then -> @testDouble.lol.toString() == '[test double for ".lol"]'
6363

6464
context 'with Symbol propKey', ->
65-
And -> @testDouble[Symbol('foo')].toString() == '[test double for "thing.Symbol(foo)"]'
65+
And -> @testDouble[Symbol('foo')].toString() == '[test double for "thing[Symbol(foo)]"]'
6666

6767
else
6868
describe 'getting an error message', ->

‎src/constructor.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ var fakeConstructorFromNames = (funcNames) => {
1313
'[test double instance of constructor]'
1414

1515
_.each(funcNames, (funcName) => {
16-
fakeConstructor.prototype[funcName] = tdFunction(`#${String(funcName)}`)
16+
fakeConstructor.prototype[funcName] = tdFunction(friendlyNameFor(funcName))
1717
})
1818
})
1919
}
20+
21+
var friendlyNameFor = (name) => (typeof name === 'symbol') ? `#[${String(name)}]` : `#${String(name)}`

‎src/imitate/create-imitation.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ 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.map(String).join('') || '(anonymous function)')
14+
return tdFunction(names.map(friendlyName).join('') || '(anonymous function)')
1515
}
1616
} else {
1717
return _.clone(original)
1818
}
1919
}
20+
var friendlyName = (name) => (typeof name === 'symbol') ? `[${String(name)}]` : String(name)

‎src/object.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var fakeObject = (nameOrType, config) => {
2626

2727
var createTestDoublesForFunctionNames = (names) =>
2828
_.transform(names, (acc, funcName) => {
29-
acc[funcName] = tdFunction(`.${String(funcName)}`)
29+
acc[funcName] = tdFunction(friendlyNameFor(funcName))
3030
})
3131

3232
var createTestDoubleViaProxy = (name, config) => {
@@ -35,13 +35,15 @@ var createTestDoubleViaProxy = (name, config) => {
3535
return new Proxy(obj, {
3636
get (target, propKey, receiver) {
3737
if (!obj.hasOwnProperty(propKey) && !_.includes(config.excludeMethods, propKey)) {
38-
obj[propKey] = tdFunction(`${nameOf(name)}.${String(propKey)}`)
38+
obj[propKey] = tdFunction(`${nameOf(name)}${friendlyNameFor(propKey)}`)
3939
}
4040
return obj[propKey]
4141
}
4242
})
4343
}
4444

45+
var friendlyNameFor = (name) => (typeof name === 'symbol') ? `[${String(name)}]` : `.${String(name)}`
46+
4547
var ensureProxySupport = (name) => {
4648
if (typeof Proxy === 'undefined') {
4749
log.error('td.object', `\

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434
const someFunc = () => {}
3535
const symFoo = Symbol('foo')
3636
const symBar = Symbol('bar')
37-
td.when(tdFunction('Symbol(foo)Symbol(bar)'))
37+
td.when(tdFunction('[Symbol(foo)][Symbol(bar)]'))
3838
.thenReturn('fake thing')
3939
td.when(isGenerator(someFunc)).thenReturn(false)
4040

0 commit comments

Comments
 (0)
Please sign in to comment.