Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #37 from robert-chiniquy/master

Adds an optional BDD testing idiom to whiskey
  • Loading branch information...
commit a29eb556edae7de60467677b913411c63ea69f53 2 parents 66bb309 + 69c474a
Tomaz Muraus Kami authored
3  CHANGES.md
Source Rendered
... ... @@ -1,6 +1,9 @@
1 1 Changes
2 2 =======
3 3
  4 +* Add an optional, minimal BDD idiom implementation.
  5 + [Robert Chiniquy]
  6 +
4 7 * Send the SIGKILL signal instead of SIGTERM when killing child processes managed by the process runner.
5 8 [Robert Chiniquy]
6 9
12 README.md
Source Rendered
@@ -123,6 +123,18 @@ exports['test_two_equals_one'] = function(test, assert) {
123 123 }
124 124 ```
125 125
  126 +A simple example using the optional BDD module:
  127 +``` javascript
  128 +var bdd = require('whiskey').bdd.init(exports);
  129 +var describe = bdd.describe;
  130 +
  131 +describe('the bdd module', function(it) {
  132 + it('supports it(), expect(), and toEqual()', function(expect) {
  133 + expect(true).toEqual(true);
  134 + });
  135 +});
  136 +```
  137 +
126 138 For more examples please check the `example/` folder.
127 139
128 140 # Build status
39 example/test-bdd-failures.js
... ... @@ -0,0 +1,39 @@
  1 +/*
  2 + * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
  3 + * contributor license agreements. See the NOTICE file distributed with
  4 + * this work for additional information regarding copyright ownership.
  5 + * Cloudkick licenses this file to You under the Apache License, Version 2.0
  6 + * (the "License"); you may not use this file except in compliance with
  7 + * the License. You may obtain a copy of the License at
  8 + *
  9 + * http://www.apache.org/licenses/LICENSE-2.0
  10 + *
  11 + * Unless required by applicable law or agreed to in writing, software
  12 + * distributed under the License is distributed on an "AS IS" BASIS,
  13 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 + * See the License for the specific language governing permissions and
  15 + * limitations under the License.
  16 + */
  17 +
  18 +var bdd = require('../lib/bdd').init(exports);
  19 +var describe = bdd.describe;
  20 +
  21 +describe('the bdd expect()', function(it) {
  22 +
  23 + it('correctly fails toBeNull()', function(expect) {
  24 + expect("not null").toBeNull();
  25 + });
  26 +
  27 + it('correctly fails toBeDefined()', function(expect) {
  28 + expect(undefined).toBeDefined();
  29 + });
  30 +
  31 + it('correctly fails toBeUndefined()', function(expect) {
  32 + expect(true).toBeUndefined();
  33 + });
  34 +
  35 + it('correctly fails toMatch()', function(expect) {
  36 + expect('fish').toMatch(/not a fish/);
  37 + });
  38 +
  39 +});
69 example/test-bdd.js
... ... @@ -0,0 +1,69 @@
  1 +/*
  2 + * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
  3 + * contributor license agreements. See the NOTICE file distributed with
  4 + * this work for additional information regarding copyright ownership.
  5 + * Cloudkick licenses this file to You under the Apache License, Version 2.0
  6 + * (the "License"); you may not use this file except in compliance with
  7 + * the License. You may obtain a copy of the License at
  8 + *
  9 + * http://www.apache.org/licenses/LICENSE-2.0
  10 + *
  11 + * Unless required by applicable law or agreed to in writing, software
  12 + * distributed under the License is distributed on an "AS IS" BASIS,
  13 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 + * See the License for the specific language governing permissions and
  15 + * limitations under the License.
  16 + */
  17 +
  18 +var bdd = require('../lib/bdd').init(exports);
  19 +var describe = bdd.describe;
  20 +var beforeEach = bdd.beforeEach;
  21 +
  22 +var wasBeforeEachCalled = false;
  23 +
  24 +beforeEach(function() {
  25 + wasBeforeEachCalled = true;
  26 +});
  27 +
  28 +describe('the bdd module', function(it) {
  29 +
  30 + it('supports it(), expect(), and toEqual()', function(expect) {
  31 + expect(true).toEqual(true);
  32 + });
  33 +
  34 + it('supports beforeEach()', function(expect) {
  35 + expect(wasBeforeEachCalled).toEqual(true);
  36 + });
  37 +
  38 + it('supports async tests', function(expect, callback) {
  39 + var called = false;
  40 + setTimeout(function() {
  41 + called = true;
  42 + }, 1);
  43 + setTimeout(function() {
  44 + expect(called).toEqual(true);
  45 + callback();
  46 + }, 3);
  47 + });
  48 +
  49 +});
  50 +
  51 +describe('the bdd expect()', function(it) {
  52 +
  53 + it('handles toBeNull()', function(expect) {
  54 + expect(null).toBeNull();
  55 + });
  56 +
  57 + it('handles toBeDefined()', function(expect) {
  58 + expect(true).toBeDefined();
  59 + });
  60 +
  61 + it('handles toBeUndefined()', function(expect) {
  62 + expect(undefined).toBeUndefined();
  63 + });
  64 +
  65 + it('handles toMatch()', function(expect) {
  66 + expect('fish').toMatch(/is/);
  67 + });
  68 +
  69 +});
1  index.js
@@ -15,5 +15,6 @@
15 15 * limitations under the License.
16 16 */
17 17
  18 +exports.bdd = require('./lib/bdd.js');
18 19 exports.run = require('./lib/run.js').run;
19 20 exports.installCoverageHandler = require('./lib/coverage').installCoverageHandler;
97 lib/bdd.js
... ... @@ -0,0 +1,97 @@
  1 +/*
  2 + * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
  3 + * contributor license agreements. See the NOTICE file distributed with
  4 + * this work for additional information regarding copyright ownership.
  5 + * Cloudkick licenses this file to You under the Apache License, Version 2.0
  6 + * (the "License"); you may not use this file except in compliance with
  7 + * the License. You may obtain a copy of the License at
  8 + *
  9 + * http://www.apache.org/licenses/LICENSE-2.0
  10 + *
  11 + * Unless required by applicable law or agreed to in writing, software
  12 + * distributed under the License is distributed on an "AS IS" BASIS,
  13 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 + * See the License for the specific language governing permissions and
  15 + * limitations under the License.
  16 + */
  17 +
  18 +var sprintf = require('sprintf').sprintf;
  19 +
  20 +function Expect(actual) {
  21 + this._actual = actual;
  22 +}
  23 +
  24 +/**
  25 + * @param {Object} imports The foreign exports object to add bdd tests to
  26 + * @returns {Object} bdd A collection of test functions
  27 + */
  28 +exports.init = function(imports) {
  29 + var bdd = {
  30 + '_suiteSetup': function() {}
  31 + };
  32 +
  33 + /** @param {Function} setup function to call before each suite */
  34 + bdd.beforeEach = function(setup) {
  35 + bdd._suiteSetup = setup;
  36 + };
  37 +
  38 + /** @description creates a test suite
  39 + * @param {string} title
  40 + * @param {Function} suite
  41 + */
  42 + bdd.describe = function(title, suite) {
  43 +
  44 + /** @description it() is the equivalent of a exports['test blah'] in the whiskey idiom.
  45 + * This function, when called, adds a whiskey test to the imports object.
  46 + * @param {string} name
  47 + * @param {Function} spec(expect, callback)
  48 + */
  49 + function it(name, spec) {
  50 + var whiskeyName = sprintf("test %s %s", title, name);
  51 +
  52 + function expect(actual) {
  53 + return new Expect(actual);
  54 + }
  55 +
  56 + /** @description Re-binds the Expect test methods using the newly-injected assert.
  57 + * @param {object} test the test object injected by whiskey
  58 + * @param {object} assert the assert object injected by whiskey
  59 + */
  60 + imports[whiskeyName] = function(test, assert) {
  61 +
  62 + // make the whiskey test and assert objects available to bdd tests
  63 + bdd.test = test;
  64 + bdd.assert = assert;
  65 +
  66 + /** @description maps an assert method to a bdd matcher
  67 + * @param {string} assertion the name of a whiskey assert method
  68 + * @param {string} bddName the name of the equivalent expect method
  69 + */
  70 + function translateMatcher(assertion, bddName) {
  71 + Expect.prototype[bddName] = function() {
  72 + assert[assertion].bind(this, this._actual).apply(this, arguments);
  73 + }
  74 + }
  75 +
  76 + // This must be done each time a test is created,
  77 + // as test and assert are injected in each test function.
  78 + translateMatcher('equal', 'toEqual');
  79 + translateMatcher('isNull', 'toBeNull');
  80 + translateMatcher('isDefined', 'toBeDefined');
  81 + translateMatcher('isUndefined', 'toBeUndefined');
  82 + translateMatcher('match', 'toMatch');
  83 +
  84 + spec(expect, test.finish);
  85 + if (spec.length === 1) {
  86 + // if spec isn't expecting test.finish as an async callback, call it directly
  87 + test.finish();
  88 + }
  89 + }
  90 + }
  91 +
  92 + bdd._suiteSetup();
  93 + suite(it);
  94 + };
  95 +
  96 + return bdd;
  97 +}
17 test/run.sh
@@ -268,6 +268,23 @@ if [ $? -ne 0 ]; then
268 268 exit 1
269 269 fi
270 270
  271 +"${CWD}/bin/whiskey" --timeout 1000 \
  272 +--tests "${CWD}/example/test-bdd.js"
  273 +
  274 +if [ $? -ne 0 ]; then
  275 + echo "BDD test didn't exit with zero exit code."
  276 + exit 1
  277 +fi
  278 +
  279 +"${CWD}/bin/whiskey" --timeout 1000 \
  280 +--tests "${CWD}/example/test-bdd-failures.js"
  281 +
  282 +if [ $? -ne 4 ]; then
  283 + echo "BDD failure test didn't fail as expected."
  284 + exit 1
  285 +fi
  286 +
  287 +
271 288 echo ""
272 289 echo "* * * Whiskey test suite PASSED. * * *"
273 290 exit 0

0 comments on commit a29eb55

Please sign in to comment.
Something went wrong with that request. Please try again.