Created
February 24, 2013 01:33
-
-
Save ishiduca/5022206 to your computer and use it in GitHub Desktop.
phantomjs + Qunit + qunit-tap でのテストサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/Project/ | |
-- /lib/ | |
-- timer.js | |
-- /t/ | |
-- index.html | |
-- run-qunit.js (https://raw.github.com/ariya/phantomjs/master/examples/run-qunit.js) | |
-- test.js | |
-- /node_modules/ | |
-- /qunit-tap/ | |
-- /qunitjs/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<head> | |
<meta charset="utf-8" /> | |
<link rel="stylesheet" href="../node_modules/qunitjs/qunit/qunit.css" /> | |
<script src="../lib/timer.js"></script><!-- テストされる対象 --> | |
<script src="../node_modules/qunitjs/qunit/qunit.js"></script> | |
<script src="../node_modules/qunit-tap/lib/qunit-tap.js"></script> | |
<script> | |
qunitTap(QUnit, function () { console.log.apply(console, arguments) }, {showSourseOnFailure: true}); | |
</script> | |
<script src="./test.js"></script><!-- テスト --> | |
<title>test timer.js</title> | |
</head> | |
<body> | |
<div id="qunit"></div> | |
<div id="qunit-fixture"></div> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('set up test', function () { | |
throws( | |
function () { new Timer } | |
, TypeError // Error Class or RegExp | |
, 'not set arguments - undefiend' | |
); | |
throws( | |
function () { new Timer('100') } | |
, TypeError | |
, 'typeof argument is "String"' | |
); | |
throws( | |
function () { new Timer(-1) } | |
, TypeError | |
, 'argument "-1" is under "0"' | |
); | |
ok( new Timer(10) | |
, '"new Timer(10)" is exsits and is not undefined' ); | |
}); | |
test('toString()', function () { | |
equal((new Timer(10)).toString(), '00:00:10', '00:00:10'); | |
equal((new Timer(59)).toString(), '00:00:59', '00:00:59'); | |
equal((new Timer(60)).toString(), '00:01:00', '00:01:00'); | |
equal((new Timer(61)).toString(), '00:01:01', '00:01:01'); | |
equal((new Timer(3599)).toString(), '00:59:59', '00:59:59'); | |
equal((new Timer(3600)).toString(), '01:00:00', '01:00:00'); | |
equal((new Timer(3601)).toString(), '01:00:01', '01:00:01'); | |
var timer = new Timer(0); | |
equal(timer.toString(), '00:00:00', '00:00:00'); | |
}); | |
asyncTest('resume', function () { | |
var count = 3; | |
var timer; try { | |
timer = new Timer(count); | |
} catch (err) { | |
console.error(err); | |
return; | |
} | |
timer.on('dec', function (hour, min, sec) { | |
equal(count, sec, count + ' === timer.time[2]'); | |
count--; | |
}); | |
timer.on('end', function () { | |
equal(timer.time[2], 0, "onend - timer.time[2]: 0"); | |
ok(true === timer.ended, "true === timer.ended"); | |
ok("undefined" === typeof timer.intervalID | |
, '"undefined" === typeof timer.intervalID'); | |
start(); | |
}); | |
timer.resume(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Timer (second) { | |
if ('number' !== typeof second || second < 0) | |
throw new TypeError('"second" must be "Number"'); | |
var hour = Math.floor(second / 3600); second -= hour * 3600; | |
var min = Math.floor(second / 60); second -= min * 60; | |
this.ended = false; | |
this.time = [ hour, min, second ]; | |
this.events = {}; | |
var that = this; | |
this.on('close', function () { that.emit('end') }); | |
} | |
(function (tp) { | |
tp.toString = function () { | |
return this.time.map(function (num) { | |
if (num < 10) num = '0' + num; | |
return num.toString(); | |
}).join(':'); | |
}; | |
tp.resume = function () { | |
if (this.ended) return; | |
if (this.intervalID) return; | |
var that = this; | |
var countdown = function () { | |
that.emit('dec', that.time[0], that.time[1], that.time[2]); | |
if (that.time[2] === 0 && | |
that.time[1] === 0 && | |
that.time[0] === 0 | |
) { | |
return that.destroy(); | |
} | |
if (0 === that.time[2]) { | |
if (0 === that.time[1]) { | |
--that.time[0]; | |
that.time[1] = 60; | |
} | |
--that.time[1]; | |
that.time[2] = 60; | |
} | |
--that.time[2]; | |
}; | |
this.intervalID = setInterval(countdown, 1000); | |
countdown(); | |
}; | |
tp.pause = function () { | |
if (this.ended) return; | |
clearInterval(this.intervalID); | |
delete this.intervalID; | |
}; | |
tp.destroy = function () { | |
this.ended = true; | |
clearInterval(this.intervalID); | |
delete this.intervalID; | |
this.emit('close'); | |
}; | |
tp.emit = function () { | |
var args = Array.prototype.slice.apply(arguments); | |
var name = args.shift(); | |
var ev = this.events[name]; | |
if (! ev) return; | |
for (var i = 0, len = ev.length; i < len; i++) { | |
ev[i].apply(null, args); | |
} | |
}; | |
tp.on = function (name, cb) { | |
if ('function' !== typeof cb) return; | |
if (! this.events[name]) this.events[name] = []; | |
this.events[name].push(cb); | |
}; | |
})(Timer.prototype); |
prove から使いたい場合は
t/test.sh
#!/bin/sh
URL=file://$PWD/index.html
phantomjs run-qunit.js $URL
$ prove test.sh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run-qunit.js はデフォルトだと 3min でタイムアウトなので、
を
にしておく。
んで、
すると、