1 | /// Module suite
|
2 | //
|
3 | // Provides a way of grouping test cases.
|
4 | //
|
5 | //
|
6 | // Copyright (c) 2013 Quildreen Motta
|
7 | //
|
8 | // Permission is hereby granted, free of charge, to any person
|
9 | // obtaining a copy of this software and associated documentation files
|
10 | // (the "Software"), to deal in the Software without restriction,
|
11 | // including without limitation the rights to use, copy, modify, merge,
|
12 | // publish, distribute, sublicense, and/or sell copies of the Software,
|
13 | // and to permit persons to whom the Software is furnished to do so,
|
14 | // subject to the following conditions:
|
15 | //
|
16 | // The above copyright notice and this permission notice shall be
|
17 | // included in all copies or substantial portions of the Software.
|
18 | //
|
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
20 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
21 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
22 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
23 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
24 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
25 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
26 | //
|
27 |
|
28 | //// -- Dependencies ---------------------------------------------------
|
29 | var run = require('./runner').run
|
30 | var Test = require('./test').Test
|
31 | var pipeline = require('pinky-combinators').pipeline
|
32 |
|
33 |
|
34 | //// -- Helpers --------------------------------------------------------
|
35 |
|
36 | ///// λ K
|
37 | //
|
38 | // The constant function.
|
39 | //
|
40 | // :: a -> () -> a
|
41 | function K(a){ return function() { return a }}
|
42 |
|
43 |
|
44 | ///// λ hook
|
45 | //
|
46 | // Constructs a Hook object.
|
47 | //
|
48 | // A Hook is something that users can attach listeners to, and those can
|
49 | // be later on ran in the order they were attached (asynchronous hooks
|
50 | // fully wait for the previous one to finish).
|
51 | //
|
52 | // :: String -> Hook
|
53 | function hook(title) {
|
54 | var fns = []
|
55 |
|
56 | function add(fun) {
|
57 | fns.push(fun)
|
58 | return add }
|
59 |
|
60 | function run(value) {
|
61 | return pipeline(fns.concat([K(value)])) }
|
62 |
|
63 | add.run = run
|
64 |
|
65 | return add
|
66 | }
|
67 |
|
68 |
|
69 | //// -- Core implementation --------------------------------------------
|
70 |
|
71 | ///// {} Suite
|
72 | //
|
73 | // Represents a logical group of test cases.
|
74 | //
|
75 | // :: Test <| Suite
|
76 | var Suite = Test.derive({
|
77 |
|
78 | ////// λ init
|
79 | //
|
80 | // Initialises a Suite instance.
|
81 | //
|
82 | // :: @Suite => Suite, String -> ()
|
83 | init:
|
84 | function _init(parent, title) {
|
85 | this.parent = parent
|
86 | this.title = title
|
87 | this.tests = []
|
88 | this.beforeAll = hook('Before All')
|
89 | this.beforeEach = hook('Before Each')
|
90 | this.afterAll = hook('After All')
|
91 | this.afterEach = hook('After Each')
|
92 |
|
93 | if (parent) parent.add(this)
|
94 | }
|
95 |
|
96 | ///// λ add
|
97 | //
|
98 | // Adds a test case to the Suite.
|
99 | //
|
100 | // :: @Suite => Test -> ()
|
101 | , add:
|
102 | function _add(test) {
|
103 | var self = this
|
104 | this.tests.push(test.derive({
|
105 | run: function(reporter) {
|
106 | return pipeline([ self.beforeEach.run
|
107 | , test.run.bind(test, reporter)
|
108 | , self.afterEach.run
|
109 | ])
|
110 | }}))
|
111 | }
|
112 |
|
113 | ///// λ run
|
114 | //
|
115 | // Runs all tests in the Suite, sequentially.
|
116 | //
|
117 | // :: @Suite => Report -> Promise [Result]
|
118 | , run:
|
119 | function _run(report) {
|
120 | return pipeline([ this.beforeAll.run
|
121 | , run.bind(null, this.tests, null, report)
|
122 | , this.afterAll.run
|
123 | ])
|
124 | }
|
125 | })
|
126 |
|
127 |
|
128 | //// -- Exports --------------------------------------------------------
|
129 | module.exports = { Suite: Suite
|
130 | , hook: hook } |
\ | No newline at end of file |