-
-
Notifications
You must be signed in to change notification settings - Fork 736
/
Copy pathequal.js
54 lines (50 loc) · 1.6 KB
/
equal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const Assertion = require('../assert')
const AssertionFailedError = require('./error')
const { template } = require('../utils')
const output = require('../output')
class EqualityAssertion extends Assertion {
constructor(params) {
const comparator = function (a, b) {
if (b.length === 0) {
b = ''
}
return a === b
}
super(comparator, params)
this.params.type = 'to equal'
}
getException() {
const params = this.params
params.jar = template(params.jar, params)
const err = new AssertionFailedError(params, '{{customMessage}}expected {{jar}} "{{expected}}" {{type}} "{{actual}}"')
err.showDiff = false
if (typeof err.cliMessage === 'function') {
err.message = err.cliMessage()
}
err.cliMessage = () => {
const msg = err.template.replace('{{jar}}', output.colors.bold('{{jar}}'))
return template(msg, this.params)
}
return err
}
addAssertParams() {
this.params.expected = arguments[0]
this.params.actual = arguments[1]
this.params.customMessage = arguments[2] ? `${arguments[2]}\n\n` : ''
}
}
module.exports = {
Assertion: EqualityAssertion,
equals: jar => new EqualityAssertion({ jar }),
urlEquals: baseUrl => {
const assert = new EqualityAssertion({ jar: 'url of current page' })
assert.comparator = function (expected, actual) {
if (expected.indexOf('http') !== 0) {
actual = actual.slice(actual.indexOf(baseUrl) + baseUrl.length)
}
return actual === expected
}
return assert
},
fileEquals: file => new EqualityAssertion({ file, jar: 'contents of {{file}}' }),
}