[484] | 1 | |
---|
| 2 | /** |
---|
| 3 | * Module dependencies. |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | var Suite = require('../suite') |
---|
| 7 | , Test = require('../test') |
---|
| 8 | , utils = require('../utils');; |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * TDD-style interface: |
---|
| 12 | * |
---|
| 13 | * suite('Array', function(){ |
---|
| 14 | * suite('#indexOf()', function(){ |
---|
| 15 | * suiteSetup(function(){ |
---|
| 16 | * |
---|
| 17 | * }); |
---|
| 18 | * |
---|
| 19 | * test('should return -1 when not present', function(){ |
---|
| 20 | * |
---|
| 21 | * }); |
---|
| 22 | * |
---|
| 23 | * test('should return the index when present', function(){ |
---|
| 24 | * |
---|
| 25 | * }); |
---|
| 26 | * |
---|
| 27 | * suiteTeardown(function(){ |
---|
| 28 | * |
---|
| 29 | * }); |
---|
| 30 | * }); |
---|
| 31 | * }); |
---|
| 32 | * |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | module.exports = function(suite){ |
---|
| 36 | var suites = [suite]; |
---|
| 37 | |
---|
| 38 | suite.on('pre-require', function(context, file, mocha){ |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * Execute before each test case. |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | context.setup = function(fn){ |
---|
| 45 | suites[0].beforeEach(fn); |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Execute after each test case. |
---|
| 50 | */ |
---|
| 51 | |
---|
| 52 | context.teardown = function(fn){ |
---|
| 53 | suites[0].afterEach(fn); |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Execute before the suite. |
---|
| 58 | */ |
---|
| 59 | |
---|
| 60 | context.suiteSetup = function(fn){ |
---|
| 61 | suites[0].beforeAll(fn); |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Execute after the suite. |
---|
| 66 | */ |
---|
| 67 | |
---|
| 68 | context.suiteTeardown = function(fn){ |
---|
| 69 | suites[0].afterAll(fn); |
---|
| 70 | }; |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * Describe a "suite" with the given `title` |
---|
| 74 | * and callback `fn` containing nested suites |
---|
| 75 | * and/or tests. |
---|
| 76 | */ |
---|
| 77 | |
---|
| 78 | context.suite = function(title, fn){ |
---|
| 79 | var suite = Suite.create(suites[0], title); |
---|
| 80 | suites.unshift(suite); |
---|
| 81 | fn.call(suite); |
---|
| 82 | suites.shift(); |
---|
| 83 | return suite; |
---|
| 84 | }; |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * Pending suite. |
---|
| 88 | */ |
---|
| 89 | context.suite.skip = function(title, fn) { |
---|
| 90 | var suite = Suite.create(suites[0], title); |
---|
| 91 | suite.pending = true; |
---|
| 92 | suites.unshift(suite); |
---|
| 93 | fn.call(suite); |
---|
| 94 | suites.shift(); |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Exclusive test-case. |
---|
| 99 | */ |
---|
| 100 | |
---|
| 101 | context.suite.only = function(title, fn){ |
---|
| 102 | var suite = context.suite(title, fn); |
---|
| 103 | mocha.grep(suite.fullTitle()); |
---|
| 104 | }; |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Describe a specification or test-case |
---|
| 108 | * with the given `title` and callback `fn` |
---|
| 109 | * acting as a thunk. |
---|
| 110 | */ |
---|
| 111 | |
---|
| 112 | context.test = function(title, fn){ |
---|
| 113 | var suite = suites[0]; |
---|
| 114 | if (suite.pending) var fn = null; |
---|
| 115 | var test = new Test(title, fn); |
---|
| 116 | suite.addTest(test); |
---|
| 117 | return test; |
---|
| 118 | }; |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Exclusive test-case. |
---|
| 122 | */ |
---|
| 123 | |
---|
| 124 | context.test.only = function(title, fn){ |
---|
| 125 | var test = context.test(title, fn); |
---|
| 126 | var reString = '^' + utils.escapeRegexp(test.fullTitle()) + '$'; |
---|
| 127 | mocha.grep(new RegExp(reString)); |
---|
| 128 | }; |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Pending test case. |
---|
| 132 | */ |
---|
| 133 | |
---|
| 134 | context.test.skip = function(title){ |
---|
| 135 | context.test(title); |
---|
| 136 | }; |
---|
| 137 | }); |
---|
| 138 | }; |
---|