screw-unit-server 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -0
- data/README.markdown +347 -0
- data/Rakefile +70 -0
- data/bin/screw_unit +6 -0
- data/bin/screw_unit_server +10 -0
- data/core/EXAMPLE.html +68 -0
- data/core/LICENSE +22 -0
- data/core/README.markdown +307 -0
- data/core/TODO +2 -0
- data/core/example/models/cat.js +5 -0
- data/core/example/models/man.js +17 -0
- data/core/example/spec/matchers/have.js +8 -0
- data/core/example/spec/models/cat_spec.js +31 -0
- data/core/example/spec/models/man_spec.js +34 -0
- data/core/example/spec/spec_helper.js +5 -0
- data/core/example/spec/suite.html +25 -0
- data/core/lib/jquery-1.2.3.js +3408 -0
- data/core/lib/jquery.fn.js +29 -0
- data/core/lib/jquery.print.js +108 -0
- data/core/lib/screw.assets.js +36 -0
- data/core/lib/screw.behaviors.js +91 -0
- data/core/lib/screw.builder.js +80 -0
- data/core/lib/screw.css +90 -0
- data/core/lib/screw.events.js +38 -0
- data/core/lib/screw.matchers.js +65 -0
- data/core/lib/screw.server.js +21 -0
- data/core/spec/behaviors_spec.js +178 -0
- data/core/spec/matchers_spec.js +93 -0
- data/core/spec/print_spec.js +119 -0
- data/core/spec/spec_helper.js +0 -0
- data/core/spec/suite.html +18 -0
- data/init.rb +0 -0
- data/lib/screw_unit.rb +26 -0
- data/lib/screw_unit/resources.rb +2 -0
- data/lib/screw_unit/resources/spec.rb +37 -0
- data/spec/functional/functional_spec.rb +14 -0
- data/spec/functional/functional_spec_helper.rb +55 -0
- data/spec/functional_suite.rb +10 -0
- data/spec/spec_suite.rb +3 -0
- data/spec/unit/js_test_core/specs/spec_dir_spec.rb +43 -0
- data/spec/unit/js_test_core/specs/spec_file_spec.rb +43 -0
- data/spec/unit/unit_spec_helper.rb +120 -0
- data/spec/unit_suite.rb +10 -0
- data/vendor/js-test-core/CHANGES +9 -0
- data/vendor/js-test-core/README +12 -0
- data/vendor/js-test-core/Rakefile +72 -0
- data/vendor/js-test-core/lib/js_test_core.rb +30 -0
- data/vendor/js-test-core/lib/js_test_core/client.rb +50 -0
- data/vendor/js-test-core/lib/js_test_core/rack.rb +2 -0
- data/vendor/js-test-core/lib/js_test_core/rack/commonlogger.rb +5 -0
- data/vendor/js-test-core/lib/js_test_core/rails_server.rb +22 -0
- data/vendor/js-test-core/lib/js_test_core/resources.rb +11 -0
- data/vendor/js-test-core/lib/js_test_core/resources/dir.rb +52 -0
- data/vendor/js-test-core/lib/js_test_core/resources/file.rb +32 -0
- data/vendor/js-test-core/lib/js_test_core/resources/file_not_found.rb +15 -0
- data/vendor/js-test-core/lib/js_test_core/resources/runners.rb +15 -0
- data/vendor/js-test-core/lib/js_test_core/resources/runners/firefox_runner.rb +75 -0
- data/vendor/js-test-core/lib/js_test_core/resources/specs/spec_dir.rb +50 -0
- data/vendor/js-test-core/lib/js_test_core/resources/specs/spec_file.rb +17 -0
- data/vendor/js-test-core/lib/js_test_core/resources/suite.rb +24 -0
- data/vendor/js-test-core/lib/js_test_core/resources/suite_finish.rb +19 -0
- data/vendor/js-test-core/lib/js_test_core/resources/web_root.rb +62 -0
- data/vendor/js-test-core/lib/js_test_core/selenium.rb +2 -0
- data/vendor/js-test-core/lib/js_test_core/selenium/selenium_driver.rb +5 -0
- data/vendor/js-test-core/lib/js_test_core/server.rb +111 -0
- data/vendor/js-test-core/lib/js_test_core/thin.rb +3 -0
- data/vendor/js-test-core/lib/js_test_core/thin/backends/js_test_core_server.rb +9 -0
- data/vendor/js-test-core/lib/js_test_core/thin/js_test_core_connection.rb +42 -0
- data/vendor/js-test-core/spec/example_core/JsTestCore.css +0 -0
- data/vendor/js-test-core/spec/example_core/JsTestCore.js +0 -0
- data/vendor/js-test-core/spec/example_public/favicon.ico +0 -0
- data/vendor/js-test-core/spec/example_public/javascripts/foo.js +3 -0
- data/vendor/js-test-core/spec/example_public/robots.txt +0 -0
- data/vendor/js-test-core/spec/example_public/stylesheets/example.css +3 -0
- data/vendor/js-test-core/spec/example_specs/failing_spec.js +5 -0
- data/vendor/js-test-core/spec/example_specs/foo/failing_spec.js +6 -0
- data/vendor/js-test-core/spec/example_specs/foo/passing_spec.js +6 -0
- data/vendor/js-test-core/spec/spec_suite.rb +2 -0
- data/vendor/js-test-core/spec/specs/failing_spec.js +0 -0
- data/vendor/js-test-core/spec/unit/js_test_core/client_spec.rb +137 -0
- data/vendor/js-test-core/spec/unit/js_test_core/rails_server_spec.rb +45 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/dir_spec.rb +42 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/file_not_found_spec.rb +26 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/file_spec.rb +88 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/runner_spec.rb +24 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/runners/firefox_runner_spec.rb +197 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/specs/spec_dir_spec.rb +79 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/specs/spec_file_spec.rb +42 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/suite_finish_spec.rb +94 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/suite_spec.rb +44 -0
- data/vendor/js-test-core/spec/unit/js_test_core/resources/web_root_spec.rb +103 -0
- data/vendor/js-test-core/spec/unit/js_test_core/server_spec.rb +131 -0
- data/vendor/js-test-core/spec/unit/thin/js_test_core_connection_spec.rb +92 -0
- data/vendor/js-test-core/spec/unit/unit_spec_helper.rb +125 -0
- data/vendor/js-test-core/spec/unit_suite.rb +10 -0
- metadata +186 -0
data/core/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2008 Nick Kallen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,307 @@
|
|
1
|
+
Screw.Unit is a Behavior-Driven Testing Framework for Javascript. It features nested describes. Its goals are to provide:
|
2
|
+
|
3
|
+
* a DSL for elegant, readable, organized specs;
|
4
|
+
* an interactive runner that can execute focused specs and describes;
|
5
|
+
* and brief, extensible source-code.
|
6
|
+
|
7
|
+
# What it is
|
8
|
+
|
9
|
+
![Test Runner](http://s3.amazonaws.com/assets.pivotallabs.com/87/original/runner.png)
|
10
|
+
|
11
|
+
The testing language is closure-based. Consider,
|
12
|
+
|
13
|
+
describe("Matchers", function() {
|
14
|
+
it("invokes the provided matcher on a call to expect", function() {
|
15
|
+
expect(true).to(equal, true);
|
16
|
+
expect(true).to_not(equal, false);
|
17
|
+
});
|
18
|
+
});
|
19
|
+
|
20
|
+
A key feature of Screw.Unit are nested `describes` and the cascading `before` (and `after`) behavior that entails:
|
21
|
+
|
22
|
+
describe("a nested describe", function() {
|
23
|
+
var invocations = [];
|
24
|
+
|
25
|
+
before(function() {
|
26
|
+
invocations.push("before");
|
27
|
+
});
|
28
|
+
|
29
|
+
describe("a doubly nested describe", function() {
|
30
|
+
before(function() {
|
31
|
+
invocations.push('inner before');
|
32
|
+
});
|
33
|
+
|
34
|
+
it("runs befores in all ancestors prior to an it", function() {
|
35
|
+
expect(invocations).to(equal, ["before", "inner before"]);
|
36
|
+
});
|
37
|
+
});
|
38
|
+
});
|
39
|
+
|
40
|
+
# The Runner
|
41
|
+
|
42
|
+
The Screw.Unit runner is pretty fancy, supporting focused `describes` and focused `its`:
|
43
|
+
|
44
|
+
![Focused Runner](http://s3.amazonaws.com/assets.pivotallabs.com/86/original/focused.png)
|
45
|
+
|
46
|
+
Click on a `describe` or `it` to run just those tests.
|
47
|
+
|
48
|
+
# Global Befores and Afters
|
49
|
+
|
50
|
+
A global `before` is a `before` block run before all tests in a test suite, regardless of their nesting. This is often useful to reset global variables, or blank-out DOM nodes before each test is run. Put this at the top of the your suite file or in your spec helper.
|
51
|
+
|
52
|
+
Screw.Unit(function() {
|
53
|
+
before(function() { ... });
|
54
|
+
});
|
55
|
+
|
56
|
+
Note that you can have any number of `Screw.Unit(...)` blocks in one file. Thus, you can have multiple global `befores` and `afters`.
|
57
|
+
|
58
|
+
# Custom Matchers
|
59
|
+
|
60
|
+
A custom matcher is a custom assertion specifically tailored to your application. These are helpful in increasing the readability and declarativity of your tests. To create a custom matcher, fill in the blanks for this code:
|
61
|
+
|
62
|
+
Screw.Matchers["be_even"] = {
|
63
|
+
match: function(expected, actual) {
|
64
|
+
return actual % 2 == 0;
|
65
|
+
},
|
66
|
+
failure_message: function(expected, actual, not) {
|
67
|
+
return 'expected ' + $.print(actual) + (not ? ' not' : '') + ' to be even';
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
You can invoke this matcher as follows: `expect(2).to(be_even)`.
|
72
|
+
|
73
|
+
# The Anatomy of Test Infrastructure
|
74
|
+
|
75
|
+
Typical test infrastructure spans multiple files:
|
76
|
+
|
77
|
+
* A `suite.html` file that has the necessary html, script tags, and link tags, to include your source code as well as the test infrastructure.
|
78
|
+
* A `spec_helper.js` file with global `before` and `after` blocks.
|
79
|
+
* A set of custom matchers.
|
80
|
+
* Your individual tests.
|
81
|
+
|
82
|
+
The file structure will typically look like:
|
83
|
+
|
84
|
+
spec/
|
85
|
+
suite.html
|
86
|
+
spec_helper.js
|
87
|
+
matchers/
|
88
|
+
a_matcher.js
|
89
|
+
another_matcher.js
|
90
|
+
models/
|
91
|
+
a_spec.js
|
92
|
+
another_spec.js
|
93
|
+
views/
|
94
|
+
yet_another_spec.js
|
95
|
+
|
96
|
+
The `models` and `views` directories are here only for comparison. As a general rule, mirror the file structure of your source code in your spec directory. For example, if you have an MVC application and you organize your source code into `models`, `views`, and `controllers` directories, have parallel directories in your spec/ directory, with tests for your models, views, and controllers in their respective directories.
|
97
|
+
|
98
|
+
# Writing Good Tests
|
99
|
+
|
100
|
+
A great test maximizes these features:
|
101
|
+
|
102
|
+
* it provides **documentation**, explaining the intended functioning of the system as well as how the source code works;
|
103
|
+
* it supports **ongoing development**, as you bit-by-bit write a failing test and make it pass;
|
104
|
+
* it supports **refactoring** and **prevents regression**;
|
105
|
+
* and it **requires little modification** as the implementation of the system changes, especially changes to unrelated code.
|
106
|
+
|
107
|
+
This section focuses principally on tests as documentation. **To provide documentation, as well as support future modification, a test should be readable and well organized.** Here are some recommendations on how to do it.
|
108
|
+
|
109
|
+
## Use Nested Describes to Express Context
|
110
|
+
|
111
|
+
Often, when you test a system (a function, an object), it behaves differently in different contexts. Use **nested** describes liberally to express the context under which you make an assertion.
|
112
|
+
|
113
|
+
describe("Caller#prioritize", function() {
|
114
|
+
describe("when there are two callers in the queue", function() {
|
115
|
+
describe("and one caller has been waiting longer than another", function() {
|
116
|
+
...
|
117
|
+
});
|
118
|
+
});
|
119
|
+
});
|
120
|
+
|
121
|
+
In addition to using nested describes to express context, use them to organize tests by the structural properties of your source code and programming language. In Javascript this is typically prototype and function. A parent describe for a prototype contains nested describes for each of its methods. If you have cross-cutting concerns (e.g., related behavior that spans across methods or prototypes), use a describe to group them conceptually.
|
122
|
+
|
123
|
+
describe("Car", function() {
|
124
|
+
describe("#start", function() {
|
125
|
+
});
|
126
|
+
|
127
|
+
describe("#stop", function() {
|
128
|
+
});
|
129
|
+
|
130
|
+
describe("callbacks", function() {
|
131
|
+
describe("after_purchase", function() {
|
132
|
+
});
|
133
|
+
});
|
134
|
+
|
135
|
+
describe("logging", function() {
|
136
|
+
});
|
137
|
+
});
|
138
|
+
|
139
|
+
In this example, one parent `describe` is used for all `Car` behavior. There is a describe for each method. Finally, cross-cutting concerns like callbacks and logging are grouped because of their conceptual affinity.
|
140
|
+
|
141
|
+
# Test Size
|
142
|
+
|
143
|
+
Individual tests should be short and sweet. It is sometimes recommended to make only one assertion per test:
|
144
|
+
|
145
|
+
it("chooses the caller who has been waiting the longest", function() {
|
146
|
+
expect(Caller.prioritize()).to(equal, caller_waiting_the_longest);
|
147
|
+
});
|
148
|
+
|
149
|
+
According to some, the ideal test is one line of code. In practice, it may be excessive to divide your tests to be this small. At ten lines of code (or more), a test is difficult to read quickly. Be pragmatic, bearing in mind the aims of testing.
|
150
|
+
|
151
|
+
Although one assertion per test is a good rule of thumb, feel free to violate the rule if equal clarity and better terseness is achievable:
|
152
|
+
|
153
|
+
it("returns the string representation of the boolean", function() {
|
154
|
+
expect($.print(true)).to(equal, 'true');
|
155
|
+
expect($.print(false)).to(equal, 'false');
|
156
|
+
});
|
157
|
+
|
158
|
+
Two tests would be overkill in this example.
|
159
|
+
|
160
|
+
## Variable Naming
|
161
|
+
|
162
|
+
Name variables descriptively, especially ones that will become expected values in assertions. `caller_waiting_the_longest` is better than `c1`.
|
163
|
+
|
164
|
+
## Dividing code between tests and `befores`
|
165
|
+
|
166
|
+
If there is only one line of setup and it is used in only one test, it may be better to include the setup in the test itself:
|
167
|
+
|
168
|
+
it("decrements the man's luck by 5", function() {
|
169
|
+
var man = new Man({luck: 5});
|
170
|
+
|
171
|
+
cat.cross_path(man);
|
172
|
+
expect(man.luck()).to(equal, 0);
|
173
|
+
});
|
174
|
+
|
175
|
+
But in general, it's nice to keep setup code in `before` blocks, especially if the setup can be shared across tests.
|
176
|
+
|
177
|
+
describe('Man', function() {
|
178
|
+
var man;
|
179
|
+
before(function() {
|
180
|
+
man = new Man({luck: 5});
|
181
|
+
});
|
182
|
+
|
183
|
+
describe('#decrement_luck', function() {
|
184
|
+
it("decrements the luck field by the given amount", function() {
|
185
|
+
man.decrement_luck(3);
|
186
|
+
expect(man.luck()).to(equal, 2)
|
187
|
+
});
|
188
|
+
});
|
189
|
+
...
|
190
|
+
});
|
191
|
+
|
192
|
+
## Preconditions
|
193
|
+
|
194
|
+
It is ideal, if there is any chance that your preconditions are non-obvious, to make precondition asserts in your test. The last example, were it more complicated, might be better written:
|
195
|
+
|
196
|
+
it("decrements the luck field by the given amount", function() {
|
197
|
+
expect(man.luck()).to(equal, 5);
|
198
|
+
|
199
|
+
man.decrement_luck(3);
|
200
|
+
expect(man.luck()).to(equal, 2)
|
201
|
+
});
|
202
|
+
|
203
|
+
Whitespace, as seen here, can be helpful in distinguishing setup and preconditions from the system under test (SUT) and its assertions. It is nice to be consistent in your use of whitespace (e.g., "always follow a group of preconditions by a newline"). But it is better to use whitespace as makes the most sense given the context. As with everything in life, do it consciously and deliberately, but change your mind frequently.
|
204
|
+
|
205
|
+
## Behavioral Testing
|
206
|
+
|
207
|
+
Behavioral testing, that is, asserting that certain functions are called rather than certain values returned, is best done with closures. The dynamic nature of JavaScript makes mocking frameworks mostly unnecessary.
|
208
|
+
|
209
|
+
it("invokes #decrement_luck", function() {
|
210
|
+
var decrement_luck_was_called = false;
|
211
|
+
man.decrement_luck = function(amount) {
|
212
|
+
decrement_luck_was_called = true;
|
213
|
+
});
|
214
|
+
|
215
|
+
cat.cross_path(man);
|
216
|
+
expect(decrement_luck_was_called).to(equal, true);
|
217
|
+
});
|
218
|
+
|
219
|
+
# How to Test the DOM
|
220
|
+
|
221
|
+
The simplest way to test the DOM is to have a special DOM node in your `suite.html` file. Have all tests insert nodes into this node; have a global `before` reset the node between tests.
|
222
|
+
|
223
|
+
In `suite.html`:
|
224
|
+
|
225
|
+
<div id="dom_test"></div>
|
226
|
+
|
227
|
+
In `spec_helper.js`:
|
228
|
+
|
229
|
+
Screw.Unit(function() {
|
230
|
+
before(function() {
|
231
|
+
document.getElementById('dom_test').innerHTML = ''; // but use your favorite JS library here.
|
232
|
+
});
|
233
|
+
});
|
234
|
+
|
235
|
+
In `some_spec.js`:
|
236
|
+
|
237
|
+
describe("something that manipulates the DOM", function() {
|
238
|
+
it("is effortless to test!", function() {
|
239
|
+
var dom_test = document.getElementById('dom_test');
|
240
|
+
dom_test.innerHTML = 'awesome';
|
241
|
+
expect(dom_test.innerHTML).to(equal, 'awesome');
|
242
|
+
});
|
243
|
+
});
|
244
|
+
|
245
|
+
A Javascript library like jQuery, Prototype, or YUI is a essential for testing events.
|
246
|
+
|
247
|
+
# Implementation Details
|
248
|
+
|
249
|
+
Screw.Unit is implemented using some fancy metaprogramming learned from the formidable Yehuda Katz. This allows the `describe` and `it` functions to not pollute the global namespace. Essentially, we take the source code of your test and wrap it in a with block which provides a new scope:
|
250
|
+
|
251
|
+
var contents = fn.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1];
|
252
|
+
var fn = new Function("matchers", "specifications",
|
253
|
+
"with (specifications) { with (matchers) { " + contents + " } }"
|
254
|
+
);
|
255
|
+
|
256
|
+
fn.call(this, Screw.Matchers, Screw.Specifications);
|
257
|
+
|
258
|
+
Furthermore, Screw.Unit is implemented using the **Concrete Javascript** style, which is made possible by the [Effen plugin](http://github.com/nkallen/effen/tree/master) and jQuery. Concrete Javascript is an alternative to MVC. In Concrete Javascript, DOM objects serve as the model and view simultaneously. The DOM is constructed using semantic (and visual) markup, and behaviors are attached directly to DOM elements. For example,
|
259
|
+
|
260
|
+
$('.describe').fn({
|
261
|
+
parent: function() {
|
262
|
+
return $(this).parent('.describes').parent('.describe');
|
263
|
+
},
|
264
|
+
run: function() {
|
265
|
+
$(this).children('.its').children('.it').fn('run');
|
266
|
+
$(this).children('.describes').children('.describe').fn('run');
|
267
|
+
},
|
268
|
+
});
|
269
|
+
|
270
|
+
Here two methods (`#parent` and `#run`) are attached directly to DOM elements that have class `describe`. To invoke one of these methods, simply:
|
271
|
+
|
272
|
+
$('.describe').fn('run');
|
273
|
+
|
274
|
+
Bind behaviors by passing a hash (see the previous example). Using CSS3 selectors and cascading to attach behaviors provides interesting kind of multiple inheritance and polymorphism:
|
275
|
+
|
276
|
+
$('.describe, .it').fn({...}); // applies to both describe and its
|
277
|
+
$('.describe .describe').fn({...}); // applies to nested describes only
|
278
|
+
|
279
|
+
# Extensibility
|
280
|
+
|
281
|
+
Screw.Unit is designed from the ground-up to be extensible. For example, to add custom logging, simply subscribe to certain events:
|
282
|
+
|
283
|
+
$('.it')
|
284
|
+
.bind('enqueued', function() {...})
|
285
|
+
.bind('running', function() {...})
|
286
|
+
.bind('passed', function() {...})
|
287
|
+
.bind('failed', function(e, reason) {...})
|
288
|
+
|
289
|
+
There are also events for the `loading` and `loaded` test code code, as well as just `before` and just `after` all tests are run:
|
290
|
+
|
291
|
+
$(Screw)
|
292
|
+
.bind('loading', function() {...})
|
293
|
+
.bind('loaded', function() {...})
|
294
|
+
.bind('before', function() {...})
|
295
|
+
.bind('after', function() {...})
|
296
|
+
|
297
|
+
# Download
|
298
|
+
|
299
|
+
You can [download the source](http://github.com/nkallen/screw-unit/tree/master) from Github. There is are plenty of examples in the distribution.
|
300
|
+
|
301
|
+
# Thanks to
|
302
|
+
|
303
|
+
* Nathan Sobo
|
304
|
+
* Yehuda Katz
|
305
|
+
* Brian Takita
|
306
|
+
* Aman Gupta
|
307
|
+
* Tim Connor
|
data/core/TODO
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
function Man(options) {
|
2
|
+
var luck = options.luck;
|
3
|
+
|
4
|
+
this.decrement_luck = function(delta) {
|
5
|
+
luck = Math.max(0, luck - delta);
|
6
|
+
};
|
7
|
+
this.luck = function() {
|
8
|
+
return luck;
|
9
|
+
};
|
10
|
+
this.render = function() {
|
11
|
+
return $('<ul class="man">')
|
12
|
+
.append('<li class="hair">')
|
13
|
+
.click(function() {
|
14
|
+
$(this).children('.hair').remove();
|
15
|
+
});
|
16
|
+
};
|
17
|
+
}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
Screw.Matchers["have"] = {
|
2
|
+
match: function(expected, actual) {
|
3
|
+
return actual.find(expected).length > 0;
|
4
|
+
},
|
5
|
+
failure_message: function(expected, actual, not) {
|
6
|
+
return 'expected ' + $.print(actual) + (not ? ' to not have ' : ' to have ') + $.print(expected);
|
7
|
+
}
|
8
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
Screw.Unit(function() {
|
2
|
+
describe('Cat', function() {
|
3
|
+
var cat;
|
4
|
+
|
5
|
+
describe('#cross_path', function() {
|
6
|
+
describe('when the cat has black fur', function() {
|
7
|
+
before(function() {
|
8
|
+
cat = new Cat({color: 'black'});
|
9
|
+
});
|
10
|
+
|
11
|
+
it("decrements the man's luck by 5", function() {
|
12
|
+
var man = new Man({luck: 5});
|
13
|
+
cat.cross_path(man);
|
14
|
+
expect(man.luck()).to(equal, 0);
|
15
|
+
});
|
16
|
+
});
|
17
|
+
|
18
|
+
describe('when the cat has non-black fur', function() {
|
19
|
+
before(function() {
|
20
|
+
cat = new Cat({color: 'white'});
|
21
|
+
});
|
22
|
+
|
23
|
+
it("does not change the man's luck", function() {
|
24
|
+
var man = new Man({luck: 5});
|
25
|
+
cat.cross_path(man);
|
26
|
+
expect(man.luck()).to(equal, 5);
|
27
|
+
});
|
28
|
+
});
|
29
|
+
});
|
30
|
+
});
|
31
|
+
});
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Screw.Unit(function() {
|
2
|
+
describe('Man', function() {
|
3
|
+
var man;
|
4
|
+
before(function() {
|
5
|
+
man = new Man({luck: 5});
|
6
|
+
});
|
7
|
+
|
8
|
+
describe('#decrement_luck', function() {
|
9
|
+
it("decrements the luck field by the given amount", function() {
|
10
|
+
man.decrement_luck(3);
|
11
|
+
expect(man.luck()).to(equal, 2)
|
12
|
+
});
|
13
|
+
|
14
|
+
describe('when the decrement exceeds the luck balance', function() {
|
15
|
+
it("decrements the luck field to zero", function() {
|
16
|
+
man.decrement_luck(10000000000);
|
17
|
+
expect(man.luck()).to(equal, 0);
|
18
|
+
});
|
19
|
+
});
|
20
|
+
});
|
21
|
+
|
22
|
+
describe('@click', function() {
|
23
|
+
before(function() {
|
24
|
+
$('#dom_test').append(man.render());
|
25
|
+
});
|
26
|
+
|
27
|
+
it("removes the man's hair", function() {
|
28
|
+
expect($('.man')).to(have, '.hair');
|
29
|
+
$('.man').click();
|
30
|
+
expect($('.man')).to_not(have, '.hair');
|
31
|
+
});
|
32
|
+
});
|
33
|
+
});
|
34
|
+
});
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<script src="../../lib/jquery-1.2.3.js"></script>
|
4
|
+
<script src="../../lib/jquery.fn.js"></script>
|
5
|
+
<script src="../../lib/jquery.print.js"></script>
|
6
|
+
<script src="../../lib/screw.builder.js"></script>
|
7
|
+
<script src="../../lib/screw.matchers.js"></script>
|
8
|
+
<script src="../../lib/screw.events.js"></script>
|
9
|
+
<script src="../../lib/screw.behaviors.js"></script>
|
10
|
+
|
11
|
+
<script src="spec_helper.js"></script>
|
12
|
+
<script src="matchers/have.js"></script>
|
13
|
+
|
14
|
+
<script src="../models/cat.js"></script>
|
15
|
+
<script src="../models/man.js"></script>
|
16
|
+
|
17
|
+
<script src="models/cat_spec.js"></script>
|
18
|
+
<script src="models/man_spec.js"></script>
|
19
|
+
|
20
|
+
<link rel="stylesheet" href="../../lib/screw.css">
|
21
|
+
</head>
|
22
|
+
<body>
|
23
|
+
<div id="dom_test" style="position: absolute; left: -9999"></div>
|
24
|
+
</body>
|
25
|
+
</html>
|