Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created February 24, 2013 01:33
Show Gist options
  • Save ishiduca/5022206 to your computer and use it in GitHub Desktop.
Save ishiduca/5022206 to your computer and use it in GitHub Desktop.
phantomjs + Qunit + qunit-tap でのテストサンプル
/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/
<!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>
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();
});
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);
@ishiduca
Copy link
Author

run-qunit.js はデフォルトだと 3min でタイムアウトなので、

    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timout is 3s

var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 4001,

にしておく。
んで、

$ phantomjs run-qunit.js indeishiduca-no-MacBook:t ishiduca$ phantomjs run-qunit.js index.html 

すると、

# test: set up test
ok 1 - not set arguments - undefiend
ok 2 - typeof argument is "String"
ok 3 - typeof argument is "-1"
ok 4 - "new Timer(10)" is exsits and is not undefined
# test: toString()
ok 5 - 00:00:10
ok 6 - 00:00:59
ok 7 - 00:01:00
ok 8 - 00:01:01
ok 9 - 00:59:59
ok 10 - 01:00:00
ok 11 - 01:00:01
ok 12 - 00:00:00
# test: resume
ok 13 - 3 === timer.time[2]
ok 14 - 2 === timer.time[2]
ok 15 - 1 === timer.time[2]
ok 16 - 0 === timer.time[2]
ok 17 - onend - timer.time[2]: 0
ok 18 - true === timer.ended
ok 19 - "undefined" === typeof timer.intervalID
1..19
'waitFor()' finished in 3206ms.
Tests completed in 3037 milliseconds.
19 tests of 19 passed, 0 failed.

@ishiduca
Copy link
Author

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