A Simple BDD (Behaviour Driven Development) library for JavaScript.


/ Published in: JavaScript
Save to your folder(s)

A Tiny JavaScript liberary for Behaviour-Driven Development (BDD).


Copy this code and paste it in your HTML
  1. /*
  2.  * A Simple BDD (Behaviour Driven Development) library for JavaScript.
  3.  *
  4.  * Copyright (c) 2008 Takanori Ishikawa <[email protected]>
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  * notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  * notice, this list of conditions and the following disclaimer in the
  16.  * documentation and/or other materials provided with the distribution.
  17.  *
  18.  * 3. Neither the name of the authors nor the names of its contributors
  19.  * may be used to endorse or promote products derived from this
  20.  * software without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  28.  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33.  */
  34. /**
  35.  * A Tiny JavaScript liberary for Behaviour-Driven Development (BDD).
  36.  *
  37.  * Author: Takanori Ishikawa <[email protected]>
  38.  * Copyright: Takanori Ishikawa 2008
  39.  * License: BSD License (see above)
  40.  *
  41.  * Supported Browsers:
  42.  * [Win] IE 6, Firefox 2
  43.  * [Mac] Safari 3, Firefox 2
  44.  *
  45.  * Usage:
  46.  * Spec.describe("Spec.should", function() {
  47.  * "simple values": function() {
  48.  * "should": function() {
  49.  * Spec.should(true);
  50.  * Spec.should(1);
  51.  * },
  52.  * ...
  53.  * },
  54.  * ...
  55.  * });
  56.  *
  57.  * See Also:
  58.  * Behavior Driven Development - Wikipedia, the free encyclopedia
  59.  * http://en.wikipedia.org/wiki/Behavior_driven_development
  60.  *
  61.  */
  62.  
  63.  
  64. /**
  65.  * Spec is the BDD style test utilities.
  66.  */
  67. var Spec = {
  68. /** Replace the Spec.describe function with empty function if false. */
  69. enabled: true,
  70.  
  71. /** Indicates whether object 'a' is "equal to" 'b'. */
  72. equals: function(a, b) {
  73. if (a instanceof Array && b instanceof Array) {
  74. if (a.length != b.length) return false;
  75. for (var i = 0; i < a.length; i++) if (!Spec.equals(a[i], b[i])) return false;
  76. return true;
  77. }
  78. if ((a != null && b != null) && (typeof a == "object" && typeof b == "object")) {
  79. for (var i in a) if (!Spec.equals(a[i], b[i])) return false;
  80. return true;
  81. }
  82. return (a == b);
  83. },
  84.  
  85. /** equivalent to xUint's assert */
  86. should: function(expection, message) {
  87. Spec.currentIndicator++;
  88. if (!expection) {
  89. var warning = [
  90. "[Spec failed",
  91. Spec.currentTitle ? " (" + Spec.currentTitle + ")] " : "] ",
  92. (message || (Spec.currentMessage + " " + Spec.currentIndicator) || "")
  93. ].join("");
  94.  
  95. alert(warning);
  96. throw warning;
  97. }
  98. return !!expection;
  99. },
  100.  
  101. /** Write your specification by using describe method. */
  102. describe: function(title, spec) {
  103. Spec.currentTitle = title;
  104. for (var name in spec) {
  105. Spec.currentMessage = name;
  106. Spec.currentIndicator = 0;
  107. spec[name]();
  108. Spec.currentIndicator = null;
  109. }
  110. Spec.currentMessage = Spec.currentTitle = null;
  111. },
  112. Version: "0.1"
  113. };
  114.  
  115. // Other BDD style stuffs.
  116. Spec.should.equal = function(a, b, message) { return Spec.should(Spec.equals(a, b), message); };
  117. Spec.should.not = function(a, message) { return Spec.should(!a, message); };
  118. Spec.should.not.equal = function(a, b, message) { return Spec.should(!Spec.equals(a, b), message); };
  119. if (!Spec.enabled) Spec.describe = function(){};
  120.  
  121.  
  122. // self test
  123. Spec.describe("Spec object", {
  124. "should": function() {
  125. Spec.should(true);
  126. Spec.should(1);
  127. },
  128. "should.not": function() {
  129. Spec.should.not(false);
  130. Spec.should.not(0);
  131. },
  132. "should.equal": function() {
  133. Spec.should.equal(null, null);
  134. Spec.should.equal("", "");
  135. Spec.should.equal(12345, 12345);
  136. Spec.should.equal([0,1,2], [0,1,2]);
  137. Spec.should.equal([0,1,[0,1,2]], [0,1,[0,1,2]]);
  138. Spec.should.equal({}, {});
  139. Spec.should.equal({x:1}, {x:1});
  140. Spec.should.equal({x:[1]}, {x:[1]});
  141. },
  142. "should.not.equal": function() {
  143. Spec.should.not.equal([1,2,3], [1,2,3,4]);
  144. Spec.should.not.equal({x:1}, [1,2,3,4]);
  145. }
  146. });

URL: http://weblog.metareal.org/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.