| 1 |
import test from 'ava'; |
| 2 |
import minimistOptions from '.'; |
| 3 |
|
| 4 |
const validate = (t, input, expected) => { |
| •5 |
t.same(minimistOptions(input), expected); |
| 6 |
}; |
| 7 |
|
| 8 |
test('empty input', validate, {}, {}); |
| 9 |
|
| 10 |
test('string option', validate, { |
| 11 |
name: 'string' |
| 12 |
}, { |
| 13 |
string: ['name'] |
| 14 |
}); |
| 15 |
|
| 16 |
test('boolean option', validate, { |
| 17 |
force: 'boolean' |
| 18 |
}, { |
| 19 |
boolean: ['force'] |
| 20 |
}); |
| 21 |
|
| 22 |
test('number option', validate, { |
| 23 |
score: 'number' |
| 24 |
}, { |
| 25 |
number: ['score'] |
| 26 |
}); |
| 27 |
|
| 28 |
test('default array option', validate, { |
| 29 |
arr: 'array' |
| 30 |
}, { |
| 31 |
array: ['arr'] |
| 32 |
}); |
| 33 |
|
| 34 |
test('string array option', validate, { |
| 35 |
arr: 'string-array' |
| 36 |
}, { |
| 37 |
array: [{key: 'arr', string: true}] |
| 38 |
}); |
| 39 |
|
| 40 |
test('number array option', validate, { |
| 41 |
arr: 'number-array' |
| 42 |
}, { |
| 43 |
array: [{key: 'arr', number: true}] |
| 44 |
}); |
| 45 |
|
| 46 |
test('boolean array option', validate, { |
| 47 |
arr: 'boolean-array' |
| 48 |
}, { |
| 49 |
array: [{key: 'arr', boolean: true}] |
| 50 |
}); |
| 51 |
|
| 52 |
test('multiple array options', validate, { |
| 53 |
xs: 'number-array', |
| 54 |
ys: 'boolean-array' |
| 55 |
}, { |
| 56 |
array: [{key: 'xs', number: true}, {key: 'ys', boolean: true}] |
| 57 |
}); |
| 58 |
|
| 59 |
test('string array default value is not string array fails', t => { |
| 60 |
const error = t.throws(() => { |
| 61 |
minimistOptions({ |
| 62 |
arr: { |
| 63 |
type: 'string-array', |
| 64 |
default: [2] |
| 65 |
} |
| 66 |
}); |
| 67 |
}, TypeError); |
| 68 |
|
| •69 |
t.equal(error.message, 'Expected "arr" default value to be of type "string-array", got "number-array"'); |
| 70 |
}); |
| 71 |
|
| 72 |
test('boolean array default value is not boolean array fails', t => { |
| 73 |
const error = t.throws(() => { |
| 74 |
minimistOptions({ |
| 75 |
arr: { |
| 76 |
type: 'boolean-array', |
| 77 |
default: ['score'] |
| 78 |
} |
| 79 |
}); |
| 80 |
}, TypeError); |
| 81 |
|
| •82 |
t.equal(error.message, 'Expected "arr" default value to be of type "boolean-array", got "string-array"'); |
| 83 |
}); |
| 84 |
|
| 85 |
test('number array default value is not number array fails', t => { |
| 86 |
const error = t.throws(() => { |
| 87 |
minimistOptions({ |
| 88 |
arr: { |
| 89 |
type: 'number-array', |
| 90 |
default: [true] |
| 91 |
} |
| 92 |
}); |
| 93 |
}, TypeError); |
| 94 |
|
| •95 |
t.equal(error.message, 'Expected "arr" default value to be of type "number-array", got "boolean-array"'); |
| 96 |
}); |
| 97 |
|
| 98 |
test('alias', validate, { |
| 99 |
score: { |
| 100 |
alias: 's' |
| 101 |
} |
| 102 |
}, { |
| 103 |
alias: { |
| 104 |
s: 'score' |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
test('alias array', validate, { |
| 109 |
score: { |
| 110 |
alias: ['s', 'sc'] |
| 111 |
} |
| 112 |
}, { |
| 113 |
alias: { |
| 114 |
s: 'score', |
| 115 |
sc: 'score' |
| 116 |
} |
| 117 |
}); |
| 118 |
|
| 119 |
test('alias and string', validate, { |
| 120 |
name: { |
| 121 |
type: 'string', |
| 122 |
alias: 'n' |
| 123 |
} |
| 124 |
}, { |
| 125 |
string: ['name'], |
| 126 |
alias: { |
| 127 |
n: 'name' |
| 128 |
} |
| 129 |
}); |
| 130 |
|
| 131 |
test('alias and boolean', validate, { |
| 132 |
force: { |
| 133 |
type: 'boolean', |
| 134 |
alias: 'f' |
| 135 |
} |
| 136 |
}, { |
| 137 |
boolean: ['force'], |
| 138 |
alias: { |
| 139 |
f: 'force' |
| 140 |
} |
| 141 |
}); |
| 142 |
|
| 143 |
test('alias and number', validate, { |
| 144 |
score: { |
| 145 |
type: 'number', |
| 146 |
alias: 's' |
| 147 |
} |
| 148 |
}, { |
| 149 |
number: ['score'], |
| 150 |
alias: { |
| 151 |
s: 'score' |
| 152 |
} |
| 153 |
}); |
| 154 |
|
| 155 |
test('alias and array', validate, { |
| 156 |
arr: { |
| 157 |
type: 'array', |
| 158 |
alias: 'a' |
| 159 |
} |
| 160 |
}, { |
| 161 |
array: ['arr'], |
| 162 |
alias: { |
| 163 |
a: 'arr' |
| 164 |
} |
| 165 |
}); |
| 166 |
|
| 167 |
test('default value', validate, { |
| 168 |
score: { |
| 169 |
default: 10 |
| 170 |
} |
| 171 |
}, { |
| 172 |
default: { |
| 173 |
score: 10 |
| 174 |
} |
| 175 |
}); |
| 176 |
|
| 177 |
test('default falsy value', validate, { |
| 178 |
falsePrimitive: { |
| 179 |
default: false |
| 180 |
}, |
| 181 |
zero: { |
| 182 |
default: 0 |
| 183 |
}, |
| 184 |
empty: { |
| 185 |
default: '' |
| 186 |
}, |
| 187 |
nan: { |
| 188 |
default: NaN |
| 189 |
}, |
| 190 |
nullPrimitive: { |
| 191 |
default: null |
| 192 |
}, |
| 193 |
undefinedPrimitive: { |
| 194 |
default: undefined |
| 195 |
} |
| 196 |
}, { |
| 197 |
default: { |
| 198 |
falsePrimitive: false, |
| 199 |
zero: 0, |
| 200 |
empty: '', |
| 201 |
nan: NaN, |
| 202 |
nullPrimitive: null, |
| 203 |
undefinedPrimitive: undefined |
| 204 |
} |
| 205 |
}); |
| 206 |
|
| 207 |
test('default array value', validate, { |
| 208 |
arr: { |
| 209 |
type: 'array', |
| 210 |
default: ['a'] |
| 211 |
} |
| 212 |
}, { |
| 213 |
array: ['arr'], |
| 214 |
default: { |
| 215 |
arr: ['a'] |
| 216 |
} |
| 217 |
}); |
| 218 |
|
| 219 |
test('default empty array value', validate, { |
| 220 |
arr: { |
| 221 |
type: 'array', |
| 222 |
default: [] |
| 223 |
}, |
| 224 |
strings: { |
| 225 |
type: 'string-array', |
| 226 |
default: [] |
| 227 |
}, |
| 228 |
booleans: { |
| 229 |
type: 'boolean-array', |
| 230 |
default: [] |
| 231 |
}, |
| 232 |
numbers: { |
| 233 |
type: 'number-array', |
| 234 |
default: [] |
| 235 |
} |
| 236 |
}, { |
| 237 |
array: [ |
| 238 |
'arr', |
| 239 |
{key: 'strings', string: true}, |
| 240 |
{key: 'booleans', boolean: true}, |
| 241 |
{key: 'numbers', number: true} |
| 242 |
], |
| 243 |
default: { |
| 244 |
arr: [], |
| 245 |
strings: [], |
| 246 |
booleans: [], |
| 247 |
numbers: [] |
| 248 |
} |
| 249 |
}); |
| 250 |
|
| 251 |
test('arguments type', validate, { |
| 252 |
arguments: 'string' |
| 253 |
}, { |
| 254 |
string: ['_'] |
| 255 |
}); |
| 256 |
|
| 257 |
test('passthrough options', validate, { |
| 258 |
'--': true, |
| 259 |
stopEarly: true, |
| 260 |
unknown: true |
| 261 |
}, { |
| 262 |
'--': true, |
| 263 |
stopEarly: true, |
| 264 |
unknown: true |
| 265 |
}); |
| 266 |
|
| 267 |
test('fail if type is not boolean, string, number or array', t => { |
| 268 |
const error = t.throws(() => { |
| 269 |
minimistOptions({ |
| 270 |
force: { |
| 271 |
type: 'bool' |
| 272 |
} |
| 273 |
}); |
| 274 |
}, TypeError); |
| 275 |
|
| •276 |
t.equal(error.message, 'Expected type of "force" to be one of ["string", "boolean", "number", "array", "string-array", "boolean-array", "number-array"], got "bool"'); |
| 277 |
}); |
| 278 |
|
| 279 |
test('fail if boolean default value is not a boolean', t => { |
| 280 |
const error = t.throws(() => { |
| 281 |
minimistOptions({ |
| 282 |
force: { |
| 283 |
type: 'boolean', |
| 284 |
default: 'true' |
| 285 |
} |
| 286 |
}); |
| 287 |
}, TypeError); |
| 288 |
|
| •289 |
t.equal(error.message, 'Expected "force" default value to be of type "boolean", got "string"'); |
| 290 |
}); |
| 291 |
|
| 292 |
test('fail if number default value is not a number', t => { |
| 293 |
const error = t.throws(() => { |
| 294 |
minimistOptions({ |
| 295 |
score: { |
| 296 |
type: 'number', |
| 297 |
default: '1' |
| 298 |
} |
| 299 |
}); |
| 300 |
}, TypeError); |
| 301 |
|
| •302 |
t.equal(error.message, 'Expected "score" default value to be of type "number", got "string"'); |
| 303 |
}); |
| 304 |
|
| 305 |
test('fail if string default value is not a string', t => { |
| 306 |
const error = t.throws(() => { |
| 307 |
minimistOptions({ |
| 308 |
score: { |
| 309 |
type: 'string', |
| 310 |
default: 1 |
| 311 |
} |
| 312 |
}); |
| 313 |
}, TypeError); |
| 314 |
|
| •315 |
t.equal(error.message, 'Expected "score" default value to be of type "string", got "number"'); |
| 316 |
}); |
| 317 |
|
| 318 |
test('fail if array default value is not an array', t => { |
| 319 |
const error = t.throws(() => { |
| 320 |
minimistOptions({ |
| 321 |
score: { |
| 322 |
type: 'array', |
| 323 |
default: '' |
| 324 |
} |
| 325 |
}); |
| 326 |
}, TypeError); |
| 327 |
|
| •328 |
t.equal(error.message, 'Expected "score" default value to be of type "string-array", got "string"'); |
| 329 |
}); |
| 330 |
|
| 331 |
test('fail if array default value element type is not string', t => { |
| 332 |
const error = t.throws(() => { |
| 333 |
minimistOptions({ |
| 334 |
score: { |
| 335 |
type: 'array', |
| 336 |
default: [1] |
| 337 |
} |
| 338 |
}); |
| 339 |
}, TypeError); |
| 340 |
|
| •341 |
t.equal(error.message, 'Expected "score" default value to be of type "string-array", got "number-array"'); |
| 342 |
}); |