Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Last active December 17, 2015 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishiduca/5607210 to your computer and use it in GitHub Desktop.
Save ishiduca/5607210 to your computer and use it in GitHub Desktop.
TDDBC長岡 #1 お題 : Javaの奇妙なバージョン
.
|-- lib/
| `-- version.js
`-- t/
|-- basic_t.js
`-- qunit/
|-- qunit-1.10.0.js # http://code.jquery.com/qunit/qunit-1.10.0.js
`-- qunit-tap.js # https://raw.github.com/twada/qunit-tap/master/lib/qunit-tap.js
(function (BASIC_TEST) {
"use strict";
var path = require('path');
var QUnit = require(path.join( __dirname, 'qunit/qunit-1.10.0'));
var qTap = require(path.join( __dirname, 'qunit/qunit-tap')).qunitTap;
qTap( QUnit, console.log.bind(console) );
QUnit.init();
QUnit.config.updateRate = 0;
[ "test", "asyncTest", "ok", "equal", "notEqual"
, "deepEqual", "notDeepEqual", "strictEqual", "notStrictEqual"
, "throws", "start", "stop"
].forEach(function (keyword) {
global[keyword] = QUnit[keyword];
});
global.is = QUnit.strictEqual;
global.mod = QUnit.module;
var version = require(path.join( __dirname, '../lib/version')).version;
BASIC_TEST(version);
})(function (version) {
"use strict";
mod('00: exists "version"');
test('exists "version"', function () {
ok(version);
});
mod('01: validかどうか調べる。version.isValid(str)');
test('case "JDK7u40"', function () {
ok( version.isValid("JDK7u40") );
});
test('case "hoge"', function () {
ok( ! version.isValid("hoge") );
});
test('case "JDK7u9x"', function () {
ok( ! version.isValid("JDK7u9x") );
});
test('case ""', function () {
ok( ! version.isValid("") );
});
test('case {foo: "bar"}', function () {
ok( ! version.isValid({foo: "bar"}) );
});
test('case null', function () {
ok( ! version.isValid(null) );
});
test('case undefined', function () {
ok( ! version.isValid() );
});
mod('02: parseしよう。version.parse(str)');
test('case "JDK7u40"', function () {
var v = version.parse("JDK7u40");
ok(v);
is( v.familyNumber, 7 );
is( v.updateNumber, 40);
});
test('case "JDK12u0"', function () {
var v = version.parse("JDK12u0");
ok(v);
is( v.familyNumber, 12 );
is( v.updateNumber, 0);
});
test('case ""', function () {
throws(
function () { version.parse("") }
, /can not parse/
);
});
test('case {foo: "bar"}', function () {
throws(
function () { version.parse({foo: "bar"}) }
, /can not parse/
);
});
test('case null', function () {
throws(
function () { version.parse(null) }
, /can not parse/
);
});
test('case undefined', function () {
throws(
function () { version.parse() }
, /can not parse/
);
});
mod('*: ._numbering()');
test('u40._numbering()', function () {
var us = ('7u40 8u0 9u9 10u1').split(' ').map(function (p) {
return version.parse("JDK" + p);
});
var res = [ 740, 800, 909, 1001 ];
us.forEach(function (jdkNuNN, i) {
is( jdkNuNN._numbering(), res[i] );
});
});
mod('03: 大小比較しよう v.gt(versionObject) or v.lt(versionObject)', {
setup: function () { this.u40 = version.parse('JDK7u40'); }
});
test('exists "u40"', function () {
var u40 = this.u40;
ok(u40);
is(u40.familyNumber, 7);
is(u40.updateNumber, 40);
});
test('u40.gt()', function () {
var u40 = this.u40;
throws(
function () { u40.gt() }
, /not "version object"/
);
});
test('u40.gt(null)', function () {
var u40 = this.u40;
throws(
function () { u40.gt(null) }
, /not "version object"/
);
});
test('u40.gt({familyNumber: 8, updateNumber: 12})', function () {
var u40 = this.u40;
throws(
function () { u40.gt({familyNumber: 8, updateNumber: 12}) }
, /not "version object"/
);
});
test('u40.lt(version.parse("JDK7u51"))', function () {
var u51 = version.parse("JDK7u51");
ok(this.u40.lt(u51));
ok(! this.u40.gt(u51));
});
test('! u40.lt(version.parse("JDK8u0"))', function () {
var jdk8u0 = version.parse("JDK8u0");
ok(this.u40.lt(jdk8u0));
ok(jdk8u0.gt(version.parse("JDK7u51")));
});
mod('04: 次の番号を計算しよう', {
setup: function () {
this.u45 = version.parse("JDK7u45");
this.update = function (next, updateNumber) {
ok(next);
is(next.updateNumber, updateNumber);
return next;
};
}
});
test('u60 = u45.nextLimitedUpdate()', function () {
var u60 = this.update(this.u45.nextLimitedUpdate(), 60);
});
test('u51 = u45.nextCriticalPatchUpdate()', function () {
var u51 = this.update(this.u45.nextCriticalPatchUpdate(), 51);
var u55 = this.update(u51.nextCriticalPatchUpdate(), 55);
var u61 = this.update(u55.nextCriticalPatchUpdate(), 61);
});
test('u5 = jdk7u[0~4].nextCriticalPatchUpdate()', function () {
('0 1 2 3 4').split(' ').map(function (nn) {
return version.parse('JDK7u' + nn);
}).forEach(function (jdk7uN) {
var u5 = jdk7uN.nextCriticalPatchUpdate();
ok(u5);
is(u5.updateNumber, 5);
});
});
test('u11 = jdk7u[5~9].nextCriticalPatchUpdate()', function () {
('5 6 7 8 9').split(' ').map(function (nn) {
return version.parse('JDK7u' + nn);
}).forEach(function (jdk7uN) {
var u11 = jdk7uN.nextCriticalPatchUpdate();
ok(u11);
is(u11.updateNumber, 11);
});
});
test('u46 = u45.nextSecurityAlert()', function () {
var u46 = this.update(this.u45.nextSecurityAlert(), 46);
});
test('version.parse("JDK7u40").nextSecurityAlert()', function () {
var update = this.update;
var least = version.parse("JDK7u40");
[ 41, 42, 43, 44, 46, 47, 48, 49, 50, 52, 53, 54, 56, 57, 58, 59 ]
.forEach(function (ver) {
least = update(least.nextSecurityAlert(), ver);
});
});
test('version.parse("JDK8u0").nextSecurityAlert()', function () {
var update = this.update;
var least = version.parse("JDK8u0");
[ 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19 ]
.forEach(function (ver) {
least = update(least.nextSecurityAlert(), ver);
});
});
});
"use strict";
var version = {
isPattern: /^JDK(\d+)u(\d+)$/
};
var _valid = function (s) {
return (typeof s !== 'string') ? null : s.match( version.isPattern );
};
version.isValid = function (val) { return !! _valid( val ) };
version.parse = function (val) {
var res = _valid( val );
if (! res) throw new Error('Invalid Value - can not parse');
return new Version(res[1], res[2]);
};
function Version (familyNumber, updateNumber) {
this.familyNumber = Number(familyNumber);
this.updateNumber = Number(updateNumber);
}
var vp = Version.prototype;
vp._numbering = function () {
return (this.familyNumber * 100) + this.updateNumber;
};
var operators = {
gt: function (num) { return num > 0 }
, lt: function (num) { return num < 0 }
// , ge: function (num) { return num >= 0 }
// , le: function (num) { return num <= 0 }
// , eq: function (num) { return num === 0 }
// , ne: function (num) { return num !== 0 }
};
Object.keys(operators).forEach(function (key) {
vp[key] = function (another) {
if (typeof another !== 'object' ||
another === null ||
another.constructor !== this.constructor
) {
throw new TypeError('1st argument is not "version object"');
}
return operators[key](this._numbering() - another._numbering());
};
});
var nexts = {
nextLimitedUpdate: function (n) {
return (Math.floor( n / 20 ) + 1) * 20;
}
, nextCriticalPatchUpdate: function (n) {
n = (Math.floor( n / 5 ) + 1) * 5;
return n % 2 == 0 ? n + 1 : n;
}
, nextSecurityAlert: function (n) {
var helper = function (n) {
return [ 0, 5, 11, 15 ].some(function (ng) {
return (n % 20) === ng;
}) ? helper(n + 1) : n;
};
return helper(n + 1);
}
};
Object.keys(nexts).forEach(function (update) {
vp[update] = function () {
var that = this;
return new Version(
that.familyNumber
, nexts[update](that.updateNumber)
);
};
});
module.exports.version = version;
@ishiduca
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment