Created
December 22, 2011 00:38
-
-
Save sawamur/1508380 to your computer and use it in GitHub Desktop.
JS中級コース向 サンプルコード
This file contains 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
// 実装 : 一箇所バグあり | |
Date.prototype.strftime = (function () { | |
function strftime(format) { | |
var date = this; | |
return (format + "").replace(/%([a-zA-Z])/g, | |
function (m, f) { | |
var formatter = Date.formats && Date.formats[f]; | |
if (typeof formatter == "function") { | |
return formatter.call(Date.formats, date); | |
} else if (typeof formatter == "string") { | |
return date.strftime(formatter); | |
} | |
return f; | |
}); | |
} | |
// Internal helper | |
function zeroPad(num) { | |
return (+num < 10 ? "0" : "") + num; | |
} | |
Date.formats = { | |
// Formatting methods | |
d: function (date) { | |
return zeroPad(date.getDate()); | |
}, | |
m: function (date) { | |
return zeroPad(date.getMonth() + 1); | |
}, | |
y: function (date) { | |
return date.getYear() % 100; | |
}, | |
Y: function (date) { | |
return date.getFullYear(); | |
}, | |
// Format shorthands | |
F: "%Y-%m-%d", | |
D: "%m/%d/%y" | |
}; | |
return strftime; | |
}()); | |
// アサート関数 | |
function assert(message,expr){ | |
if(!expr){ | |
throw new Error(message); | |
} | |
assert.count ++; | |
return true; | |
} | |
assert.count = 0; | |
// 色分けして出力する用 | |
function output(text, color) { | |
var p = document.createElement("p"); | |
p.innerHTML = text; | |
p.style.color = color; | |
document.body.appendChild(p); | |
} | |
// テストスイート | |
function testCase(name, tests) { | |
assert.count = 0; | |
var successful = 0; | |
var testCount = 0; | |
var hasSetup = typeof tests.setUp == "function"; | |
var hasTeardown = typeof tests.tearDown == "function"; | |
for (var test in tests) { | |
if (!/^test/.test(test)) { | |
continue; | |
} | |
testCount++; | |
try { | |
if (hasSetup) { | |
tests.setUp(); | |
} | |
tests[test](); | |
output(test, "#0c0"); | |
if (hasTeardown) { | |
tests.tearDown(); | |
} | |
// If the tearDown method throws an error, it is | |
// considered a test failure, so we don't count | |
// success until all methods have run successfully | |
successful++; | |
} catch (e) { | |
output(test + " failed: " + e.message, "#c00"); | |
} | |
} | |
var color = successful == testCount ? "#0c0" : "#c00"; | |
output("<strong>" + testCount + " tests, " + | |
(testCount - successful) + " failures</strong>", | |
color); | |
} | |
// テストケース | |
testCase("strftime test", { | |
setUp: function () { | |
this.date = new Date(2009, 9, 2, 22, 14, 45); | |
}, | |
"test format specifier %Y": function () { | |
assert("%Y should return full year", | |
this.date.strftime("%Y") === "2009"); | |
}, | |
"test format specifier %m": function () { | |
assert("%m should return month", | |
this.date.strftime("%m") === "10"); | |
//assert_equal("10",this.date.strftime("%m")); | |
//assert_match(/\d{3,10}/,this.date.strftime("%j")); | |
}, | |
"test format specifier %d": function () { | |
assert("%d should return date", | |
this.date.strftime("%d") === "02"); | |
}, | |
"test format specifier %y": function () { | |
assert("%y should return year as two digits", | |
this.date.strftime("%y") === "09"); | |
}, | |
"test format shorthand %F": function () { | |
assert("%F should act as %Y-%m-%d", | |
this.date.strftime("%F") === "2009-10-02"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment