Permalink
Browse files

1.1.0

Removed parallel.
Better output.
Added `sync` option for tests that stub their asynchronous parts.
  • Loading branch information...
masylum committed Mar 19, 2011
1 parent b90f995 commit 176a924624aff3b6e83baa095074f048a6117d50
Showing with 111 additions and 125 deletions.
  1. +3 −6 Makefile
  2. +11 −22 Readme.md
  3. +40 −44 docs/index.html
  4. +54 −46 lib/testosterone.js
  5. +3 −7 package.json
View
@@ -1,20 +1,17 @@
NODE = node
-test: test_api test_parallel test_serial test_stubs
+test: test_api test_serial test_stubs test_sync
test_api:
@$(NODE) tests/test.js
-test_parallel:
- @$(NODE) tests/parallel.js
-
test_serial:
@$(NODE) tests/serial.js
test_stubs:
@$(NODE) tests/stubs.js
-test_app:
- @$(NODE) tests/app.js
+test_sync:
+ @$(NODE) tests/sync.js
.PHONY: test
View
@@ -13,35 +13,26 @@ Testosterone is built on nodejs but it allows you to test any http server.
Testosterone allows you to follow BDD or TDD on any of your projects using
the same testing library.
-You can run your tests in parallel or serial. Running tests in parallel can
-be a painful experience if you use stubs.
-
## Options
- `host` _(localhost)_ : Host to do the http calls.
- `port` _(80)_ : Host to do the http calls.
- `quiet` _(false)_ : Ninja mode.
- `title` _(Testosteron)_ : Test title, it will be printed out.
+- `sync` _(false)_ : If set to true, you don't need to specify when your tests are done as they run synchronously.
## API
_testosterone_ is simple and flexible.
- `get|post|head|put|delete...(url, req, response, cb)`: Does a http call with the given request. If a response is given, testosterone will assert that the real response matches.
-- `add(spec, resolve)`: Adds a test. The `spec` will be printed when `resolve` function is called. You can use `resolve` to curry a function.
-- `serial(cb)`: Runs the tests in serial, if you use stubs this options will be very handy. `cb` will be called once all the `resolve` functions are executed.
-- `parallel(cb)`: Runs the tests in parallel. `cb` will be called once all the `resolve` functions are executed.
+- `add(spec, done)`: Adds a test. The `spec` will be printed when `done` function is called. You can use `done` to curry a function.
+- `run(cb)`: Runs the tests in serial. `cb` will be called once all the `done` functions are executed.
- `assert`: Using this assert object instead of the native one will allow you to count and print the assertions.
-## Usage
-
-Use the returned object to do http calls with a sinatra-like-chainable syntax.
-
-If you want the virilest experiencie, use `testosterone.assert` instead of the native assert.
-
## Show me the code
-Chained example:
+HTTP testing example:
var testosterone = require('testosterone')({post: 3000}),
assert = testosterone.assert;
@@ -68,9 +59,9 @@ Chained example:
✿ Testosterone : ✓ ✓ ✓ ✓ ✓
» 3 responses, 5 asserts
-Serial example with [gently](https://github.com/felixge/node-gently.git) stubbing:
+Example with [gently](https://github.com/felixge/node-gently.git) stubbing and `sync: true`:
- var testosterone = require('testosterone')({post: 3000, title: 'Testing with stubs'}),
+ var testosterone = require('testosterone')({post: 3000, title: 'Testing with stubs', sync: true}),
gently = new (require('gently')),
fs = require('fs'),
assert = testosterone.assert;
@@ -82,7 +73,6 @@ Serial example with [gently](https://github.com/felixge/node-gently.git) stubbin
cb(null, null);
});
- spec();
fs.readFile('foo.txt', 'utf-8', function (er, data) {
assert.equal(er, null);
assert.equal(data, null);
@@ -95,14 +85,13 @@ Serial example with [gently](https://github.com/felixge/node-gently.git) stubbin
cb(null, 'foo');
});
- spec();
fs.readFile('foo.txt', 'utf-8', function (er, data) {
assert.equal(er, null);
assert.equal(data, 'foo');
});
})
- .serial(function () {
+ .run(function () {
require('sys').print('done!');
});
@@ -111,15 +100,15 @@ Serial example with [gently](https://github.com/felixge/node-gently.git) stubbin
$ node test.js
✿ Testing with stubs :
+
GIVEN foo.txt
WHEN its empty
- THEN it return null
- ✓ ✓ ✓
+ THEN it return null => ✓ ✓ ✓
GIVEN foo.txt
WHEN it have content
- THEN it return that content
- ✓ ✓ ✓
+ THEN it return that content => ✓ ✓ ✓
+
» 0 responses, 6 asserts
## Test
View
@@ -138,6 +138,15 @@
<span class="class">TESTOSTERONE</span> = {},
+ <span class="variable">_parseSpec</span> = <span class="keyword">function</span> (<span class="variable">spec</span>) {
+ <span class="keyword">var</span> <span class="variable">specs</span> = <span class="variable">spec</span>.<span class="variable">split</span>(<span class="string">'\n'</span>).<span class="variable">map</span>(<span class="keyword">function</span> (<span class="variable">spec</span>) {
+ <span class="variable">spec</span> = <span class="variable">spec</span>.<span class="variable">replace</span>(<span class="regexp">/(GIVEN|WHEN|AND|THEN)/gi</span>, <span class="string">'$1'</span>.<span class="variable">magenta</span> + <span class="string">'\033[90m'</span>);
+ <span class="variable">spec</span> = <span class="variable">spec</span>.<span class="variable">replace</span>(<span class="regexp">/`([^`]*)`/g</span>, <span class="string">'$1'</span>.<span class="variable">blue</span> + <span class="string">'\033[90m'</span>);
+ <span class="keyword">return</span> <span class="variable">spec</span>;
+ });
+ <span class="keyword">return</span> <span class="variable">specs</span>.<span class="variable">join</span>(<span class="string">'\n'</span>);
+ },
+
<span class="variable">_assert</span> = (<span class="keyword">function</span> () {
<span class="keyword">var</span> <span class="variable">assert</span> = <span class="variable">require</span>(<span class="string">'assert'</span>),
<span class="variable">functions</span> = <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">assert</span>),
@@ -193,11 +202,10 @@ <h2></h2>
</tr>
<tr class="code">
<td class="docs">
-<p>Adds a function to be called either serial or parallel</p>
+<p>Adds a function to be called</p>
<ul><li><strong>se</strong>: <em>e</em>
-#serial</li><li><strong>se</strong>: <em>e</em>
-#parallel</li></ul>
+#serial</li></ul>
<h2></h2>
@@ -206,21 +214,35 @@ <h2></h2>
</td>
<td class="code">
<pre><code><span class="class">TESTOSTERONE</span>.<span class="variable">add</span> = <span class="keyword">function</span> (<span class="variable">spec</span>, <span class="variable">resolved</span>) {
+ <span class="keyword">var</span> <span class="variable">print</span> = <span class="keyword">function</span> () {
+ <span class="variable">_sys</span>.<span class="variable">print</span>(<span class="string">'\n\n'</span> + <span class="variable">_parseSpec</span>(<span class="variable">spec</span>) + <span class="string">' =&gt; '</span>.<span class="variable">yellow</span>);
+ };
+
<span class="variable">_specs</span>.<span class="variable">add</span>(<span class="keyword">function</span> () {
- <span class="variable">resolved</span>.<span class="variable">call</span>(<span class="variable">resolved</span>, <span class="keyword">function</span> (<span class="variable">fn</span>) {
- <span class="keyword">if</span> (<span class="variable">fn</span>) {
- <span class="keyword">return</span> <span class="variable">_finished</span>.<span class="variable">add</span>(<span class="keyword">function</span> () {
+ <span class="keyword">if</span> (!<span class="variable">_config</span>.<span class="variable">sync</span>) {
+ <span class="variable">_finished</span>.<span class="variable">add</span>(<span class="keyword">function</span> () {})();
+ <span class="keyword">if</span> (!<span class="variable">_config</span>.<span class="variable">quiet</span>) {
+ <span class="variable">print</span>();
+ }
+ <span class="variable">resolved</span>.<span class="variable">call</span>(<span class="variable">resolved</span>, <span class="keyword">function</span> () { });
+ } <span class="keyword">else</span> {
+ <span class="variable">resolved</span>.<span class="variable">call</span>(<span class="variable">resolved</span>, <span class="keyword">function</span> (<span class="variable">fn</span>) {
+ <span class="keyword">if</span> (<span class="variable">fn</span>) {
+ <span class="keyword">return</span> <span class="variable">_finished</span>.<span class="variable">add</span>(<span class="keyword">function</span> () {
+ <span class="keyword">if</span> (!<span class="variable">_config</span>.<span class="variable">quiet</span>) {
+ <span class="variable">print</span>();
+ }
+ <span class="variable">fn</span>.<span class="variable">apply</span>(<span class="variable">fn</span>, <span class="variable">arguments</span>);
+ });
+ } <span class="keyword">else</span> {
+ <span class="comment">// this is hackish</span>
+ <span class="variable">_finished</span>.<span class="variable">add</span>(<span class="keyword">function</span> () {})();
<span class="keyword">if</span> (!<span class="variable">_config</span>.<span class="variable">quiet</span>) {
- <span class="variable">_sys</span>.<span class="variable">print</span>(<span class="string">'\n'</span> + <span class="variable">spec</span>.<span class="variable">grey</span> + <span class="string">'\n'</span>);
+ <span class="variable">print</span>();
}
- <span class="variable">fn</span>.<span class="variable">apply</span>(<span class="variable">fn</span>, <span class="variable">arguments</span>);
- });
- } <span class="keyword">else</span> {
- <span class="comment">// this is hackish</span>
- <span class="variable">_finished</span>.<span class="variable">add</span>(<span class="keyword">function</span> () {})();
- <span class="variable">_sys</span>.<span class="variable">print</span>(<span class="string">'\n'</span> + <span class="variable">spec</span>.<span class="variable">grey</span> + <span class="string">'\n'</span>);
- }
- });
+ }
+ });
+ }
})();
<span class="keyword">return</span> <span class="class">TESTOSTERONE</span>;
@@ -232,54 +254,28 @@ <h2></h2>
<p>Runs all the added tests in serial.</p>
<ul><li><strong>se</strong>: <em>e</em>
-#add</li><li><strong>se</strong>: <em>e</em>
-#parallel</li></ul>
+#add</li></ul>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> [cb=undefined]</p><p>Callback that is run after all the <code>resolved</code> callbacks are run.</p></li><li><p><strong>return</strong>: <em>s</em></p><p>Testosterone, so you can chain http calls.</p></li></ul>
</td>
<td class="code">
-<pre><code><span class="class">TESTOSTERONE</span>.<span class="variable">serial</span> = <span class="keyword">function</span> (<span class="variable">cb</span>) {
+<pre><code><span class="class">TESTOSTERONE</span>.<span class="variable">serial</span> = <span class="class">TESTOSTERONE</span>.<span class="variable">run</span> = <span class="keyword">function</span> (<span class="variable">cb</span>) {
<span class="variable">_specs</span>.<span class="variable">serial</span>(<span class="keyword">function</span> () {
<span class="variable">_finished</span>.<span class="variable">parallel</span>(<span class="keyword">function</span> () {
<span class="variable">cb</span>.<span class="variable">call</span>(<span class="variable">cb</span>, <span class="variable">arguments</span>);
<span class="variable">_test</span>();
});
});
- <span class="keyword">return</span> <span class="class">TESTOSTERONE</span>;
- };</code></pre>
-</td>
-</tr>
-<tr class="code">
-<td class="docs">
-<p>Runs all the added tests in parallel.</p>
-
-<ul><li><strong>se</strong>: <em>e</em>
-#add</li><li><strong>se</strong>: <em>e</em>
-#serial</li></ul>
-
-<h2></h2>
-
-<ul><li><p><strong>param</strong>: <em>Function</em> [cb=undefined]</p><p>Callback that is run after all the <code>resolved</code> callbacks are run.</p></li><li><p><strong>return</strong>: <em>s</em></p><p>Testosterone, so you can chain http calls.</p></li></ul>
-</td>
-<td class="code">
-<pre><code><span class="class">TESTOSTERONE</span>.<span class="variable">parallel</span> = <span class="keyword">function</span> (<span class="variable">cb</span>) {
- <span class="variable">_specs</span>.<span class="variable">parallel</span>(<span class="keyword">function</span> () {
- <span class="variable">_finished</span>.<span class="variable">parallel</span>(<span class="keyword">function</span> () {
- <span class="variable">cb</span>.<span class="variable">call</span>(<span class="variable">cb</span>, <span class="variable">arguments</span>);
- <span class="variable">_test</span>();
- });
- });
-
<span class="keyword">return</span> <span class="class">TESTOSTERONE</span>;
};
<span class="class">TESTOSTERONE</span>.<span class="variable">assert</span> = <span class="variable">_assert</span>;
<span class="keyword">if</span> (!<span class="variable">_config</span>.<span class="variable">quiet</span>) {
- <span class="variable">_sys</span>.<span class="variable">print</span>((<span class="string">'✿ '</span> + (<span class="variable">_config</span>.<span class="variable">title</span> || <span class="string">'Testosterone'</span>) + <span class="string">' : '</span>).<span class="variable">yellow</span>);
+ <span class="variable">_sys</span>.<span class="variable">print</span>((<span class="string">'✿ '</span> + (<span class="variable">_config</span>.<span class="variable">title</span> || <span class="string">'Testosterone'</span>) + <span class="string">' :'</span>).<span class="variable">inverse</span>.<span class="variable">yellow</span> + <span class="string">' '</span>);
}
<span class="keyword">return</span> <span class="class">TESTOSTERONE</span>;
Oops, something went wrong.

0 comments on commit 176a924

Please sign in to comment.