Skip to content

Commit

Permalink
Add tmatch support to expect(object).toMatch
Browse files Browse the repository at this point in the history
Fixes #81
  • Loading branch information
mjackson committed Apr 19, 2016
1 parent 53eaec1 commit a3b3b0b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 32 deletions.
53 changes: 35 additions & 18 deletions modules/Expectation.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 tmatch from 'tmatch'
import has from 'has'
import assert from './assert'
import { isSpy } from './SpyUtils'
Expand Down Expand Up @@ -176,18 +177,26 @@ class Expectation {
}

toMatch(pattern, message) {
assert(
typeof this.actual === 'string',
'The "actual" argument in expect(actual).toMatch() must be a string'
)
let matches = false

assert(
isRegExp(pattern),
'The "value" argument in toMatch(value) must be a RegExp'
)
if (typeof this.actual === 'string') {
assert(
isRegExp(pattern),
'The "value" argument in expect(string).toMatch(value) 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(
pattern.test(this.actual),
matches,
(message || 'Expected %s to match %s'),
this.actual,
pattern
Expand All @@ -197,18 +206,26 @@ class Expectation {
}

toNotMatch(pattern, message) {
assert(
typeof this.actual === 'string',
'The "actual" argument in expect(actual).toNotMatch() must be a string'
)
let matches = false

assert(
isRegExp(pattern),
'The "value" argument in toNotMatch(value) must be a RegExp'
)
if (typeof this.actual === 'string') {
assert(
isRegExp(pattern),
'The "value" argument in toNotMatch(value) 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(
!pattern.test(this.actual),
!matches,
(message || 'Expected %s to not match %s'),
this.actual,
pattern
Expand Down
40 changes: 27 additions & 13 deletions modules/__tests__/toMatch-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import expect from '../index'

describe('toMatch', () => {
describe('expect(string).toMatch', () => {
it('requires the pattern to be a RegExp', () => {
expect(() => {
expect('actual').toMatch('expected')
Expand All @@ -20,22 +20,36 @@ describe('toMatch', () => {
})
})

describe('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', () => {
describe('expect(object).toMatch', () => {
it('does not throw when the actual value matches the pattern', () => {
expect(() => {
expect('actual').toNotMatch(/nope/)
expect({
statusCode: 200,
headers: {
server: 'express web server'
}
}).toMatch({
statusCode: 200,
headers: {
server: /express/
}
})
}).toNotThrow()
})

it('throws when the actual value matches the pattern', () => {
it('throws when the actual value does not match the pattern', () => {
expect(() => {
expect('actual').toNotMatch(/^actual$/)
}).toThrow(/to not match/)
expect({
statusCode: 200,
headers: {
server: 'nginx web server'
}
}).toMatch({
statusCode: 200,
headers: {
server: /express/
}
})
}).toThrow(/to match/)
})
})
55 changes: 55 additions & 0 deletions modules/__tests__/toNotMatch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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/)
}).toNotThrow()
})

it('throws when the actual value matches the pattern', () => {
expect(() => {
expect('actual').toNotMatch(/^actual$/)
}).toThrow(/to not match/)
})
})

describe('expect(object).toNotMatch', () => {
it('does not throw when the actual value does not match the pattern', () => {
expect(() => {
expect({
statusCode: 200,
headers: {
server: 'nginx web server'
}
}).toNotMatch({
statusCode: 200,
headers: {
server: /express/
}
})
}).toNotThrow()
})

it('throws when the actual value matches the pattern', () => {
expect(() => {
expect({
statusCode: 200,
headers: {
server: 'express web server'
}
}).toNotMatch({
statusCode: 200,
headers: {
server: /express/
}
})
}).toThrow(/to not match/)
})
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"is-equal": "^1.5.1",
"is-regex": "^1.0.3",
"object-inspect": "^1.1.0",
"object-keys": "^1.0.9"
"object-keys": "^1.0.9",
"tmatch": "^2.0.1"
},
"devDependencies": {
"babel-cli": "^6.6.5",
Expand Down

0 comments on commit a3b3b0b

Please sign in to comment.