From 3ebc11dae87205cfa18d9a395e6bcbd593acb96a Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 24 Jul 2017 09:38:52 -0700 Subject: [PATCH] more test coverage --- semver.js | 2 +- test/index.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/semver.js b/semver.js index 33f36e58..58c607df 100644 --- a/semver.js +++ b/semver.js @@ -563,7 +563,7 @@ function patch(a, loose) { exports.compare = compare; function compare(a, b, loose) { - return new SemVer(a, loose).compare(b); + return new SemVer(a, loose).compare(new SemVer(b, loose)); } exports.compareLoose = compareLoose; diff --git a/test/index.js b/test/index.js index 4996ea74..90e189b2 100644 --- a/test/index.js +++ b/test/index.js @@ -189,6 +189,8 @@ test('\nrange tests', function(t) { ['*', '1.2.3'], ['2', '2.1.2'], ['2.3', '2.3.1'], + ['~x', '0.0.9'], // >=2.4.0 <2.5.0 + ['~2', '2.0.9'], // >=2.4.0 <2.5.0 ['~2.4', '2.4.0'], // >=2.4.0 <2.5.0 ['~2.4', '2.4.5'], ['~>3.2.1', '3.2.2'], // >=3.2.1 <3.3.0, @@ -222,11 +224,19 @@ test('\nrange tests', function(t) { ['^1.2.3', '1.8.1'], ['^0.1.2', '0.1.2'], ['^0.1', '0.1.2'], + ['^0.0.1', '0.0.1'], ['^1.2', '1.4.2'], ['^1.2 ^1', '1.4.2'], ['^1.2.3-alpha', '1.2.3-pre'], ['^1.2.0-alpha', '1.2.0-pre'], - ['^0.0.1-alpha', '0.0.1-beta'] + ['^0.0.1-alpha', '0.0.1-beta'], + ['^0.1.1-alpha', '0.1.1-beta'], + ['^x', '1.2.3'], + ['x - 1.0.0', '0.9.7'], + ['x - 1.x', '0.9.7'], + ['1.0.0 - x', '1.9.7'], + ['1.x - x', '1.9.7'], + ['<=7.x', '7.9.9'] ].forEach(function(v) { var range = v[0]; var ver = v[1]; @@ -299,6 +309,7 @@ test('\nnegative range tests', function(t) { ['<1.2.3', '1.2.3-beta'], ['=1.2.3', '1.2.3-beta'], ['>1.2', '1.2.8'], + ['^0.0.1', '0.0.2'], ['^1.2.3', '2.0.0-alpha'], ['^1.2.3', '1.2.2'], ['^1.2', '1.1.9'], @@ -306,7 +317,8 @@ test('\nnegative range tests', function(t) { // invalid ranges never satisfied! ['blerg', '1.2.3'], ['git+https://user:password0123@github.com/foo', '123.0.0', true], - ['^1.2.3', '2.0.0-pre'] + ['^1.2.3', '2.0.0-pre'], + ['^1.2.3', false] ].forEach(function(v) { var range = v[0]; var ver = v[1]; @@ -664,6 +676,7 @@ test('\nstrict vs loose version numbers', function(t) { t.throws(function() { new SemVer(strict).compare(loose); }); + t.equal(semver.compareLoose(v[0], v[1]), 0); }); t.end(); }); @@ -826,3 +839,54 @@ test('\nmissing range parameter in range intersect', function(t) { 'throws type error'); t.end(); }); + +test('outside with bad hilo throws', function(t) { + t.throws(function() { + semver.outside('1.2.3', '>1.5.0', 'blerg', true) + }, new TypeError('Must provide a hilo val of "<" or ">"')); + t.end() +}) + +test('comparator testing', function(t) { + var c = new Comparator('>=1.2.3'); + t.ok(c.test('1.2.4')); + var c2 = new Comparator(c); + t.ok(c2.test('1.2.4')); + var c3 = new Comparator(c, true); + t.ok(c3.test('1.2.4')); + t.end() +}); + +test('tostrings', function(t) { + t.equal(Range('>= v1.2.3').toString(), '>=1.2.3') + t.equal(Comparator('>= v1.2.3').toString(), '>=1.2.3') + t.end() +}) + +test('invalid cmp usage', function(t) { + t.throws(function() { + cmp('1.2.3', 'a frog', '4.5.6'); + }, new TypeError('Invalid operator: a frog')); + t.end(); +}); + +test('sorting', function(t) { + var list = [ + '1.2.3', + '5.9.6', + '0.1.2' + ]; + var sorted = [ + '0.1.2', + '1.2.3', + '5.9.6' + ]; + var rsorted = [ + '5.9.6', + '1.2.3', + '0.1.2' + ]; + t.same(semver.sort(list), sorted); + t.same(semver.rsort(list), rsorted); + t.end(); +});