Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

basic.js 8.5KB

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