JavaScript 适用于JavaScript的简单BDD(行为驱动开发)库。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 适用于JavaScript的简单BDD(行为驱动开发)库。相关的知识,希望对你有一定的参考价值。
/*
* A Simple BDD (Behaviour Driven Development) library for JavaScript.
*
* Copyright (c) 2008 Takanori Ishikawa <takanori.ishikawa@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the authors nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* A Tiny JavaScript liberary for Behaviour-Driven Development (BDD).
*
* Author: Takanori Ishikawa <takanori.ishikawa@gmail.com>
* Copyright: Takanori Ishikawa 2008
* License: BSD License (see above)
*
* Supported Browsers:
* [Win] IE 6, Firefox 2
* [Mac] Safari 3, Firefox 2
*
* Usage:
* Spec.describe("Spec.should", function() {
* "simple values": function() {
* "should": function() {
* Spec.should(true);
* Spec.should(1);
* },
* ...
* },
* ...
* });
*
* See Also:
* Behavior Driven Development - Wikipedia, the free encyclopedia
* http://en.wikipedia.org/wiki/Behavior_driven_development
*
*/
/**
* Spec is the BDD style test utilities.
*/
var Spec = {
/** Replace the Spec.describe function with empty function if false. */
enabled: true,
/** Indicates whether object 'a' is "equal to" 'b'. */
equals: function(a, b) {
if (a instanceof Array && b instanceof Array) {
if (a.length != b.length) return false;
for (var i = 0; i < a.length; i++) if (!Spec.equals(a[i], b[i])) return false;
return true;
}
if ((a != null && b != null) && (typeof a == "object" && typeof b == "object")) {
for (var i in a) if (!Spec.equals(a[i], b[i])) return false;
return true;
}
return (a == b);
},
/** equivalent to xUint's assert */
should: function(expection, message) {
Spec.currentIndicator++;
if (!expection) {
var warning = [
"[Spec failed",
Spec.currentTitle ? " (" + Spec.currentTitle + ")] " : "] ",
(message || (Spec.currentMessage + " " + Spec.currentIndicator) || "")
].join("");
alert(warning);
throw warning;
}
return !!expection;
},
/** Write your specification by using describe method. */
describe: function(title, spec) {
Spec.currentTitle = title;
for (var name in spec) {
Spec.currentMessage = name;
Spec.currentIndicator = 0;
spec[name]();
Spec.currentIndicator = null;
}
Spec.currentMessage = Spec.currentTitle = null;
},
Version: "0.1"
};
// Other BDD style stuffs.
Spec.should.equal = function(a, b, message) { return Spec.should(Spec.equals(a, b), message); };
Spec.should.not = function(a, message) { return Spec.should(!a, message); };
Spec.should.not.equal = function(a, b, message) { return Spec.should(!Spec.equals(a, b), message); };
if (!Spec.enabled) Spec.describe = function(){};
// self test
Spec.describe("Spec object", {
"should": function() {
Spec.should(true);
Spec.should(1);
},
"should.not": function() {
Spec.should.not(false);
Spec.should.not(0);
},
"should.equal": function() {
Spec.should.equal(null, null);
Spec.should.equal("", "");
Spec.should.equal(12345, 12345);
Spec.should.equal([0,1,2], [0,1,2]);
Spec.should.equal([0,1,[0,1,2]], [0,1,[0,1,2]]);
Spec.should.equal({}, {});
Spec.should.equal({x:1}, {x:1});
Spec.should.equal({x:[1]}, {x:[1]});
},
"should.not.equal": function() {
Spec.should.not.equal([1,2,3], [1,2,3,4]);
Spec.should.not.equal({x:1}, [1,2,3,4]);
}
});
以上是关于JavaScript 适用于JavaScript的简单BDD(行为驱动开发)库。的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript Javascript:检测和显示页面宽度(适用于调试网站宽度)
深入理解JavaScript系列(23):JavaScript与DOM(上)——也适用于新手