Skip to content

Commit

Permalink
semver class test coverage to 100
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Nov 22, 2019
1 parent b4b40cd commit 166acc8
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 129 deletions.
13 changes: 12 additions & 1 deletion classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class SemVer {
}
}
if (version instanceof SemVer) {
if (version.loose === options.loose) {
if (version.loose === !!options.loose &&
version.includePrerelease === !!options.includePrerelease) {
return version
} else {
version = version.version
Expand All @@ -30,6 +31,9 @@ class SemVer {
debug('SemVer', version, options)
this.options = options
this.loose = !!options.loose
// this isn't actually relevant for versions, but keep it so that we
// don't run into trouble passing this.options around.
this.includePrerelease = !!options.includePrerelease

const m = version.trim().match(options.loose ? re[t.LOOSE] : re[t.FULL])

Expand Down Expand Up @@ -90,9 +94,16 @@ class SemVer {
compare (other) {
debug('SemVer.compare', this.version, this.options, other)
if (!(other instanceof SemVer)) {
if (typeof other === 'string' && other === this.version) {
return 0
}
other = new SemVer(other, this.options)
}

if (other.version === this.version) {
return 0
}

return this.compareMain(other) || this.comparePre(other)
}

Expand Down
10 changes: 5 additions & 5 deletions functions/inc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const SemVer = require('../classes/semver')

const inc = (version, release, loose, identifier) => {
if (typeof (loose) === 'string') {
identifier = loose
loose = undefined
const inc = (version, release, options, identifier) => {
if (typeof (options) === 'string') {
identifier = options
options = undefined
}

try {
return new SemVer(version, loose).inc(release, identifier).version
return new SemVer(version, options).inc(release, identifier).version
} catch (er) {
return null
}
Expand Down
88 changes: 88 additions & 0 deletions test/classes/semver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
const { test } = require('tap')
const SemVer = require('../../classes/semver')
const increments = require('../fixtures/increments.js')
const comparisons = require('../fixtures/comparisons.js')
const equality = require('../fixtures/equality.js')
const invalidVersions = require('../fixtures/invalid-versions')

test('comparisons', t => {
t.plan(comparisons.length)
comparisons.forEach(([v0, v1, opt]) => t.test(`${v0} ${v1}`, t => {
const s0 = new SemVer(v0, opt)
const s1 = new SemVer(v1, opt)
t.equal(s0.compare(s1), 1)
t.equal(s0.compare(v1), 1)
t.equal(s1.compare(s0), -1)
t.equal(s1.compare(v0), -1)
t.equal(s0.compare(v0), 0)
t.equal(s1.compare(v1), 0)
t.end()
}))
})

test('equality', t => {
t.plan(equality.length)
equality.forEach(([v0, v1, loose]) => t.test(`${v0} ${v1} ${loose}`, t => {
const s0 = new SemVer(v0, loose)
const s1 = new SemVer(v1, loose)
t.equal(s0.compare(s1), 0)
t.equal(s1.compare(s0), 0)
t.equal(s0.compare(v1), 0)
t.equal(s1.compare(v0), 0)
t.equal(s0.compare(s0), 0)
t.equal(s1.compare(s1), 0)
t.equal(s0.comparePre(s1), 0, 'comparePre just to hit that code path')
t.end()
}))
})

test('toString equals parsed version', t => {
t.equal(String(new SemVer('v1.2.3')), '1.2.3')
t.end()
})

test('throws when presented with garbage', t => {
t.plan(invalidVersions.length)
invalidVersions.forEach(([v, msg, opts]) =>
t.throws(() => new SemVer(v, opts), msg))
})

test('return SemVer arg to ctor if options match', t => {
const s = new SemVer('1.2.3', { loose: true, includePrerelease: true })
t.equal(new SemVer(s, {loose: true, includePrerelease: true}), s,
'get same object when options match')
t.notEqual(new SemVer(s), s, 'get new object when options match')
t.end()
})

test('really big numeric prerelease value', (t) => {
const r = new SemVer(`1.2.3-beta.${Number.MAX_SAFE_INTEGER}0`)
Expand All @@ -22,6 +76,23 @@ test('invalid version numbers', (t) => {
t.end()
})

test('incrementing', t => {
t.plan(increments.length)
increments.forEach(([
version,
inc,
expect,
options,
id,
]) => t.test(`${version} ${inc} ${id || ''}`.trim(), t => {
t.plan(1)
if (expect === null)
t.throws(() => new SemVer(version, options).inc(inc, id))
else
t.equal(new SemVer(version, options).inc(inc, id).version, expect)
}))
})

test('compare main vs pre', (t) => {
const s = new SemVer('1.2.3')
t.equal(s.compareMain('2.3.4'), -1)
Expand Down Expand Up @@ -53,3 +124,20 @@ test('invalid version numbers', (t) => {

t.end()
})

test('compareBuild', (t) => {
const noBuild = new SemVer('1.0.0')
const build0 = new SemVer('1.0.0+0')
const build1 = new SemVer('1.0.0+1')
const build10 = new SemVer('1.0.0+1.0')
t.equal(noBuild.compareBuild(build0), -1)
t.equal(build0.compareBuild(build0), 0)
t.equal(build0.compareBuild(noBuild), 1)

t.equal(build0.compareBuild('1.0.0+0.0'), -1)
t.equal(build0.compareBuild(build1), -1)
t.equal(build1.compareBuild(build0), 1)
t.equal(build10.compareBuild(build1), 1)

t.end()
})
85 changes: 85 additions & 0 deletions test/fixtures/increments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// [version, inc, result, options, identifier]
// inc(version, inc) -> result
module.exports = [
['1.2.3', 'major', '2.0.0'],
['1.2.3', 'minor', '1.3.0'],
['1.2.3', 'patch', '1.2.4'],
['1.2.3tag', 'major', '2.0.0', true],
['1.2.3-tag', 'major', '2.0.0'],
['1.2.3', 'fake', null],
['1.2.0-0', 'patch', '1.2.0'],
['fake', 'major', null],
['1.2.3-4', 'major', '2.0.0'],
['1.2.3-4', 'minor', '1.3.0'],
['1.2.3-4', 'patch', '1.2.3'],
['1.2.3-alpha.0.beta', 'major', '2.0.0'],
['1.2.3-alpha.0.beta', 'minor', '1.3.0'],
['1.2.3-alpha.0.beta', 'patch', '1.2.3'],
['1.2.4', 'prerelease', '1.2.5-0'],
['1.2.3-0', 'prerelease', '1.2.3-1'],
['1.2.3-alpha.0', 'prerelease', '1.2.3-alpha.1'],
['1.2.3-alpha.1', 'prerelease', '1.2.3-alpha.2'],
['1.2.3-alpha.2', 'prerelease', '1.2.3-alpha.3'],
['1.2.3-alpha.0.beta', 'prerelease', '1.2.3-alpha.1.beta'],
['1.2.3-alpha.1.beta', 'prerelease', '1.2.3-alpha.2.beta'],
['1.2.3-alpha.2.beta', 'prerelease', '1.2.3-alpha.3.beta'],
['1.2.3-alpha.10.0.beta', 'prerelease', '1.2.3-alpha.10.1.beta'],
['1.2.3-alpha.10.1.beta', 'prerelease', '1.2.3-alpha.10.2.beta'],
['1.2.3-alpha.10.2.beta', 'prerelease', '1.2.3-alpha.10.3.beta'],
['1.2.3-alpha.10.beta.0', 'prerelease', '1.2.3-alpha.10.beta.1'],
['1.2.3-alpha.10.beta.1', 'prerelease', '1.2.3-alpha.10.beta.2'],
['1.2.3-alpha.10.beta.2', 'prerelease', '1.2.3-alpha.10.beta.3'],
['1.2.3-alpha.9.beta', 'prerelease', '1.2.3-alpha.10.beta'],
['1.2.3-alpha.10.beta', 'prerelease', '1.2.3-alpha.11.beta'],
['1.2.3-alpha.11.beta', 'prerelease', '1.2.3-alpha.12.beta'],
['1.2.0', 'prepatch', '1.2.1-0'],
['1.2.0-1', 'prepatch', '1.2.1-0'],
['1.2.0', 'preminor', '1.3.0-0'],
['1.2.3-1', 'preminor', '1.3.0-0'],
['1.2.0', 'premajor', '2.0.0-0'],
['1.2.3-1', 'premajor', '2.0.0-0'],
['1.2.0-1', 'minor', '1.2.0'],
['1.0.0-1', 'major', '1.0.0'],

['1.2.3', 'major', '2.0.0', false, 'dev'],
['1.2.3', 'minor', '1.3.0', false, 'dev'],
['1.2.3', 'patch', '1.2.4', false, 'dev'],
['1.2.3tag', 'major', '2.0.0', true, 'dev'],
['1.2.3-tag', 'major', '2.0.0', false, 'dev'],
['1.2.3', 'fake', null, false, 'dev'],
['1.2.0-0', 'patch', '1.2.0', false, 'dev'],
['fake', 'major', null, false, 'dev'],
['1.2.3-4', 'major', '2.0.0', false, 'dev'],
['1.2.3-4', 'minor', '1.3.0', false, 'dev'],
['1.2.3-4', 'patch', '1.2.3', false, 'dev'],
['1.2.3-alpha.0.beta', 'major', '2.0.0', false, 'dev'],
['1.2.3-alpha.0.beta', 'minor', '1.3.0', false, 'dev'],
['1.2.3-alpha.0.beta', 'patch', '1.2.3', false, 'dev'],
['1.2.4', 'prerelease', '1.2.5-dev.0', false, 'dev'],
['1.2.3-0', 'prerelease', '1.2.3-dev.0', false, 'dev'],
['1.2.3-alpha.0', 'prerelease', '1.2.3-dev.0', false, 'dev'],
['1.2.3-alpha.0', 'prerelease', '1.2.3-alpha.1', false, 'alpha'],
['1.2.3-alpha.0.beta', 'prerelease', '1.2.3-dev.0', false, 'dev'],
['1.2.3-alpha.0.beta', 'prerelease', '1.2.3-alpha.1.beta', false, 'alpha'],
['1.2.3-alpha.10.0.beta', 'prerelease', '1.2.3-dev.0', false, 'dev'],
['1.2.3-alpha.10.0.beta', 'prerelease', '1.2.3-alpha.10.1.beta', false, 'alpha'],
['1.2.3-alpha.10.1.beta', 'prerelease', '1.2.3-alpha.10.2.beta', false, 'alpha'],
['1.2.3-alpha.10.2.beta', 'prerelease', '1.2.3-alpha.10.3.beta', false, 'alpha'],
['1.2.3-alpha.10.beta.0', 'prerelease', '1.2.3-dev.0', false, 'dev'],
['1.2.3-alpha.10.beta.0', 'prerelease', '1.2.3-alpha.10.beta.1', false, 'alpha'],
['1.2.3-alpha.10.beta.1', 'prerelease', '1.2.3-alpha.10.beta.2', false, 'alpha'],
['1.2.3-alpha.10.beta.2', 'prerelease', '1.2.3-alpha.10.beta.3', false, 'alpha'],
['1.2.3-alpha.9.beta', 'prerelease', '1.2.3-dev.0', false, 'dev'],
['1.2.3-alpha.9.beta', 'prerelease', '1.2.3-alpha.10.beta', false, 'alpha'],
['1.2.3-alpha.10.beta', 'prerelease', '1.2.3-alpha.11.beta', false, 'alpha'],
['1.2.3-alpha.11.beta', 'prerelease', '1.2.3-alpha.12.beta', false, 'alpha'],
['1.2.0', 'prepatch', '1.2.1-dev.0', false, 'dev'],
['1.2.0-1', 'prepatch', '1.2.1-dev.0', false, 'dev'],
['1.2.0', 'preminor', '1.3.0-dev.0', false, 'dev'],
['1.2.3-1', 'preminor', '1.3.0-dev.0', false, 'dev'],
['1.2.0', 'premajor', '2.0.0-dev.0', false, 'dev'],
['1.2.3-1', 'premajor', '2.0.0-dev.0', false, 'dev'],
['1.2.0-1', 'minor', '1.2.0', false, 'dev'],
['1.0.0-1', 'major', '1.0.0', 'dev'],
['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.0', false, 'dev'],
]
15 changes: 15 additions & 0 deletions test/fixtures/invalid-versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// none of these are semvers
// [value, reason, opt]
const {MAX_LENGTH, MAX_SAFE_INTEGER} = require('../../internal/constants')
module.exports = [
[ new Array(MAX_LENGTH).join('1') + '.0.0', 'too long'],
[ `${MAX_SAFE_INTEGER}0.0.0`, 'too big'],
[ `0.${MAX_SAFE_INTEGER}0.0`, 'too big'],
[ `0.0.${MAX_SAFE_INTEGER}0`, 'too big'],
[ 'hello, world', 'not a version'],
[ 'hello, world', true, 'even loose, its still junk'],
[ 'xyz', 'even loose as an opt, same', , { loose: true }],
[ /a regexp/, 'regexp is not a string'],
[ /1.2.3/, 'semver-ish regexp is not a string'],
[ {toString: () => '1.2.3'}, 'obj with a tostring is not a string'],
]
23 changes: 12 additions & 11 deletions test/functions/compare-build.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const { test } = require('tap')
const SemVer = require('../../classes/semver')
const compareBuild = require('../../functions/compare-build')

test('compareBuild', (t) => {
const noBuild = new SemVer('1.0.0')
const build0 = new SemVer('1.0.0+0')
const build1 = new SemVer('1.0.0+1')
const build10 = new SemVer('1.0.0+1.0')
t.equal(noBuild.compareBuild(build0), -1)
t.equal(build0.compareBuild(build0), 0)
t.equal(build0.compareBuild(noBuild), 1)
const noBuild = '1.0.0'
const build0 = '1.0.0+0'
const build1 = '1.0.0+1'
const build10 = '1.0.0+1.0'
t.equal(compareBuild(noBuild, build0), -1)
t.equal(compareBuild(build0, build0), 0)
t.equal(compareBuild(build0, noBuild), 1)

t.equal(build0.compareBuild('1.0.0+0.0'), -1)
t.equal(build0.compareBuild(build1), -1)
t.equal(build1.compareBuild(build0), 1)
t.equal(build10.compareBuild(build1), 1)
t.equal(compareBuild(build0, '1.0.0+0.0'), -1)
t.equal(compareBuild(build0, build1), -1)
t.equal(compareBuild(build1, build0), 1)
t.equal(compareBuild(build10, build1), 1)

t.end()
})
Loading

0 comments on commit 166acc8

Please sign in to comment.