/
index.js
165 lines (141 loc) · 3.94 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const log = require('fliplog')
const power = require('power-assert')
const extend = require('lodash.assigninwith')
const exposeHidden = require('expose-hidden')
const {assert, should, assert2, should2} = require('./chai')
const {lcFirst, ucFirst} = require('./helpers')
const powers = require('./power')
const izz = require('./izz')
// https://github.com/avajs/ava/blob/2a206311d708361b294a21aa4c5bd199936d0824/lib/enhance-assert.js
let Fosho = {}
let debugMode = false
let logMode = false
// compatibility
power.true = power
// log if good to go
function gtg(key, args) {
if (!logMode) return
log
.color('green')
.text(key + '? fosho')
.echo()
}
// scope it so it doesn't need to be passed in every time
let scoped = null
// https://github.com/avajs/ava#assertions
const foshizzle = (arg, t) => {
if (!t && scoped) t = scoped
if (!t) t = power
// if (t && !scoped) {
// scoped = t
// }
// else if (t && scoped && scoped._test && t._test && scoped._test.title != t._test.title) {
// scoped = t
// }
// else if (!t && scoped) {
// t = scoped
// }
// else if (!t) {
// t = power
// }
Fosho = {}
const shoulds = should(arg)
// take hidden properties, expose them
exposeHidden(shoulds)
exposeHidden(assert)
// extend power-assert
// extend(Fosho, t, (v, fn, key, o, src) => (fnArg) => {
// t(arg, fnArg)
// return Fosho
// })
// does not return true just throws
extend(Fosho, assert, (v, fn, key, o, src) => {
o[key] = (fnArg, msg) => {
// log.data({key, arg, fnArg, assertion: assert[key](arg, fnArg, msg)}).echo()
t.true(assert[key](arg, fnArg, msg) !== false)
gtg(key)
return Fosho
}
return o[key]
})
// objValue, srcValue, key, object, source
// does not return true just throws
extend(Fosho, shoulds, (v, fn, key, o, src) => {
o[key] = (fnArg) => {
t.true(should2(arg)[key](fnArg) !== false)
gtg(key)
return Fosho
}
return o[key]
})
extend(Fosho, izz, (v, fn, key, o, src) => {
// log.color('bold').text(key).echo()
o[key] = (fnArg) => {
const assertion = izz[key](arg, fnArg)
if (assertion) {
// if we decorate with `not`, or `is*` or `uppercaseFirst`
// then we want to not assert twice
if (o[key].aint !== true) {
gtg(key)
t.true(izz[key](arg, fnArg) !== false)
return Fosho
}
return Fosho
}
else {
t.true(izz[key](arg, fnArg) !== false)
if (debugMode) {
const msg = 'was not fosho ' + key
const validation = new Error(msg)
validation.youUsed = arg
log
.trace()
.verbose(2)
.data(validation)
.preset('error')
.echo()
throw validation
}
}
return Fosho
}
return o[key]
})
const ass = powers(arg, t)
extend(Fosho, ass, (v, fn, key, o, src) => (fnArg, fnArg2) => {
ass[key](fnArg, fnArg2)
// else log.quick({v, fn, key, o, src})
// else if (o[key])
return Fosho
})
Object.keys(Fosho).forEach((key) => {
if (key.includes('is')) {
const shortKey = lcFirst(key.replace('is', ''))
if (Fosho[shortKey]) return
Fosho[shortKey] = Fosho[key]
}
if (key.includes('is')) {
const keyBe = key.replace('is', 'be')
if (Fosho[keyBe]) return
Fosho[keyBe] = Fosho[key]
}
})
return Fosho
}
// if previous assertions have passed,
// this will be true,
// otherwise they will return false
Fosho.izzle = () => true
// as static methods, simply wrap the fn call
Object.keys(izz).forEach((key) => {
foshizzle[key] = (arg, fnArg) => foshizzle(arg)[key](fnArg)
})
foshizzle.logMode = (bool) => logMode = bool
foshizzle.debugMode = (bool) => debugMode = bool
foshizzle.is = izz
foshizzle.assert = assert2
foshizzle.should = should2
foshizzle.fosho = foshizzle
foshizzle.log = log
foshizzle.t = (t) => scoped = t
module.exports = foshizzle