1 | var nopt = require("../")
|
2 | , test = require('tap').test
|
3 |
|
4 |
|
5 | test("passing a string results in a string", function (t) {
|
6 | var parsed = nopt({ key: String }, {}, ["--key", "myvalue"], 0)
|
7 | t.same(parsed.key, "myvalue")
|
8 | t.end()
|
9 | })
|
10 |
|
11 |
|
12 | test("Empty String results in empty string, not true", function (t) {
|
13 | var parsed = nopt({ empty: String }, {}, ["--empty"], 0)
|
14 | t.same(parsed.empty, "")
|
15 | t.end()
|
16 | })
|
17 |
|
18 | test("~ path is resolved to $HOME", function (t) {
|
19 | var path = require("path")
|
20 | if (!process.env.HOME) process.env.HOME = "/tmp"
|
21 | var parsed = nopt({key: path}, {}, ["--key=~/val"], 0)
|
22 | t.same(parsed.key, path.resolve(process.env.HOME, "val"))
|
23 | t.end()
|
24 | })
|
25 |
|
26 |
|
27 | test("Unknown options are not parsed as numbers", function (t) {
|
28 | var parsed = nopt({"parse-me": Number}, null, ['--leave-as-is=1.20', '--parse-me=1.20'], 0)
|
29 | t.equal(parsed['leave-as-is'], '1.20')
|
30 | t.equal(parsed['parse-me'], 1.2)
|
31 | t.end()
|
32 | });
|
33 |
|
34 |
|
35 | test("Check types based on name of type", function (t) {
|
36 | var parsed = nopt({"parse-me": {name: "Number"}}, null, ['--parse-me=1.20'], 0)
|
37 | t.equal(parsed['parse-me'], 1.2)
|
38 | t.end()
|
39 | })
|
40 |
|
41 |
|
42 | test("Types passed without a name are not parsed", function (t) {
|
43 | var parsed = nopt({"parse-me": {}}, null, ['--parse-me=1.20'], 0)
|
44 |
|
45 | t.equal(Object.keys(parsed).length, 1)
|
46 | t.end()
|
47 | })
|
48 |
|
49 | test("other tests", function (t) {
|
50 |
|
51 | var util = require("util")
|
52 | , Stream = require("stream")
|
53 | , path = require("path")
|
54 | , url = require("url")
|
55 |
|
56 | , shorthands =
|
57 | { s : ["--loglevel", "silent"]
|
58 | , d : ["--loglevel", "info"]
|
59 | , dd : ["--loglevel", "verbose"]
|
60 | , ddd : ["--loglevel", "silly"]
|
61 | , noreg : ["--no-registry"]
|
62 | , reg : ["--registry"]
|
63 | , "no-reg" : ["--no-registry"]
|
64 | , silent : ["--loglevel", "silent"]
|
65 | , verbose : ["--loglevel", "verbose"]
|
66 | , h : ["--usage"]
|
67 | , H : ["--usage"]
|
68 | , "?" : ["--usage"]
|
69 | , help : ["--usage"]
|
70 | , v : ["--version"]
|
71 | , f : ["--force"]
|
72 | , desc : ["--description"]
|
73 | , "no-desc" : ["--no-description"]
|
74 | , "local" : ["--no-global"]
|
75 | , l : ["--long"]
|
76 | , p : ["--parseable"]
|
77 | , porcelain : ["--parseable"]
|
78 | , g : ["--global"]
|
79 | }
|
80 |
|
81 | , types =
|
82 | { aoa: Array
|
83 | , nullstream: [null, Stream]
|
84 | , date: Date
|
85 | , str: String
|
86 | , browser : String
|
87 | , cache : path
|
88 | , color : ["always", Boolean]
|
89 | , depth : Number
|
90 | , description : Boolean
|
91 | , dev : Boolean
|
92 | , editor : path
|
93 | , force : Boolean
|
94 | , global : Boolean
|
95 | , globalconfig : path
|
96 | , group : [String, Number]
|
97 | , gzipbin : String
|
98 | , logfd : [Number, Stream]
|
99 | , loglevel : ["silent","win","error","warn","info","verbose","silly"]
|
100 | , long : Boolean
|
101 | , "node-version" : [false, String]
|
102 | , npaturl : url
|
103 | , npat : Boolean
|
104 | , "onload-script" : [false, String]
|
105 | , outfd : [Number, Stream]
|
106 | , parseable : Boolean
|
107 | , pre: Boolean
|
108 | , prefix: path
|
109 | , proxy : url
|
110 | , "rebuild-bundle" : Boolean
|
111 | , registry : url
|
112 | , searchopts : String
|
113 | , searchexclude: [null, String]
|
114 | , shell : path
|
115 | , t: [Array, String]
|
116 | , tag : String
|
117 | , tar : String
|
118 | , tmp : path
|
119 | , "unsafe-perm" : Boolean
|
120 | , usage : Boolean
|
121 | , user : String
|
122 | , username : String
|
123 | , userconfig : path
|
124 | , version : Boolean
|
125 | , viewer: path
|
126 | , _exit : Boolean
|
127 | , path: path
|
128 | }
|
129 |
|
130 | ; [["-v", {version:true}, []]
|
131 | ,["---v", {version:true}, []]
|
132 | ,["ls -s --no-reg connect -d",
|
133 | {loglevel:"info",registry:null},["ls","connect"]]
|
134 | ,["ls ---s foo",{loglevel:"silent"},["ls","foo"]]
|
135 | ,["ls --registry blargle", {}, ["ls"]]
|
136 | ,["--no-registry", {registry:null}, []]
|
137 | ,["--no-color true", {color:false}, []]
|
138 | ,["--no-color false", {color:true}, []]
|
139 | ,["--no-color", {color:false}, []]
|
140 | ,["--color false", {color:false}, []]
|
141 | ,["--color --logfd 7", {logfd:7,color:true}, []]
|
142 | ,["--color=true", {color:true}, []]
|
143 | ,["--logfd=10", {logfd:10}, []]
|
144 | ,["--tmp=/tmp -tar=gtar",{tmp:"/tmp",tar:"gtar"},[]]
|
145 | ,["--tmp=tmp -tar=gtar",
|
146 | {tmp:path.resolve(process.cwd(), "tmp"),tar:"gtar"},[]]
|
147 | ,["--logfd x", {}, []]
|
148 | ,["a -true -- -no-false", {true:true},["a","-no-false"]]
|
149 | ,["a -no-false", {false:false},["a"]]
|
150 | ,["a -no-no-true", {true:true}, ["a"]]
|
151 | ,["a -no-no-no-false", {false:false}, ["a"]]
|
152 | ,["---NO-no-No-no-no-no-nO-no-no"+
|
153 | "-No-no-no-no-no-no-no-no-no"+
|
154 | "-no-no-no-no-NO-NO-no-no-no-no-no-no"+
|
155 | "-no-body-can-do-the-boogaloo-like-I-do"
|
156 | ,{"body-can-do-the-boogaloo-like-I-do":false}, []]
|
157 | ,["we are -no-strangers-to-love "+
|
158 | "--you-know=the-rules --and=so-do-i "+
|
159 | "---im-thinking-of=a-full-commitment "+
|
160 | "--no-you-would-get-this-from-any-other-guy "+
|
161 | "--no-gonna-give-you-up "+
|
162 | "-no-gonna-let-you-down=true "+
|
163 | "--no-no-gonna-run-around false "+
|
164 | "--desert-you=false "+
|
165 | "--make-you-cry false "+
|
166 | "--no-tell-a-lie "+
|
167 | "--no-no-and-hurt-you false"
|
168 | ,{"strangers-to-love":false
|
169 | ,"you-know":"the-rules"
|
170 | ,"and":"so-do-i"
|
171 | ,"you-would-get-this-from-any-other-guy":false
|
172 | ,"gonna-give-you-up":false
|
173 | ,"gonna-let-you-down":false
|
174 | ,"gonna-run-around":false
|
175 | ,"desert-you":false
|
176 | ,"make-you-cry":false
|
177 | ,"tell-a-lie":false
|
178 | ,"and-hurt-you":false
|
179 | },["we", "are"]]
|
180 | ,["-t one -t two -t three"
|
181 | ,{t: ["one", "two", "three"]}
|
182 | ,[]]
|
183 | ,["-t one -t null -t three four five null"
|
184 | ,{t: ["one", "null", "three"]}
|
185 | ,["four", "five", "null"]]
|
186 | ,["-t foo"
|
187 | ,{t:["foo"]}
|
188 | ,[]]
|
189 | ,["--no-t"
|
190 | ,{t:["false"]}
|
191 | ,[]]
|
192 | ,["-no-no-t"
|
193 | ,{t:["true"]}
|
194 | ,[]]
|
195 | ,["-aoa one -aoa null -aoa 100"
|
196 | ,{aoa:["one", null, '100']}
|
197 | ,[]]
|
198 | ,["-str 100"
|
199 | ,{str:"100"}
|
200 | ,[]]
|
201 | ,["--color always"
|
202 | ,{color:"always"}
|
203 | ,[]]
|
204 | ,["--no-nullstream"
|
205 | ,{nullstream:null}
|
206 | ,[]]
|
207 | ,["--nullstream false"
|
208 | ,{nullstream:null}
|
209 | ,[]]
|
210 | ,["--notadate=2011-01-25"
|
211 | ,{notadate: "2011-01-25"}
|
212 | ,[]]
|
213 | ,["--date 2011-01-25"
|
214 | ,{date: new Date("2011-01-25")}
|
215 | ,[]]
|
216 | ,["-cl 1"
|
217 | ,{config: true, length: 1}
|
218 | ,[]
|
219 | ,{config: Boolean, length: Number, clear: Boolean}
|
220 | ,{c: "--config", l: "--length"}]
|
221 | ,["--acount bla"
|
222 | ,{"acount":true}
|
223 | ,["bla"]
|
224 | ,{account: Boolean, credentials: Boolean, options: String}
|
225 | ,{a:"--account", c:"--credentials",o:"--options"}]
|
226 | ,["--clear"
|
227 | ,{clear:true}
|
228 | ,[]
|
229 | ,{clear:Boolean,con:Boolean,len:Boolean,exp:Boolean,add:Boolean,rep:Boolean}
|
230 | ,{c:"--con",l:"--len",e:"--exp",a:"--add",r:"--rep"}]
|
231 | ,["--file -"
|
232 | ,{"file":"-"}
|
233 | ,[]
|
234 | ,{file:String}
|
235 | ,{}]
|
236 | ,["--file -"
|
237 | ,{"file":true}
|
238 | ,["-"]
|
239 | ,{file:Boolean}
|
240 | ,{}]
|
241 | ,["--path"
|
242 | ,{"path":null}
|
243 | ,[]]
|
244 | ,["--path ."
|
245 | ,{"path":process.cwd()}
|
246 | ,[]]
|
247 | ].forEach(function (test) {
|
248 | var argv = test[0].split(/\s+/)
|
249 | , opts = test[1]
|
250 | , rem = test[2]
|
251 | , actual = nopt(test[3] || types, test[4] || shorthands, argv, 0)
|
252 | , parsed = actual.argv
|
253 | delete actual.argv
|
254 | for (var i in opts) {
|
255 | var e = JSON.stringify(opts[i])
|
256 | , a = JSON.stringify(actual[i] === undefined ? null : actual[i])
|
257 | if (e && typeof e === "object") {
|
258 | t.deepEqual(e, a)
|
259 | } else {
|
260 | t.equal(e, a)
|
261 | }
|
262 | }
|
263 | t.deepEqual(rem, parsed.remain)
|
264 | })
|
265 | t.end()
|
266 | })
|