Skip to content

Commit

Permalink
to[Not]Match use tmatch directly
Browse files Browse the repository at this point in the history
Fixes #98
  • Loading branch information
mjackson committed May 10, 2016
1 parent 5983068 commit 866b129
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 51 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [HEAD]

- `toMatch` and `toNotMatch` use `tmatch` directly, so a wider array
of objects and patterns are supported

[HEAD]: https://github.com/mjackson/expect/compare/v1.20.1...HEAD

## [v1.20.1]
> May 7, 2016
Expand Down
41 changes: 2 additions & 39 deletions modules/Expectation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import has from 'has'
import tmatch from 'tmatch'
import isRegExp from 'is-regex'
import assert from './assert'
import { isSpy } from './SpyUtils'
import {
Expand Down Expand Up @@ -169,26 +168,8 @@ class Expectation {
}

toMatch(pattern, message) {
let matches = false

if (typeof this.actual === 'string') {
assert(
isRegExp(pattern),
'The "pattern" argument in expect(string).toMatch(pattern) must be a RegExp'
)

matches = pattern.test(this.actual)
} else if (isObject(this.actual)) {
matches = tmatch(this.actual, pattern)
} else {
assert(
false,
'The "actual" argument in expect(actual).toMatch() must be a string or an object'
)
}

assert(
matches,
tmatch(this.actual, pattern),
(message || 'Expected %s to match %s'),
this.actual,
pattern
Expand All @@ -198,26 +179,8 @@ class Expectation {
}

toNotMatch(pattern, message) {
let matches = false

if (typeof this.actual === 'string') {
assert(
isRegExp(pattern),
'The "pattern" argument in expect(string).toNotMatch(pattern) must be a RegExp'
)

matches = pattern.test(this.actual)
} else if (isObject(this.actual)) {
matches = tmatch(this.actual, pattern)
} else {
assert(
false,
'The "actual" argument in expect(actual).toNotMatch() must be a string or an object'
)
}

assert(
!matches,
!tmatch(this.actual, pattern),
(message || 'Expected %s to not match %s'),
this.actual,
pattern
Expand Down
18 changes: 12 additions & 6 deletions modules/__tests__/toMatch-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import expect from '../index'

describe('expect(string).toMatch', () => {
it('requires the pattern to be a RegExp', () => {
expect(() => {
expect('actual').toMatch('expected')
}).toThrow(/must be a RegExp/)
})

it('does not throw when the actual value matches the pattern', () => {
expect(() => {
expect('actual').toMatch(/^actual$/)
Expand Down Expand Up @@ -53,3 +47,15 @@ describe('expect(object).toMatch', () => {
}).toThrow(/to match/)
})
})

describe('expect(array).toMatch', () => {
it('does not throw when the array contains an object that matches the pattern', () => {
const array = [
{ one: 'one' },
{ two: 'two' },
{ three: 'three' }
]

expect(array).toMatch([ { one: /one/ } ])
})
})
18 changes: 12 additions & 6 deletions modules/__tests__/toNotMatch-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import expect from '../index'

describe('expect(string).toNotMatch', () => {
it('requires the pattern to be a RegExp', () => {
expect(() => {
expect('actual').toNotMatch('expected')
}).toThrow(/must be a RegExp/)
})

it('does not throw when the actual value does not match the pattern', () => {
expect(() => {
expect('actual').toNotMatch(/nope/)
Expand Down Expand Up @@ -53,3 +47,15 @@ describe('expect(object).toNotMatch', () => {
}).toThrow(/to not match/)
})
})

describe('expect(array).toNotMatch', () => {
it('does not throw when the array does not contain an object that matches the pattern', () => {
const array = [
{ one: 'one' },
{ two: 'two' },
{ three: 'three' }
]

expect(array).toNotMatch([ { one: /two/ } ])
})
})

0 comments on commit 866b129

Please sign in to comment.