Skip to content

Commit

Permalink
Dry up toInclude/toExclude
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer committed Apr 9, 2016
1 parent 6ec27a7 commit 096af85
Showing 1 changed file with 19 additions and 56 deletions.
75 changes: 19 additions & 56 deletions modules/Expectation.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,84 +293,47 @@ class Expectation {
return this
}

toInclude(value, compareValues, message) {
toInclude(value, compareValues, message, _exclude = false) {
const funcName = _exclude ? 'toExclude' : 'toInclude'

assert(
isArray(this.actual) || isObject(this.actual) || typeof this.actual === 'string',
'The "actual" argument in expect(actual).toInclude() must be an array, object, or a string'
`The "actual" argument in expect(actual).${funcName}() must be an array, object, or a string`
)

if (typeof compareValues === 'string') {
message = compareValues
compareValues = null
}

message = message || 'Expected %s to include %s'
let condition = false

if (isArray(this.actual)) {
assert(
arrayContains(this.actual, value, compareValues),
message,
this.actual,
value
)
condition = arrayContains(this.actual, value, compareValues)
} else if (isObject(this.actual)) {
assert(
objectContains(this.actual, value, compareValues),
message,
this.actual,
value
)
condition = objectContains(this.actual, value, compareValues)
} else {
assert(
stringContains(this.actual, value),
message,
this.actual,
value
)
condition = stringContains(this.actual, value)
}

return this
}
if (_exclude) {
condition = !condition
}

toExclude(value, compareValues, message) {
assert(
isArray(this.actual) || isObject(this.actual) || typeof this.actual === 'string',
'The "actual" argument in expect(actual).toExclude() must be an array, object, or a string'
condition,
message || (_exclude ? 'Expected %s to exclude %s' : 'Expected %s to include %s'),
this.actual,
value
)

if (typeof compareValues === 'string') {
message = compareValues
compareValues = null
}

message = message || 'Expected %s to exclude %s'

if (isArray(this.actual)) {
assert(
!arrayContains(this.actual, value, compareValues),
message,
this.actual,
value
)
} else if (isObject(this.actual)) {
assert(
!objectContains(this.actual, value, compareValues),
message,
this.actual,
value
)
} else {
assert(
!stringContains(this.actual, value),
message,
this.actual,
value
)
}

return this
}

toExclude(value, compareValues, message) {
return this.toInclude(value, compareValues, message, true)
}

toHaveBeenCalled(message) {
const spy = this.actual

Expand Down

0 comments on commit 096af85

Please sign in to comment.