Skip to content

Commit

Permalink
many tests and slight refactoring to boost coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Sep 14, 2016
1 parent aee1770 commit 918f472
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 16 deletions.
25 changes: 18 additions & 7 deletions semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@ exports = module.exports = SemVer;

// The debug function is excluded entirely from the minified version.
/* nomin */ var debug;
/* istanbul ignore next */
/* nomin */ if (typeof process === 'object' &&
/* istanbul ignore next */
/* nomin */ process.env &&
/* istanbul ignore next */
/* nomin */ process.env.NODE_DEBUG &&
/* istanbul ignore next */
/* nomin */ /\bsemver\b/i.test(process.env.NODE_DEBUG))
/* istanbul ignore next */
/* nomin */ debug = function() {
/* istanbul ignore next */
/* nomin */ var args = Array.prototype.slice.call(arguments, 0);
/* istanbul ignore next */
/* nomin */ args.unshift('SEMVER');
/* istanbul ignore next */
/* nomin */ console.log.apply(console, args);
/* istanbul ignore next */
/* nomin */ };
/* istanbul ignore next */
/* nomin */ else
/* istanbul ignore next */
/* nomin */ debug = function() {};

// Note: this is the semver.org version of the spec that it implements
// Not necessarily the package version of this code.
exports.SEMVER_SPEC_VERSION = '2.0.0';

var MAX_LENGTH = 256;
/* istanbul ignore next */
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;

// The actual regexps go on exports.re
Expand Down Expand Up @@ -840,8 +852,7 @@ function replaceTilde(comp, loose) {
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
else if (pr) {
debug('replaceTilde pr', pr);
if (pr.charAt(0) !== '-')
pr = '-' + pr;
pr = '-' + pr;
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + M + '.' + (+m + 1) + '.0';
} else
Expand Down Expand Up @@ -884,8 +895,7 @@ function replaceCaret(comp, loose) {
ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.0.0';
} else if (pr) {
debug('replaceCaret pr', pr);
if (pr.charAt(0) !== '-')
pr = '-' + pr;
pr = '-' + pr;
if (M === '0') {
if (m === '0')
ret = '>=' + M + '.' + m + '.' + p + pr +
Expand Down Expand Up @@ -947,8 +957,9 @@ function replaceXRange(comp, loose) {
// replace X with 0
if (xm)
m = 0;
if (xp)
p = 0;

// p is always an x if we get here.
p = 0;

if (gtlt === '>') {
// >1 => >=2.0.0
Expand All @@ -959,7 +970,7 @@ function replaceXRange(comp, loose) {
M = +M + 1;
m = 0;
p = 0;
} else if (xp) {
} else {
m = +m + 1;
p = 0;
}
Expand Down
29 changes: 22 additions & 7 deletions test/big-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,34 @@ test('long version is too long', function (t) {
})

test('big number is like too long version', function (t) {
var v = '1.2.' + new Array(100).join('1')
t.throws(function () {
new semver.SemVer(v)
var big = new Array(100).join('1')

var versions = [
'1.2.' + big,
'1.' + big + '.2',
big + '.2.3',
'-1.2.3',
'1.-2.3',
'1.2.-3'
]
versions.forEach(function (v) {
t.throws(function () {
new semver.SemVer(v)
})
t.equal(semver.valid(v, false), null)
t.equal(semver.valid(v, true), null)
t.equal(semver.inc(v, 'patch'), null)
})
t.equal(semver.valid(v, false), null)
t.equal(semver.valid(v, true), null)
t.equal(semver.inc(v, 'patch'), null)
t.isa(semver.parse('1.2.3-' + big).prerelease[0], 'string')
t.isa(semver.parse('1.2.3-5').prerelease[0], 'number')
t.end()
})

test('parsing null does not throw', function (t) {
t.equal(semver.parse(null), null)
t.equal(semver.parse({}), null)
t.equal(semver.parse(new semver.SemVer('1.2.3')).version, '1.2.3')
t.equal(semver.valid(null), null)
t.equal(semver.valid(semver.parse('1.2.3')), '1.2.3')
t.equal(semver.parse(semver.SemVer('1.2.3')).version, '1.2.3')
t.end()
})
5 changes: 5 additions & 0 deletions test/gtr.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,8 @@ test('\nnegative gtr tests', function(t) {
});
t.end();
});

test('\noutside should throw if hilo is not set', function (t) {
t.throws(semver.outside.bind(semver, '1.2.3', '*', 'x'))
t.end()
})
41 changes: 39 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,31 @@ test('\nrange tests', function(t) {
[['1.0.0 - 2.0.0', '1.2.3'],
['^1.2.3+build', '1.2.3'],
['^1.2.3+build', '1.3.0'],
['^1.2.x', '1.3.0'],
['^1.x', '1.3.0'],
['^x', '1.3.0'],
['1.2.3-pre+asdf - 2.4.3-pre+asdf', '1.2.3'],
['1.2.3pre+asdf - 2.4.3-pre+asdf', '1.2.3', true],
['1.2.3-pre+asdf - 2.4.3pre+asdf', '1.2.3', true],
['1.2.3pre+asdf - 2.4.3pre+asdf', '1.2.3', true],
['1.2.3-pre+asdf - 2.4.3-pre+asdf', '1.2.3-pre.2'],
['1.2.3-pre+asdf - 2.4.3-pre+asdf', '2.4.3-alpha'],
['1.2.3+asdf - 2.4.3+asdf', '1.2.3'],
['1.2.x - 2.4.3', '1.2.3'],
['1.x - 2.4.3', '1.2.3'],
['x - 2.4.3', '1.2.3'],
['1.2.3 - 2.4.x', '1.2.3'],
['1.2.3 - 2.x', '1.2.3'],
['1.2.3 - x', '1.2.3'],
['1.0.0', '1.0.0'],
['>=*', '0.2.4'],
['', '1.0.0'],
['*', '1.2.3'],
['x', '1.2.3'],
['X', '1.2.3'],
['*', 'v1.2.3', true],
['x', '1.2.3', true],
['X', '1.2.3', true],
['>=1.0.0', '1.0.0'],
['>=1.0.0', '1.0.1'],
['>=1.0.0', '1.1.0'],
Expand Down Expand Up @@ -191,6 +204,7 @@ test('\nrange tests', function(t) {
['~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,
['~x', '1.2.3'], // >=1.0.0 <2.0.0
['~1', '1.2.3'], // >=1.0.0 <2.0.0
['~>1', '1.2.3'],
['~> 1', '1.2.3'],
Expand All @@ -204,8 +218,15 @@ test('\nrange tests', function(t) {
['~v0.5.4-pre', '0.5.5'],
['~v0.5.4-pre', '0.5.4'],
['=0.7.x', '0.7.2'],
['=2.7.x', '2.7.2'],
['<=0.7.x', '0.7.2'],
['<=7.x', '7.2.5'],
['<=7.2.x', '7.2.5'],
['>=0.7.x', '0.7.2'],
['>=7.x', '7.2.5'],
['>=7.2.x', '7.2.5'],
['>7.x', '8.2.5'],
['>7.2.x', '7.3.5'],
['<=0.7.x', '0.6.2'],
['~1.2.1 >=1.2.3', '1.2.3'],
['~1.2.1 =1.2.3', '1.2.3'],
Expand All @@ -219,13 +240,27 @@ test('\nrange tests', function(t) {
['>=1.2.1 >=1.2.3', '1.2.3'],
['>=1.2', '1.2.8'],
['^1.2.3', '1.8.1'],
['^0.0.2', '0.0.2'],
['^0.1.2', '0.1.2'],
['^0.1', '0.1.2'],
['^0', '0.1.2'],
['^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.1.2-alpha', '0.1.2-beta'],
['^0.1.2-alpha', '0.1.3'],
['^0.0.1-alpha', '0.0.1-beta'],
['^1.2.3--alpha', '1.2.3-pre'],
['^1.2.0--alpha', '1.2.0-pre'],
['^0.1.2--alpha', '0.1.2-beta'],
['^0.1.2--alpha', '0.1.3'],
['^0.0.1--alpha', '0.0.1-beta'],
['^1.2.3alpha', '1.2.3-pre', true],
['^1.2.0alpha', '1.2.0-pre', true],
['^0.1.2alpha', '0.1.2-beta', true],
['^0.1.2alpha', '0.1.3', true],
['^0.0.1alpha', '0.0.1-beta', true]
].forEach(function(v) {
var range = v[0];
var ver = v[1];
Expand Down Expand Up @@ -305,7 +340,9 @@ 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'],
// invalid ranges always fail a range test
['*', false]
].forEach(function(v) {
var range = v[0];
var ver = v[1];
Expand Down

0 comments on commit 918f472

Please sign in to comment.