Skip to content

Commit ce918b9

Browse files
wraithgarlukekarrys
authored andcommitted
fix: remove dead code
increase test coverage
1 parent 200c0c4 commit ce918b9

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

lib/nopt.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// info about each config option.
22

3+
/* istanbul ignore next */
34
var debug = process.env.DEBUG_NOPT || process.env.NOPT_DEBUG
45
? function () {
56
console.error.apply(console, arguments)
@@ -189,9 +190,7 @@ function validateDate (data, k, val) {
189190
}
190191

191192
function validateBoolean (data, k, val) {
192-
if (val instanceof Boolean) {
193-
val = val.valueOf()
194-
} else if (typeof val === 'string') {
193+
if (typeof val === 'string') {
195194
if (!isNaN(val)) {
196195
val = !!(+val)
197196
} else if (val === 'null' || val === 'false') {

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
"tap": "^16.3.0"
3131
},
3232
"tap": {
33-
"lines": 87,
34-
"functions": 91,
35-
"branches": 81,
36-
"statements": 87,
33+
"lines": 91,
34+
"branches": 87,
35+
"statements": 91,
3736
"nyc-arg": [
3837
"--exclude",
3938
"tap-snapshots/**"

test/basic.js

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var nopt = require('../')
2-
var test = require('tap').test
2+
var t = require('tap')
33
var isWin = process.platform === 'win32'
44

5-
test('empty array is fine if type includes Array', function (t) {
5+
t.test('empty array is fine if type includes Array', function (t) {
66
var typeDefs = {
77
arr: [Array, String],
88
}
@@ -14,35 +14,35 @@ test('empty array is fine if type includes Array', function (t) {
1414
t.end()
1515
})
1616

17-
test('passing a string results in a string', function (t) {
17+
t.test('passing a string results in a string', function (t) {
1818
var parsed = nopt({ key: String }, {}, ['--key', 'myvalue'], 0)
1919
t.same(parsed.key, 'myvalue')
2020
t.end()
2121
})
2222

2323
// https://github.com/npm/nopt/issues/31
24-
test('Empty String results in empty string, not true', function (t) {
24+
t.test('Empty String results in empty string, not true', function (t) {
2525
var parsed = nopt({ empty: String }, {}, ['--empty'], 0)
2626
t.same(parsed.empty, '')
2727
t.end()
2828
})
2929

3030
// https://github.com/npm/nopt/issues/65
31-
test('Empty String should not swallow next flag', function (t) {
31+
t.test('Empty String should not swallow next flag', function (t) {
3232
var parsed = nopt({ empty: String, foo: String }, {}, ['--empty', '--foo'], 0)
3333
t.same(parsed.empty, '')
3434
t.same(parsed.foo, '')
3535
t.end()
3636
})
3737

3838
// https://github.com/npm/nopt/issues/66
39-
test('Empty String should not be true when type is single item Array', function (t) {
39+
t.test('Empty String should not be true when type is single item Array', function (t) {
4040
var parsed = nopt({ foo: [String] }, {}, ['--foo'], 0)
4141
t.same(parsed.foo, '')
4242
t.end()
4343
})
4444

45-
test('~ path is resolved to ' + (isWin ? '%USERPROFILE%' : '$HOME'), function (t) {
45+
t.test('~ path is resolved to ' + (isWin ? '%USERPROFILE%' : '$HOME'), function (t) {
4646
var path = require('path')
4747
var the
4848

@@ -68,35 +68,35 @@ test('~ path is resolved to ' + (isWin ? '%USERPROFILE%' : '$HOME'), function (t
6868
})
6969

7070
// https://github.com/npm/nopt/issues/24
71-
test('Unknown options are not parsed as numbers', function (t) {
71+
t.test('Unknown options are not parsed as numbers', function (t) {
7272
var parsed = nopt({ 'parse-me': Number }, null, ['--leave-as-is=1.20', '--parse-me=1.20'], 0)
7373
t.equal(parsed['leave-as-is'], '1.20')
7474
t.equal(parsed['parse-me'], 1.2)
7575
t.end()
7676
})
7777

7878
// https://github.com/npm/nopt/issues/48
79-
test('Check types based on name of type', function (t) {
79+
t.test('Check types based on name of type', function (t) {
8080
var parsed = nopt({ 'parse-me': { name: 'Number' } }, null, ['--parse-me=1.20'], 0)
8181
t.equal(parsed['parse-me'], 1.2)
8282
t.end()
8383
})
8484

85-
test('Missing types are not parsed', function (t) {
85+
t.test('Missing types are not parsed', function (t) {
8686
var parsed = nopt({ 'parse-me': {} }, null, ['--parse-me=1.20'], 0)
8787
// should only contain argv
8888
t.equal(Object.keys(parsed).length, 1)
8989
t.end()
9090
})
9191

92-
test('Types passed without a name are not parsed', function (t) {
92+
t.test('Types passed without a name are not parsed', function (t) {
9393
var parsed = nopt({ 'parse-me': {} }, {}, ['--parse-me=1.20'], 0)
9494
// should only contain argv
9595
t.equal(Object.keys(parsed).length, 1)
9696
t.end()
9797
})
9898

99-
test('other tests', function (t) {
99+
t.test('other tests', function (t) {
100100
var Stream = require('stream')
101101
var path = require('path')
102102
var url = require('url')
@@ -312,3 +312,51 @@ test('other tests', function (t) {
312312
})
313313
t.end()
314314
})
315+
316+
t.test('argv toString()', function (t) {
317+
var parsed = nopt({ key: String }, {}, ['--key', 'myvalue'], 0)
318+
t.same(parsed.argv.toString(), '"--key" "myvalue"')
319+
t.end()
320+
})
321+
322+
t.test('custom invalidHandler', function (t) {
323+
t.teardown(() => {
324+
delete nopt.invalidHandler
325+
})
326+
nopt.invalidHandler = (k, v) => {
327+
t.match(k, 'key')
328+
t.match(v, 'nope')
329+
t.end()
330+
}
331+
nopt({ key: Number }, {}, ['--key', 'nope'], 0)
332+
})
333+
334+
t.test('numbered boolean', function (t) {
335+
var parsed = nopt({ key: [Boolean, String] }, {}, ['--key', '0'], 0)
336+
t.same(parsed.key, false)
337+
t.end()
338+
})
339+
340+
t.test('false string boolean', function (t) {
341+
var parsed = nopt({ key: [Boolean, String] }, {}, ['--key', 'false'], 0)
342+
t.same(parsed.key, false)
343+
t.end()
344+
})
345+
346+
t.test('true string boolean', function (t) {
347+
var parsed = nopt({ key: [Boolean, String] }, {}, ['--key', 'true'], 0)
348+
t.same(parsed.key, true)
349+
t.end()
350+
})
351+
352+
t.test('null string boolean', function (t) {
353+
var parsed = nopt({ key: [Boolean, String] }, {}, ['--key', 'null'], 0)
354+
t.same(parsed.key, false)
355+
t.end()
356+
})
357+
358+
t.test('other string boolean', function (t) {
359+
var parsed = nopt({ key: [Boolean, String] }, {}, ['--key', 'yes'], 0)
360+
t.same(parsed.key, true)
361+
t.end()
362+
})

0 commit comments

Comments
 (0)