Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support deep matchers #203

Merged
merged 10 commits into from Mar 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -48,7 +48,9 @@
"prepublish": "yarn run compile"
},
"babel": {
"presets": ["env"]
"presets": [
"env"
]
},
"browser": {
"./lib/replace/module.js": "./lib/replace/module.browser.js",
Expand All @@ -57,7 +59,7 @@
"dependencies": {
"lodash": "^4.15.0",
"quibble": "^0.4.0",
"stringify-object": "^2.4.0"
"stringify-object-es5": "^2.5.0"
},
"devDependencies": {
"babel-cli": "^6.23.0",
Expand All @@ -77,7 +79,7 @@
"testem": "^0.9.4",
"typescript": "^2.1.4"
},
"directories":{
"directories": {
"doc": "./docs",
"example": "./examples",
"lib": "./lib",
Expand Down
7 changes: 4 additions & 3 deletions src/args-match.coffee
Expand Up @@ -11,14 +11,15 @@ arityMismatch = (expectedArgs, actualArgs, config) ->
expectedArgs.length != actualArgs.length && !config.ignoreExtraArgs

equalsWithMatchers = (expectedArgs, actualArgs) ->
_.every expectedArgs, (expectedArg, i) ->
argumentMatchesExpectation(expectedArg, actualArgs[i])
_.every expectedArgs, (expectedArg, key) ->
argumentMatchesExpectation(expectedArg, actualArgs[key])

argumentMatchesExpectation = (expectedArg, actualArg) ->
if matcher = matcherFor(expectedArg)
matcher(actualArg)
else
_.isEqual(expectedArg, actualArg)
_.isEqualWith expectedArg, actualArg, (expectedEl, actualEl) ->
matcherFor(expectedEl)?(actualEl)

matcherFor = (expectedArg) ->
if _.isFunction(expectedArg?.__matches)
Expand Down
7 changes: 6 additions & 1 deletion src/stringify/anything.coffee
@@ -1,6 +1,6 @@
_ = require('../util/lodash-wrap')

stringifyObject = require('stringify-object')
stringifyObject = require('stringify-object-es5')

module.exports = (anything) ->
if _.isString(anything)
Expand All @@ -15,3 +15,8 @@ module.exports = (anything) ->
indent: ' '
singleQuotes: false
inlineCharacterLimit: 65
transform: (obj, prop, originalResult) ->
if obj[prop]?.__matches?
obj[prop].__name
else
originalResult
1 change: 1 addition & 0 deletions src/util/lodash-wrap.js
Expand Up @@ -15,6 +15,7 @@ export {
isArray,
isBoolean,
isEqual,
isEqualWith,
isFunction,
isNumber,
isPlainObject,
Expand Down
5 changes: 5 additions & 0 deletions test/src/stringify/anything-test.coffee
Expand Up @@ -12,6 +12,11 @@ describe 'stringify/anything', ->
context 'short strings of objects should be one-lined', ->
Then -> expect(@subject({userId: 42, name: 'Jane'})).to.eq('{userId: 42, name: "Jane"}')

context 'matchers', ->
Then -> @subject(td.matchers.isA(Number)) == 'isA(Number)'
Then -> expect(@subject({val: td.matchers.isA(Number)})).
to.eq('{val: isA(Number)}')

context 'long strings of objects should be multi-lined', ->
Given -> @object =
userId: 42,
Expand Down
54 changes: 54 additions & 0 deletions test/src/verify-test.coffee
Expand Up @@ -105,6 +105,60 @@ describe '.verify', ->
- called with `(55)`.
"""

context 'using deep matchers', ->

context 'single level', ->
When -> @testDouble({ value: 55 })

context 'satisfied', ->
Then -> shouldNotThrow(=> td.verify(@testDouble({ value: td.matchers.isA(Number) })))

context 'unsatisfied', ->
Then -> shouldThrow (=> td.verify(@testDouble({ value: td.matchers.isA(String) }))), """
Unsatisfied verification on test double.

Wanted:
- called with `({value: isA(String)})`.

All calls of the test double, in order were:
- called with `({value: 55})`.
"""

context 'deeply nested', ->
When -> @testDouble({value:{ value: 55 }})

context 'satisfied', ->
Then -> shouldNotThrow(=> td.verify(@testDouble({ value: { value: td.matchers.isA(Number) } })))

context 'unsatisfied', ->
Then -> shouldThrow (=> td.verify(@testDouble({ value: { value: td.matchers.isA(String) } }))), """
Unsatisfied verification on test double.

Wanted:
- called with `({value: {value: isA(String)}})`.

All calls of the test double, in order were:
- called with `({value: {value: 55}})`.
"""

context 'array values', ->
When -> @testDouble([ 55 ])

context 'satisfied', ->
Then -> shouldNotThrow(=> td.verify(@testDouble([ td.matchers.isA(Number) ])))

context 'unsatisfied', ->
Then -> shouldThrow (=> td.verify(@testDouble([ td.matchers.isA(String) ]))), """
Unsatisfied verification on test double.

Wanted:
- called with `([isA(String)])`.

All calls of the test double, in order were:
- called with `([55])`.
"""


describe 'configuration', ->

describe 'ignoring extra arguments (more thoroughly tested via when())', ->
Expand Down
31 changes: 31 additions & 0 deletions test/src/when-test.coffee
Expand Up @@ -39,6 +39,37 @@ describe 'when', ->
Then -> @testDouble(44, 5) == undefined
Then -> @testDouble(88, "five") == undefined

describe 'using deep matchers', ->
context 'single level', ->
Given -> td.when(@testDouble(key: td.matchers.isA(String))).thenReturn("yay")
Then -> @testDouble(key: "testytest") == "yay"
Then -> @testDouble(key: 42) == undefined
Then -> @testDouble({}) == undefined
Then -> @testDouble("i am a string") == undefined

context 'deeply nested', ->
Given -> td.when(@testDouble(a: {b: td.matchers.isA(String)})).thenReturn("yay")
Then -> @testDouble(a: {b: "testytest"}) == "yay"
Then -> @testDouble(a: {b: 42}) == undefined
Then -> @testDouble(a: "testytest") == undefined

context 'array values', ->
Given -> td.when(@testDouble([5, td.matchers.isA(String)])).thenReturn("yay")
Then -> @testDouble([5, "testytest"]) == "yay"
Then -> @testDouble([5, 6]) == undefined
Then -> @testDouble([5]) == undefined
Then -> @testDouble([]) == undefined

context 'arguments with circular structures', ->
Given -> @arg =
foo: 'bar'
Given -> @arg.baz = @arg
Given -> td.when(@testDouble(@arg)).thenReturn("yay")
Then -> @testDouble(@arg) == "yay"
Then -> @testDouble('no') == undefined



describe 'stubbing sequential returns', ->
context 'a single stubbing', ->
Given -> td.when(@testDouble()).thenReturn(10,9)
Expand Down
19 changes: 12 additions & 7 deletions yarn.lock
Expand Up @@ -1777,6 +1777,10 @@ generate-object-property@^1.1.0:
dependencies:
is-property "^1.0.0"

get-own-enumerable-property-symbols@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-1.0.1.tgz#f1d4e3ad1402e039898e56d1e9b9aa924c26e484"

getpass@^0.1.1:
version "0.1.6"
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.6.tgz#283ffd9fc1256840875311c1b60e8c40187110e6"
Expand Down Expand Up @@ -2110,9 +2114,9 @@ is-number@^2.0.2, is-number@^2.1.0:
dependencies:
kind-of "^3.0.2"

is-plain-obj@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e"
is-obj@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-obj/-/is-obj-1.0.1.tgz#3e4729ac1f5fde025cd7d83a896dab9f4f67db0f"

is-posix-bracket@^0.1.0:
version "0.1.1"
Expand Down Expand Up @@ -3329,11 +3333,12 @@ string_decoder@~0.10.0, string_decoder@~0.10.x:
version "0.10.31"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"

stringify-object@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/stringify-object/-/stringify-object-2.4.0.tgz#c62d11023eb21fe2d9b087be039a26df3b22a09d"
stringify-object@searls/stringify-object:
version "3.1.1"
resolved "https://codeload.github.com/searls/stringify-object/tar.gz/4f5c3f4c7eee5fe011dfb550036b436eecccdc2e"
dependencies:
is-plain-obj "^1.0.0"
get-own-enumerable-property-symbols "^1.0.1"
is-obj "^1.0.1"
is-regexp "^1.0.0"

stringstream@~0.0.4:
Expand Down