测试驱动javascript开发 -
<div id="cnblogs_post_body"> 本篇我们将通过对Date.strftime编写单元测试的例子,学会断言、测试用例函数的相关知识。首先我们先来看Date.strftime的实现代码。
<div class="cnblogs_code">Date.prototype.strftime = (function () { function strftime(format) { var date = this; return (format + "").replace(/%()/g, function (m, f) { var formatter = Date.formats && Date.formats; if (typeof formatter == "function") { return formatter.call(Date.formats, date); } else if (typeof formatter == "string") { return date.strftime(formatter); } return f; }); } // 内部帮助函数 function zeroPad(num) { return (+num < 10 ? "0" : "") + num; } Date.formats = { // Formatting 方法 d: function (date) { return zeroPad(date.getDate()); }, m: function (date) { return zeroPad(date.getMonth() + 1); }, y: function (date) { return zeroPad(date.getYear() % 100); }, Y: function (date) { return date.getFullYear(); }, // Format 速记方式 F: "%Y-%m-%d", D: "%m/%d/%y" }; return strftime;}());
页:
[1]