forked from nathansobo/screw-unit
/
screw.builder.js
87 lines (74 loc) · 2.3 KB
/
screw.builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var Screw = (function($) {
var screw = {
Unit: function(fn) {
var contents = fn.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1];
var fn = new Function("matchers", "specifications",
"with (specifications) { with (matchers) { " + contents + " } }"
);
$(Screw).queue(function() {
Screw.Specifications.context.push($('body > .describe'));
fn.call(this, Screw.Matchers, Screw.Specifications);
Screw.Specifications.context.pop();
$(this).dequeue();
});
},
Specifications: {
context: [],
describe: function(name, fn) {
var describe = $('<li class="describe">')
.append($('<h1>').text(name))
.append('<ol class="befores">')
.append('<ul class="its">')
.append('<ul class="describes">')
.append('<ol class="afters">');
this.context.push(describe);
fn.call();
this.context.pop();
this.context[this.context.length-1]
.children('.describes')
.append(describe);
},
it: function(name, fn) {
var className = "it";
if (arguments.length < 2) {
name += " (pending)";
className = "pending";
fn = function(){};
}
var it = $('<li>')
.addClass(className)
.append($('<h2>').text(name))
.data('screwunit.run', fn);
this.context[this.context.length-1]
.children('.its')
.append(it);
},
before: function(fn) {
var before = $('<li class="before">')
.data('screwunit.run', fn);
this.context[this.context.length-1]
.children('.befores')
.append(before);
},
after: function(fn) {
var after = $('<li class="after">')
.data('screwunit.run', fn);
this.context[this.context.length-1]
.children('.afters')
.append(after);
}
}
};
$(screw).queue(function() { $(screw).trigger('loading') });
$(function() {
$('<div class="describe">')
.append('<h3 class="status">')
.append('<ol class="befores">')
.append('<ul class="describes">')
.append('<ol class="afters">')
.appendTo('body');
$(screw).dequeue();
$(screw).trigger('loaded');
});
return screw;
})(jQuery);