index.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. 'use strict';
  2. var tap = require('tap');
  3. var test = tap.test;
  4. var semver = require('../semver.js');
  5. var eq = semver.eq;
  6. var gt = semver.gt;
  7. var lt = semver.lt;
  8. var neq = semver.neq;
  9. var cmp = semver.cmp;
  10. var gte = semver.gte;
  11. var lte = semver.lte;
  12. var satisfies = semver.satisfies;
  13. var validRange = semver.validRange;
  14. var inc = semver.inc;
  15. var diff = semver.diff;
  16. var replaceStars = semver.replaceStars;
  17. var toComparators = semver.toComparators;
  18. var SemVer = semver.SemVer;
  19. var Range = semver.Range;
  20. test('\ncomparison tests', function(t) {
  21. // [version1, version2]
  22. // version1 should be greater than version2
  23. [['0.0.0', '0.0.0-foo'],
  24. ['0.0.1', '0.0.0'],
  25. ['1.0.0', '0.9.9'],
  26. ['0.10.0', '0.9.0'],
  27. ['0.99.0', '0.10.0'],
  28. ['2.0.0', '1.2.3'],
  29. ['v0.0.0', '0.0.0-foo', true],
  30. ['v0.0.1', '0.0.0', true],
  31. ['v1.0.0', '0.9.9', true],
  32. ['v0.10.0', '0.9.0', true],
  33. ['v0.99.0', '0.10.0', true],
  34. ['v2.0.0', '1.2.3', true],
  35. ['0.0.0', 'v0.0.0-foo', true],
  36. ['0.0.1', 'v0.0.0', true],
  37. ['1.0.0', 'v0.9.9', true],
  38. ['0.10.0', 'v0.9.0', true],
  39. ['0.99.0', 'v0.10.0', true],
  40. ['2.0.0', 'v1.2.3', true],
  41. ['1.2.3', '1.2.3-asdf'],
  42. ['1.2.3', '1.2.3-4'],
  43. ['1.2.3', '1.2.3-4-foo'],
  44. ['1.2.3-5-foo', '1.2.3-5'],
  45. ['1.2.3-5', '1.2.3-4'],
  46. ['1.2.3-5-foo', '1.2.3-5-Foo'],
  47. ['3.0.0', '2.7.2+asdf'],
  48. ['1.2.3-a.10', '1.2.3-a.5'],
  49. ['1.2.3-a.b', '1.2.3-a.5'],
  50. ['1.2.3-a.b', '1.2.3-a'],
  51. ['1.2.3-a.b.c.10.d.5', '1.2.3-a.b.c.5.d.100'],
  52. ['1.2.3-r2', '1.2.3-r100'],
  53. ['1.2.3-r100', '1.2.3-R2']
  54. ].forEach(function(v) {
  55. var v0 = v[0];
  56. var v1 = v[1];
  57. var loose = v[2];
  58. t.ok(gt(v0, v1, loose), "gt('" + v0 + "', '" + v1 + "')");
  59. t.ok(lt(v1, v0, loose), "lt('" + v1 + "', '" + v0 + "')");
  60. t.ok(!gt(v1, v0, loose), "!gt('" + v1 + "', '" + v0 + "')");
  61. t.ok(!lt(v0, v1, loose), "!lt('" + v0 + "', '" + v1 + "')");
  62. t.ok(eq(v0, v0, loose), "eq('" + v0 + "', '" + v0 + "')");
  63. t.ok(eq(v1, v1, loose), "eq('" + v1 + "', '" + v1 + "')");
  64. t.ok(neq(v0, v1, loose), "neq('" + v0 + "', '" + v1 + "')");
  65. t.ok(cmp(v1, '==', v1, loose), "cmp('" + v1 + "' == '" + v1 + "')");
  66. t.ok(cmp(v0, '>=', v1, loose), "cmp('" + v0 + "' >= '" + v1 + "')");
  67. t.ok(cmp(v1, '<=', v0, loose), "cmp('" + v1 + "' <= '" + v0 + "')");
  68. t.ok(cmp(v0, '!=', v1, loose), "cmp('" + v0 + "' != '" + v1 + "')");
  69. });
  70. t.end();
  71. });
  72. test('\nequality tests', function(t) {
  73. // [version1, version2]
  74. // version1 should be equivalent to version2
  75. [['1.2.3', 'v1.2.3', true],
  76. ['1.2.3', '=1.2.3', true],
  77. ['1.2.3', 'v 1.2.3', true],
  78. ['1.2.3', '= 1.2.3', true],
  79. ['1.2.3', ' v1.2.3', true],
  80. ['1.2.3', ' =1.2.3', true],
  81. ['1.2.3', ' v 1.2.3', true],
  82. ['1.2.3', ' = 1.2.3', true],
  83. ['1.2.3-0', 'v1.2.3-0', true],
  84. ['1.2.3-0', '=1.2.3-0', true],
  85. ['1.2.3-0', 'v 1.2.3-0', true],
  86. ['1.2.3-0', '= 1.2.3-0', true],
  87. ['1.2.3-0', ' v1.2.3-0', true],
  88. ['1.2.3-0', ' =1.2.3-0', true],
  89. ['1.2.3-0', ' v 1.2.3-0', true],
  90. ['1.2.3-0', ' = 1.2.3-0', true],
  91. ['1.2.3-1', 'v1.2.3-1', true],
  92. ['1.2.3-1', '=1.2.3-1', true],
  93. ['1.2.3-1', 'v 1.2.3-1', true],
  94. ['1.2.3-1', '= 1.2.3-1', true],
  95. ['1.2.3-1', ' v1.2.3-1', true],
  96. ['1.2.3-1', ' =1.2.3-1', true],
  97. ['1.2.3-1', ' v 1.2.3-1', true],
  98. ['1.2.3-1', ' = 1.2.3-1', true],
  99. ['1.2.3-beta', 'v1.2.3-beta', true],
  100. ['1.2.3-beta', '=1.2.3-beta', true],
  101. ['1.2.3-beta', 'v 1.2.3-beta', true],
  102. ['1.2.3-beta', '= 1.2.3-beta', true],
  103. ['1.2.3-beta', ' v1.2.3-beta', true],
  104. ['1.2.3-beta', ' =1.2.3-beta', true],
  105. ['1.2.3-beta', ' v 1.2.3-beta', true],
  106. ['1.2.3-beta', ' = 1.2.3-beta', true],
  107. ['1.2.3-beta+build', ' = 1.2.3-beta+otherbuild', true],
  108. ['1.2.3+build', ' = 1.2.3+otherbuild', true],
  109. ['1.2.3-beta+build', '1.2.3-beta+otherbuild'],
  110. ['1.2.3+build', '1.2.3+otherbuild'],
  111. [' v1.2.3+build', '1.2.3+otherbuild']
  112. ].forEach(function(v) {
  113. var v0 = v[0];
  114. var v1 = v[1];
  115. var loose = v[2];
  116. t.ok(eq(v0, v1, loose), "eq('" + v0 + "', '" + v1 + "')");
  117. t.ok(!neq(v0, v1, loose), "!neq('" + v0 + "', '" + v1 + "')");
  118. t.ok(cmp(v0, '==', v1, loose), 'cmp(' + v0 + '==' + v1 + ')');
  119. t.ok(!cmp(v0, '!=', v1, loose), '!cmp(' + v0 + '!=' + v1 + ')');
  120. t.ok(!cmp(v0, '===', v1, loose), '!cmp(' + v0 + '===' + v1 + ')');
  121. t.ok(cmp(v0, '!==', v1, loose), 'cmp(' + v0 + '!==' + v1 + ')');
  122. t.ok(!gt(v0, v1, loose), "!gt('" + v0 + "', '" + v1 + "')");
  123. t.ok(gte(v0, v1, loose), "gte('" + v0 + "', '" + v1 + "')");
  124. t.ok(!lt(v0, v1, loose), "!lt('" + v0 + "', '" + v1 + "')");
  125. t.ok(lte(v0, v1, loose), "lte('" + v0 + "', '" + v1 + "')");
  126. });
  127. t.end();
  128. });
  129. test('\nrange tests', function(t) {
  130. // [range, version]
  131. // version should be included by range
  132. [['1.0.0 - 2.0.0', '1.2.3'],
  133. ['^1.2.3+build', '1.2.3'],
  134. ['^1.2.3+build', '1.3.0'],
  135. ['1.2.3-pre+asdf - 2.4.3-pre+asdf', '1.2.3'],
  136. ['1.2.3pre+asdf - 2.4.3-pre+asdf', '1.2.3', true],
  137. ['1.2.3-pre+asdf - 2.4.3pre+asdf', '1.2.3', true],
  138. ['1.2.3pre+asdf - 2.4.3pre+asdf', '1.2.3', true],
  139. ['1.2.3-pre+asdf - 2.4.3-pre+asdf', '1.2.3-pre.2'],
  140. ['1.2.3-pre+asdf - 2.4.3-pre+asdf', '2.4.3-alpha'],
  141. ['1.2.3+asdf - 2.4.3+asdf', '1.2.3'],
  142. ['1.0.0', '1.0.0'],
  143. ['>=*', '0.2.4'],
  144. ['', '1.0.0'],
  145. ['*', '1.2.3'],
  146. ['*', 'v1.2.3', true],
  147. ['>=1.0.0', '1.0.0'],
  148. ['>=1.0.0', '1.0.1'],
  149. ['>=1.0.0', '1.1.0'],
  150. ['>1.0.0', '1.0.1'],
  151. ['>1.0.0', '1.1.0'],
  152. ['<=2.0.0', '2.0.0'],
  153. ['<=2.0.0', '1.9999.9999'],
  154. ['<=2.0.0', '0.2.9'],
  155. ['<2.0.0', '1.9999.9999'],
  156. ['<2.0.0', '0.2.9'],
  157. ['>= 1.0.0', '1.0.0'],
  158. ['>= 1.0.0', '1.0.1'],
  159. ['>= 1.0.0', '1.1.0'],
  160. ['> 1.0.0', '1.0.1'],
  161. ['> 1.0.0', '1.1.0'],
  162. ['<= 2.0.0', '2.0.0'],
  163. ['<= 2.0.0', '1.9999.9999'],
  164. ['<= 2.0.0', '0.2.9'],
  165. ['< 2.0.0', '1.9999.9999'],
  166. ['<\t2.0.0', '0.2.9'],
  167. ['>=0.1.97', 'v0.1.97', true],
  168. ['>=0.1.97', '0.1.97'],
  169. ['0.1.20 || 1.2.4', '1.2.4'],
  170. ['>=0.2.3 || <0.0.1', '0.0.0'],
  171. ['>=0.2.3 || <0.0.1', '0.2.3'],
  172. ['>=0.2.3 || <0.0.1', '0.2.4'],
  173. ['||', '1.3.4'],
  174. ['2.x.x', '2.1.3'],
  175. ['1.2.x', '1.2.3'],
  176. ['1.2.x || 2.x', '2.1.3'],
  177. ['1.2.x || 2.x', '1.2.3'],
  178. ['x', '1.2.3'],
  179. ['2.*.*', '2.1.3'],
  180. ['1.2.*', '1.2.3'],
  181. ['1.2.* || 2.*', '2.1.3'],
  182. ['1.2.* || 2.*', '1.2.3'],
  183. ['*', '1.2.3'],
  184. ['2', '2.1.2'],
  185. ['2.3', '2.3.1'],
  186. ['~2.4', '2.4.0'], // >=2.4.0 <2.5.0
  187. ['~2.4', '2.4.5'],
  188. ['~>3.2.1', '3.2.2'], // >=3.2.1 <3.3.0,
  189. ['~1', '1.2.3'], // >=1.0.0 <2.0.0
  190. ['~>1', '1.2.3'],
  191. ['~> 1', '1.2.3'],
  192. ['~1.0', '1.0.2'], // >=1.0.0 <1.1.0,
  193. ['~ 1.0', '1.0.2'],
  194. ['~ 1.0.3', '1.0.12'],
  195. ['>=1', '1.0.0'],
  196. ['>= 1', '1.0.0'],
  197. ['<1.2', '1.1.1'],
  198. ['< 1.2', '1.1.1'],
  199. ['~v0.5.4-pre', '0.5.5'],
  200. ['~v0.5.4-pre', '0.5.4'],
  201. ['=0.7.x', '0.7.2'],
  202. ['<=0.7.x', '0.7.2'],
  203. ['>=0.7.x', '0.7.2'],
  204. ['<=0.7.x', '0.6.2'],
  205. ['~1.2.1 >=1.2.3', '1.2.3'],
  206. ['~1.2.1 =1.2.3', '1.2.3'],
  207. ['~1.2.1 1.2.3', '1.2.3'],
  208. ['~1.2.1 >=1.2.3 1.2.3', '1.2.3'],
  209. ['~1.2.1 1.2.3 >=1.2.3', '1.2.3'],
  210. ['~1.2.1 1.2.3', '1.2.3'],
  211. ['>=1.2.1 1.2.3', '1.2.3'],
  212. ['1.2.3 >=1.2.1', '1.2.3'],
  213. ['>=1.2.3 >=1.2.1', '1.2.3'],
  214. ['>=1.2.1 >=1.2.3', '1.2.3'],
  215. ['>=1.2', '1.2.8'],
  216. ['^1.2.3', '1.8.1'],
  217. ['^0.1.2', '0.1.2'],
  218. ['^0.1', '0.1.2'],
  219. ['^1.2', '1.4.2'],
  220. ['^1.2 ^1', '1.4.2'],
  221. ['^1.2.3-alpha', '1.2.3-pre'],
  222. ['^1.2.0-alpha', '1.2.0-pre'],
  223. ['^0.0.1-alpha', '0.0.1-beta']
  224. ].forEach(function(v) {
  225. var range = v[0];
  226. var ver = v[1];
  227. var loose = v[2];
  228. t.ok(satisfies(ver, range, loose), range + ' satisfied by ' + ver);
  229. });
  230. t.end();
  231. });
  232. test('\nnegative range tests', function(t) {
  233. // [range, version]
  234. // version should not be included by range
  235. [['1.0.0 - 2.0.0', '2.2.3'],
  236. ['1.2.3+asdf - 2.4.3+asdf', '1.2.3-pre.2'],
  237. ['1.2.3+asdf - 2.4.3+asdf', '2.4.3-alpha'],
  238. ['^1.2.3+build', '2.0.0'],
  239. ['^1.2.3+build', '1.2.0'],
  240. ['^1.2.3', '1.2.3-pre'],
  241. ['^1.2', '1.2.0-pre'],
  242. ['>1.2', '1.3.0-beta'],
  243. ['<=1.2.3', '1.2.3-beta'],
  244. ['^1.2.3', '1.2.3-beta'],
  245. ['=0.7.x', '0.7.0-asdf'],
  246. ['>=0.7.x', '0.7.0-asdf'],
  247. ['1', '1.0.0beta', true],
  248. ['<1', '1.0.0beta', true],
  249. ['< 1', '1.0.0beta', true],
  250. ['1.0.0', '1.0.1'],
  251. ['>=1.0.0', '0.0.0'],
  252. ['>=1.0.0', '0.0.1'],
  253. ['>=1.0.0', '0.1.0'],
  254. ['>1.0.0', '0.0.1'],
  255. ['>1.0.0', '0.1.0'],
  256. ['<=2.0.0', '3.0.0'],
  257. ['<=2.0.0', '2.9999.9999'],
  258. ['<=2.0.0', '2.2.9'],
  259. ['<2.0.0', '2.9999.9999'],
  260. ['<2.0.0', '2.2.9'],
  261. ['>=0.1.97', 'v0.1.93', true],
  262. ['>=0.1.97', '0.1.93'],
  263. ['0.1.20 || 1.2.4', '1.2.3'],
  264. ['>=0.2.3 || <0.0.1', '0.0.3'],
  265. ['>=0.2.3 || <0.0.1', '0.2.2'],
  266. ['2.x.x', '1.1.3'],
  267. ['2.x.x', '3.1.3'],
  268. ['1.2.x', '1.3.3'],
  269. ['1.2.x || 2.x', '3.1.3'],
  270. ['1.2.x || 2.x', '1.1.3'],
  271. ['2.*.*', '1.1.3'],
  272. ['2.*.*', '3.1.3'],
  273. ['1.2.*', '1.3.3'],
  274. ['1.2.* || 2.*', '3.1.3'],
  275. ['1.2.* || 2.*', '1.1.3'],
  276. ['2', '1.1.2'],
  277. ['2.3', '2.4.1'],
  278. ['~2.4', '2.5.0'], // >=2.4.0 <2.5.0
  279. ['~2.4', '2.3.9'],
  280. ['~>3.2.1', '3.3.2'], // >=3.2.1 <3.3.0
  281. ['~>3.2.1', '3.2.0'], // >=3.2.1 <3.3.0
  282. ['~1', '0.2.3'], // >=1.0.0 <2.0.0
  283. ['~>1', '2.2.3'],
  284. ['~1.0', '1.1.0'], // >=1.0.0 <1.1.0
  285. ['<1', '1.0.0'],
  286. ['>=1.2', '1.1.1'],
  287. ['1', '2.0.0beta', true],
  288. ['~v0.5.4-beta', '0.5.4-alpha'],
  289. ['=0.7.x', '0.8.2'],
  290. ['>=0.7.x', '0.6.2'],
  291. ['<0.7.x', '0.7.2'],
  292. ['<1.2.3', '1.2.3-beta'],
  293. ['=1.2.3', '1.2.3-beta'],
  294. ['>1.2', '1.2.8'],
  295. ['^1.2.3', '2.0.0-alpha'],
  296. ['^1.2.3', '1.2.2'],
  297. ['^1.2', '1.1.9'],
  298. ['*', 'v1.2.3-foo', true],
  299. // invalid ranges never satisfied!
  300. ['blerg', '1.2.3'],
  301. ['git+https://user:password0123@github.com/foo', '123.0.0', true],
  302. ['^1.2.3', '2.0.0-pre']
  303. ].forEach(function(v) {
  304. var range = v[0];
  305. var ver = v[1];
  306. var loose = v[2];
  307. var found = satisfies(ver, range, loose);
  308. t.ok(!found, ver + ' not satisfied by ' + range);
  309. });
  310. t.end();
  311. });
  312. test('\nincrement versions test', function(t) {
  313. // [version, inc, result, identifier]
  314. // inc(version, inc) -> result
  315. [['1.2.3', 'major', '2.0.0'],
  316. ['1.2.3', 'minor', '1.3.0'],
  317. ['1.2.3', 'patch', '1.2.4'],
  318. ['1.2.3tag', 'major', '2.0.0', true],
  319. ['1.2.3-tag', 'major', '2.0.0'],
  320. ['1.2.3', 'fake', null],
  321. ['1.2.0-0', 'patch', '1.2.0'],
  322. ['fake', 'major', null],
  323. ['1.2.3-4', 'major', '2.0.0'],
  324. ['1.2.3-4', 'minor', '1.3.0'],
  325. ['1.2.3-4', 'patch', '1.2.3'],
  326. ['1.2.3-alpha.0.beta', 'major', '2.0.0'],
  327. ['1.2.3-alpha.0.beta', 'minor', '1.3.0'],
  328. ['1.2.3-alpha.0.beta', 'patch', '1.2.3'],
  329. ['1.2.4', 'prerelease', '1.2.5-0'],
  330. ['1.2.3-0', 'prerelease', '1.2.3-1'],
  331. ['1.2.3-alpha.0', 'prerelease', '1.2.3-alpha.1'],
  332. ['1.2.3-alpha.1', 'prerelease', '1.2.3-alpha.2'],
  333. ['1.2.3-alpha.2', 'prerelease', '1.2.3-alpha.3'],
  334. ['1.2.3-alpha.0.beta', 'prerelease', '1.2.3-alpha.1.beta'],
  335. ['1.2.3-alpha.1.beta', 'prerelease', '1.2.3-alpha.2.beta'],
  336. ['1.2.3-alpha.2.beta', 'prerelease', '1.2.3-alpha.3.beta'],
  337. ['1.2.3-alpha.10.0.beta', 'prerelease', '1.2.3-alpha.10.1.beta'],
  338. ['1.2.3-alpha.10.1.beta', 'prerelease', '1.2.3-alpha.10.2.beta'],
  339. ['1.2.3-alpha.10.2.beta', 'prerelease', '1.2.3-alpha.10.3.beta'],
  340. ['1.2.3-alpha.10.beta.0', 'prerelease', '1.2.3-alpha.10.beta.1'],
  341. ['1.2.3-alpha.10.beta.1', 'prerelease', '1.2.3-alpha.10.beta.2'],
  342. ['1.2.3-alpha.10.beta.2', 'prerelease', '1.2.3-alpha.10.beta.3'],
  343. ['1.2.3-alpha.9.beta', 'prerelease', '1.2.3-alpha.10.beta'],
  344. ['1.2.3-alpha.10.beta', 'prerelease', '1.2.3-alpha.11.beta'],
  345. ['1.2.3-alpha.11.beta', 'prerelease', '1.2.3-alpha.12.beta'],
  346. ['1.2.0', 'prepatch', '1.2.1-0'],
  347. ['1.2.0-1', 'prepatch', '1.2.1-0'],
  348. ['1.2.0', 'preminor', '1.3.0-0'],
  349. ['1.2.3-1', 'preminor', '1.3.0-0'],
  350. ['1.2.0', 'premajor', '2.0.0-0'],
  351. ['1.2.3-1', 'premajor', '2.0.0-0'],
  352. ['1.2.0-1', 'minor', '1.2.0'],
  353. ['1.0.0-1', 'major', '1.0.0'],
  354. ['1.2.3', 'major', '2.0.0', false, 'dev'],
  355. ['1.2.3', 'minor', '1.3.0', false, 'dev'],
  356. ['1.2.3', 'patch', '1.2.4', false, 'dev'],
  357. ['1.2.3tag', 'major', '2.0.0', true, 'dev'],
  358. ['1.2.3-tag', 'major', '2.0.0', false, 'dev'],
  359. ['1.2.3', 'fake', null, false, 'dev'],
  360. ['1.2.0-0', 'patch', '1.2.0', false, 'dev'],
  361. ['fake', 'major', null, false, 'dev'],
  362. ['1.2.3-4', 'major', '2.0.0', false, 'dev'],
  363. ['1.2.3-4', 'minor', '1.3.0', false, 'dev'],
  364. ['1.2.3-4', 'patch', '1.2.3', false, 'dev'],
  365. ['1.2.3-alpha.0.beta', 'major', '2.0.0', false, 'dev'],
  366. ['1.2.3-alpha.0.beta', 'minor', '1.3.0', false, 'dev'],
  367. ['1.2.3-alpha.0.beta', 'patch', '1.2.3', false, 'dev'],
  368. ['1.2.4', 'prerelease', '1.2.5-dev.0', false, 'dev'],
  369. ['1.2.3-0', 'prerelease', '1.2.3-dev.0', false, 'dev'],
  370. ['1.2.3-alpha.0', 'prerelease', '1.2.3-dev.0', false, 'dev'],
  371. ['1.2.3-alpha.0', 'prerelease', '1.2.3-alpha.1', false, 'alpha'],
  372. ['1.2.3-alpha.0.beta', 'prerelease', '1.2.3-dev.0', false, 'dev'],
  373. ['1.2.3-alpha.0.beta', 'prerelease', '1.2.3-alpha.1.beta', false, 'alpha'],
  374. ['1.2.3-alpha.10.0.beta', 'prerelease', '1.2.3-dev.0', false, 'dev'],
  375. ['1.2.3-alpha.10.0.beta', 'prerelease', '1.2.3-alpha.10.1.beta', false, 'alpha'],
  376. ['1.2.3-alpha.10.1.beta', 'prerelease', '1.2.3-alpha.10.2.beta', false, 'alpha'],
  377. ['1.2.3-alpha.10.2.beta', 'prerelease', '1.2.3-alpha.10.3.beta', false, 'alpha'],
  378. ['1.2.3-alpha.10.beta.0', 'prerelease', '1.2.3-dev.0', false, 'dev'],
  379. ['1.2.3-alpha.10.beta.0', 'prerelease', '1.2.3-alpha.10.beta.1', false, 'alpha'],
  380. ['1.2.3-alpha.10.beta.1', 'prerelease', '1.2.3-alpha.10.beta.2', false, 'alpha'],
  381. ['1.2.3-alpha.10.beta.2', 'prerelease', '1.2.3-alpha.10.beta.3', false, 'alpha'],
  382. ['1.2.3-alpha.9.beta', 'prerelease', '1.2.3-dev.0', false, 'dev'],
  383. ['1.2.3-alpha.9.beta', 'prerelease', '1.2.3-alpha.10.beta', false, 'alpha'],
  384. ['1.2.3-alpha.10.beta', 'prerelease', '1.2.3-alpha.11.beta', false, 'alpha'],
  385. ['1.2.3-alpha.11.beta', 'prerelease', '1.2.3-alpha.12.beta', false, 'alpha'],
  386. ['1.2.0', 'prepatch', '1.2.1-dev.0', 'dev'],
  387. ['1.2.0-1', 'prepatch', '1.2.1-dev.0', 'dev'],
  388. ['1.2.0', 'preminor', '1.3.0-dev.0', 'dev'],
  389. ['1.2.3-1', 'preminor', '1.3.0-dev.0', 'dev'],
  390. ['1.2.0', 'premajor', '2.0.0-dev.0', 'dev'],
  391. ['1.2.3-1', 'premajor', '2.0.0-dev.0', 'dev'],
  392. ['1.2.0-1', 'minor', '1.2.0', 'dev'],
  393. ['1.0.0-1', 'major', '1.0.0', 'dev'],
  394. ['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.0', false, 'dev']
  395. ].forEach(function(v) {
  396. var pre = v[0];
  397. var what = v[1];
  398. var wanted = v[2];
  399. var loose = v[3];
  400. var id = v[4];
  401. var found = inc(pre, what, loose, id);
  402. var cmd = 'inc(' + pre + ', ' + what + ', ' + id + ')';
  403. t.equal(found, wanted, cmd + ' === ' + wanted);
  404. });
  405. t.end();
  406. });
  407. test('\ndiff versions test', function(t) {
  408. // [version1, version2, result]
  409. // diff(version1, version2) -> result
  410. [['1.2.3', '0.2.3', 'major'],
  411. ['1.4.5', '0.2.3', 'major'],
  412. ['1.2.3', '2.0.0-pre', 'premajor'],
  413. ['1.2.3', '1.3.3', 'minor'],
  414. ['1.0.1', '1.1.0-pre', 'preminor'],
  415. ['1.2.3', '1.2.4', 'patch'],
  416. ['1.2.3', '1.2.4-pre', 'prepatch'],
  417. ['0.0.1', '0.0.1-pre', 'prerelease'],
  418. ['0.0.1', '0.0.1-pre-2', 'prerelease'],
  419. ['1.1.0', '1.1.0-pre', 'prerelease'],
  420. ['1.1.0-pre-1', '1.1.0-pre-2', 'prerelease'],
  421. ['1.0.0', '1.0.0', null]
  422. ].forEach(function(v) {
  423. var version1 = v[0];
  424. var version2 = v[1];
  425. var wanted = v[2];
  426. var found = diff(version1, version2);
  427. var cmd = 'diff(' + version1 + ', ' + version2 + ')';
  428. t.equal(found, wanted, cmd + ' === ' + wanted);
  429. });
  430. t.end();
  431. });
  432. test('\nvalid range test', function(t) {
  433. // [range, result]
  434. // validRange(range) -> result
  435. // translate ranges into their canonical form
  436. [['1.0.0 - 2.0.0', '>=1.0.0 <=2.0.0'],
  437. ['1.0.0', '1.0.0'],
  438. ['>=*', '*'],
  439. ['', '*'],
  440. ['*', '*'],
  441. ['*', '*'],
  442. ['>=1.0.0', '>=1.0.0'],
  443. ['>1.0.0', '>1.0.0'],
  444. ['<=2.0.0', '<=2.0.0'],
  445. ['1', '>=1.0.0 <2.0.0'],
  446. ['<=2.0.0', '<=2.0.0'],
  447. ['<=2.0.0', '<=2.0.0'],
  448. ['<2.0.0', '<2.0.0'],
  449. ['<2.0.0', '<2.0.0'],
  450. ['>= 1.0.0', '>=1.0.0'],
  451. ['>= 1.0.0', '>=1.0.0'],
  452. ['>= 1.0.0', '>=1.0.0'],
  453. ['> 1.0.0', '>1.0.0'],
  454. ['> 1.0.0', '>1.0.0'],
  455. ['<= 2.0.0', '<=2.0.0'],
  456. ['<= 2.0.0', '<=2.0.0'],
  457. ['<= 2.0.0', '<=2.0.0'],
  458. ['< 2.0.0', '<2.0.0'],
  459. ['< 2.0.0', '<2.0.0'],
  460. ['>=0.1.97', '>=0.1.97'],
  461. ['>=0.1.97', '>=0.1.97'],
  462. ['0.1.20 || 1.2.4', '0.1.20||1.2.4'],
  463. ['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'],
  464. ['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'],
  465. ['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'],
  466. ['||', '||'],
  467. ['2.x.x', '>=2.0.0 <3.0.0'],
  468. ['1.2.x', '>=1.2.0 <1.3.0'],
  469. ['1.2.x || 2.x', '>=1.2.0 <1.3.0||>=2.0.0 <3.0.0'],
  470. ['1.2.x || 2.x', '>=1.2.0 <1.3.0||>=2.0.0 <3.0.0'],
  471. ['x', '*'],
  472. ['2.*.*', '>=2.0.0 <3.0.0'],
  473. ['1.2.*', '>=1.2.0 <1.3.0'],
  474. ['1.2.* || 2.*', '>=1.2.0 <1.3.0||>=2.0.0 <3.0.0'],
  475. ['*', '*'],
  476. ['2', '>=2.0.0 <3.0.0'],
  477. ['2.3', '>=2.3.0 <2.4.0'],
  478. ['~2.4', '>=2.4.0 <2.5.0'],
  479. ['~2.4', '>=2.4.0 <2.5.0'],
  480. ['~>3.2.1', '>=3.2.1 <3.3.0'],
  481. ['~1', '>=1.0.0 <2.0.0'],
  482. ['~>1', '>=1.0.0 <2.0.0'],
  483. ['~> 1', '>=1.0.0 <2.0.0'],
  484. ['~1.0', '>=1.0.0 <1.1.0'],
  485. ['~ 1.0', '>=1.0.0 <1.1.0'],
  486. ['^0', '>=0.0.0 <1.0.0'],
  487. ['^ 1', '>=1.0.0 <2.0.0'],
  488. ['^0.1', '>=0.1.0 <0.2.0'],
  489. ['^1.0', '>=1.0.0 <2.0.0'],
  490. ['^1.2', '>=1.2.0 <2.0.0'],
  491. ['^0.0.1', '>=0.0.1 <0.0.2'],
  492. ['^0.0.1-beta', '>=0.0.1-beta <0.0.2'],
  493. ['^0.1.2', '>=0.1.2 <0.2.0'],
  494. ['^1.2.3', '>=1.2.3 <2.0.0'],
  495. ['^1.2.3-beta.4', '>=1.2.3-beta.4 <2.0.0'],
  496. ['<1', '<1.0.0'],
  497. ['< 1', '<1.0.0'],
  498. ['>=1', '>=1.0.0'],
  499. ['>= 1', '>=1.0.0'],
  500. ['<1.2', '<1.2.0'],
  501. ['< 1.2', '<1.2.0'],
  502. ['1', '>=1.0.0 <2.0.0'],
  503. ['>01.02.03', '>1.2.3', true],
  504. ['>01.02.03', null],
  505. ['~1.2.3beta', '>=1.2.3-beta <1.3.0', true],
  506. ['~1.2.3beta', null],
  507. ['^ 1.2 ^ 1', '>=1.2.0 <2.0.0 >=1.0.0 <2.0.0']
  508. ].forEach(function(v) {
  509. var pre = v[0];
  510. var wanted = v[1];
  511. var loose = v[2];
  512. var found = validRange(pre, loose);
  513. t.equal(found, wanted, 'validRange(' + pre + ') === ' + wanted);
  514. });
  515. t.end();
  516. });
  517. test('\ncomparators test', function(t) {
  518. // [range, comparators]
  519. // turn range into a set of individual comparators
  520. [['1.0.0 - 2.0.0', [['>=1.0.0', '<=2.0.0']]],
  521. ['1.0.0', [['1.0.0']]],
  522. ['>=*', [['']]],
  523. ['', [['']]],
  524. ['*', [['']]],
  525. ['*', [['']]],
  526. ['>=1.0.0', [['>=1.0.0']]],
  527. ['>=1.0.0', [['>=1.0.0']]],
  528. ['>=1.0.0', [['>=1.0.0']]],
  529. ['>1.0.0', [['>1.0.0']]],
  530. ['>1.0.0', [['>1.0.0']]],
  531. ['<=2.0.0', [['<=2.0.0']]],
  532. ['1', [['>=1.0.0', '<2.0.0']]],
  533. ['<=2.0.0', [['<=2.0.0']]],
  534. ['<=2.0.0', [['<=2.0.0']]],
  535. ['<2.0.0', [['<2.0.0']]],
  536. ['<2.0.0', [['<2.0.0']]],
  537. ['>= 1.0.0', [['>=1.0.0']]],
  538. ['>= 1.0.0', [['>=1.0.0']]],
  539. ['>= 1.0.0', [['>=1.0.0']]],
  540. ['> 1.0.0', [['>1.0.0']]],
  541. ['> 1.0.0', [['>1.0.0']]],
  542. ['<= 2.0.0', [['<=2.0.0']]],
  543. ['<= 2.0.0', [['<=2.0.0']]],
  544. ['<= 2.0.0', [['<=2.0.0']]],
  545. ['< 2.0.0', [['<2.0.0']]],
  546. ['<\t2.0.0', [['<2.0.0']]],
  547. ['>=0.1.97', [['>=0.1.97']]],
  548. ['>=0.1.97', [['>=0.1.97']]],
  549. ['0.1.20 || 1.2.4', [['0.1.20'], ['1.2.4']]],
  550. ['>=0.2.3 || <0.0.1', [['>=0.2.3'], ['<0.0.1']]],
  551. ['>=0.2.3 || <0.0.1', [['>=0.2.3'], ['<0.0.1']]],
  552. ['>=0.2.3 || <0.0.1', [['>=0.2.3'], ['<0.0.1']]],
  553. ['||', [[''], ['']]],
  554. ['2.x.x', [['>=2.0.0', '<3.0.0']]],
  555. ['1.2.x', [['>=1.2.0', '<1.3.0']]],
  556. ['1.2.x || 2.x', [['>=1.2.0', '<1.3.0'], ['>=2.0.0', '<3.0.0']]],
  557. ['1.2.x || 2.x', [['>=1.2.0', '<1.3.0'], ['>=2.0.0', '<3.0.0']]],
  558. ['x', [['']]],
  559. ['2.*.*', [['>=2.0.0', '<3.0.0']]],
  560. ['1.2.*', [['>=1.2.0', '<1.3.0']]],
  561. ['1.2.* || 2.*', [['>=1.2.0', '<1.3.0'], ['>=2.0.0', '<3.0.0']]],
  562. ['1.2.* || 2.*', [['>=1.2.0', '<1.3.0'], ['>=2.0.0', '<3.0.0']]],
  563. ['*', [['']]],
  564. ['2', [['>=2.0.0', '<3.0.0']]],
  565. ['2.3', [['>=2.3.0', '<2.4.0']]],
  566. ['~2.4', [['>=2.4.0', '<2.5.0']]],
  567. ['~2.4', [['>=2.4.0', '<2.5.0']]],
  568. ['~>3.2.1', [['>=3.2.1', '<3.3.0']]],
  569. ['~1', [['>=1.0.0', '<2.0.0']]],
  570. ['~>1', [['>=1.0.0', '<2.0.0']]],
  571. ['~> 1', [['>=1.0.0', '<2.0.0']]],
  572. ['~1.0', [['>=1.0.0', '<1.1.0']]],
  573. ['~ 1.0', [['>=1.0.0', '<1.1.0']]],
  574. ['~ 1.0.3', [['>=1.0.3', '<1.1.0']]],
  575. ['~> 1.0.3', [['>=1.0.3', '<1.1.0']]],
  576. ['<1', [['<1.0.0']]],
  577. ['< 1', [['<1.0.0']]],
  578. ['>=1', [['>=1.0.0']]],
  579. ['>= 1', [['>=1.0.0']]],
  580. ['<1.2', [['<1.2.0']]],
  581. ['< 1.2', [['<1.2.0']]],
  582. ['1', [['>=1.0.0', '<2.0.0']]],
  583. ['1 2', [['>=1.0.0', '<2.0.0', '>=2.0.0', '<3.0.0']]],
  584. ['1.2 - 3.4.5', [['>=1.2.0', '<=3.4.5']]],
  585. ['1.2.3 - 3.4', [['>=1.2.3', '<3.5.0']]],
  586. ['1.2.3 - 3', [['>=1.2.3', '<4.0.0']]],
  587. ['>*', [['<0.0.0']]],
  588. ['<*', [['<0.0.0']]]
  589. ].forEach(function(v) {
  590. var pre = v[0];
  591. var wanted = v[1];
  592. var found = toComparators(v[0]);
  593. var jw = JSON.stringify(wanted);
  594. t.equivalent(found, wanted, 'toComparators(' + pre + ') === ' + jw);
  595. });
  596. t.end();
  597. });
  598. test('\ninvalid version numbers', function(t) {
  599. ['1.2.3.4',
  600. 'NOT VALID',
  601. 1.2,
  602. null,
  603. 'Infinity.NaN.Infinity'
  604. ].forEach(function(v) {
  605. t.throws(function() {
  606. new SemVer(v);
  607. }, {name:'TypeError', message:'Invalid Version: ' + v});
  608. });
  609. t.end();
  610. });
  611. test('\nstrict vs loose version numbers', function(t) {
  612. [['=1.2.3', '1.2.3'],
  613. ['01.02.03', '1.2.3'],
  614. ['1.2.3-beta.01', '1.2.3-beta.1'],
  615. [' =1.2.3', '1.2.3'],
  616. ['1.2.3foo', '1.2.3-foo']
  617. ].forEach(function(v) {
  618. var loose = v[0];
  619. var strict = v[1];
  620. t.throws(function() {
  621. new SemVer(loose);
  622. });
  623. var lv = new SemVer(loose, true);
  624. t.equal(lv.version, strict);
  625. t.ok(eq(loose, strict, true));
  626. t.throws(function() {
  627. eq(loose, strict);
  628. });
  629. t.throws(function() {
  630. new SemVer(strict).compare(loose);
  631. });
  632. });
  633. t.end();
  634. });
  635. test('\nstrict vs loose ranges', function(t) {
  636. [['>=01.02.03', '>=1.2.3'],
  637. ['~1.02.03beta', '>=1.2.3-beta <1.3.0']
  638. ].forEach(function(v) {
  639. var loose = v[0];
  640. var comps = v[1];
  641. t.throws(function() {
  642. new Range(loose);
  643. });
  644. t.equal(new Range(loose, true).range, comps);
  645. });
  646. t.end();
  647. });
  648. test('\nmax satisfying', function(t) {
  649. [[['1.2.3', '1.2.4'], '1.2', '1.2.4'],
  650. [['1.2.4', '1.2.3'], '1.2', '1.2.4'],
  651. [['1.2.3', '1.2.4', '1.2.5', '1.2.6'], '~1.2.3', '1.2.6'],
  652. [['1.1.0', '1.2.0', '1.2.1', '1.3.0', '2.0.0b1', '2.0.0b2', '2.0.0b3', '2.0.0', '2.1.0'], '~2.0.0', '2.0.0', true]
  653. ].forEach(function(v) {
  654. var versions = v[0];
  655. var range = v[1];
  656. var expect = v[2];
  657. var loose = v[3];
  658. var actual = semver.maxSatisfying(versions, range, loose);
  659. t.equal(actual, expect);
  660. });
  661. t.end();
  662. });