-
-
Notifications
You must be signed in to change notification settings - Fork 214
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Feature toBeHexadecimal Matcher #152
Changes from 5 commits
27db3dc
1deb3ae
fd32b72
c405020
300a412
c51a2e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toBeHexadecimal fails when given valid hexadecimal 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toBeHexadecimal(</><dim>)</> | ||
|
||
Expected value to not be a hexadecimal, received: | ||
<red>\\"#eeffee\\"</>" | ||
`; | ||
|
||
exports[`.toBeHexadecimal fails when given invalid hexadecimal 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toBeHexadecimal(</><dim>)</> | ||
|
||
Expected value to be a hexadecimal, received: | ||
<red>\\"#eeggee\\"</>" | ||
`; | ||
|
||
exports[`.toBeHexadecimal fails when given non-string 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toBeHexadecimal(</><dim>)</> | ||
|
||
Expected value to be a hexadecimal, received: | ||
<red>true</>" | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { matcherHint, printReceived } from 'jest-matcher-utils'; | ||
|
||
import predicate from './predicate'; | ||
|
||
const passMessage = received => () => | ||
matcherHint('.not.toBeHexadecimal', 'received', '') + | ||
'\n\n' + | ||
'Expected value to not be a hexadecimal, received:\n' + | ||
` ${printReceived(received)}`; | ||
|
||
const failMessage = received => () => | ||
matcherHint('.toBeHexadecimal', 'received', '') + | ||
'\n\n' + | ||
'Expected value to be a hexadecimal, received:\n' + | ||
` ${printReceived(received)}`; | ||
|
||
export default { | ||
toBeHexadecimal: expected => { | ||
const pass = predicate(expected); | ||
if (pass) { | ||
return { pass: true, message: passMessage(expected) }; | ||
} | ||
|
||
return { pass: false, message: failMessage(expected) }; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import matcher from './'; | ||
|
||
expect.extend(matcher); | ||
|
||
describe('.toBeHexadecimal', () => { | ||
test('passes when given valid 6 digit hexadecimal', () => { | ||
expect('#ECECEC').toBeHexadecimal(); | ||
}); | ||
|
||
test('passes when given valid 3 digit hexadecimal', () => { | ||
expect('#000').toBeHexadecimal(); | ||
}); | ||
|
||
test('fails when given invalid hexadecimal', () => { | ||
expect(() => expect('#eeggee').toBeHexadecimal()).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
test('fails when given non-string', () => { | ||
expect(() => expect(true).toBeHexadecimal()).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toBeHexadecimal', () => { | ||
test('passes when given positive number', () => { | ||
expect(1).not.toBeHexadecimal(); | ||
}); | ||
|
||
test('fails when given valid hexadecimal', () => { | ||
expect(() => expect('#eeffee').not.toBeHexadecimal()).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default expected => { | ||
const re1 = RegExp(/^#\b[a-f0-9]{6}\b/gi); | ||
const re2 = RegExp(/^#\b[a-f0-9]{3}\b/gi); | ||
return re1.test(expected) || re2.test(expected); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it worth naming these a bit more explicitly, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. This was lazy on my part. |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import predicate from './predicate'; | ||
|
||
describe('toBeHexadecimal Predicate', () => { | ||
describe('returns true', () => { | ||
test('When valid hex is passed', () => { | ||
expect(predicate('#123abc')).toBe(true); | ||
expect(predicate('#ABC123')).toBe(true); | ||
expect(predicate('#FFF')).toBe(true); | ||
expect(predicate('#ecECec')).toBe(true); | ||
expect(predicate('#00f')).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('return false', () => { | ||
test('When incomplete hex is passed', () => { | ||
expect(predicate('#ecec')).toBe(false); | ||
expect(predicate('#00')).toBe(false); | ||
}); | ||
|
||
test('When octothorp is not passed', () => { | ||
expect(predicate('000')).toBe(false); | ||
expect(predicate('ececec')).toBe(false); | ||
}); | ||
|
||
test('When invalid character is passed', () => { | ||
expect(predicate('#123ffg')).toBe(false); | ||
expect(predicate('#GGG')).toBe(false); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth having a test for a string that isn't hexadecimal as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. This boundary is already covered in the predicate test.