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 afce62d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 73 deletions.
82 changes: 21 additions & 61 deletions modules/Expectation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import assert from './assert'
import { isSpy } from './SpyUtils'
import {
functionThrows,
arrayContains,
stringContains,
objectContains,
isArray,
isObject,
containsHelper,
isFunction,
isA
} from './TestUtils'
Expand Down Expand Up @@ -294,79 +290,43 @@ class Expectation {
}

toInclude(value, compareValues, message) {
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'
)

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

message = message || 'Expected %s to include %s'
if (compareValues == null)
compareValues = isEqual

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
)
}
containsHelper(
this.actual,
value,
compareValues,
false,
'toInclude',
message || 'Expected %s to include %s'
)

return this
}

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'
)

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

message = message || 'Expected %s to exclude %s'
if (compareValues == null)
compareValues = isEqual

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
)
}
containsHelper(
this.actual,
value,
compareValues,
true,
'toExclude',
message || 'Expected %s to exclude %s'
)

return this
}
Expand Down
41 changes: 29 additions & 12 deletions modules/TestUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isEqual from 'is-equal'
import isRegExp from 'is-regex'
import assert from './assert'

/**
* Returns true if the given object is a function.
Expand Down Expand Up @@ -71,33 +72,49 @@ export const functionThrows = (fn, context, args, value) => {
* otherwise. The compareValues function must return false to
* indicate a non-match.
*/
export const arrayContains = (array, value, compareValues) => {
if (compareValues == null)
compareValues = isEqual

return array.some(item => compareValues(item, value) !== false)
}
export const arrayContains = (array, value, compareValues) =>
array.some(item => compareValues(item, value) !== false)

/**
* Returns true if the given object contains the value, false
* otherwise. The compareValues function must return false to
* indicate a non-match.
*/
export const objectContains = (object, value, compareValues) => {
if (compareValues == null)
compareValues = isEqual

return Object.keys(value).every(k => {
export const objectContains = (object, value, compareValues) =>
Object.keys(value).every(k => {
if (isObject(object[k])) {
return objectContains(object[k], value[k], compareValues)
}

return compareValues(object[k], value[k])
})
}

/**
* Returns true if the given string contains the value, false otherwise.
*/
export const stringContains = (string, value) =>
string.indexOf(value) !== -1

export const containsHelper = (actual, value, compareValues, exclude, funcName, message) => {
assert(
isArray(actual) || isObject(actual) || typeof actual === 'string',
`The "actual" argument in expect(actual).${funcName}() must be an array, object, or a string`
)

let condition = false

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

assert(
exclude ? !condition : condition,
message,
actual,
value
)
}

0 comments on commit afce62d

Please sign in to comment.