forked from nathansobo/screw-unit
/
screw.builder.js
121 lines (114 loc) · 3.5 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var Screw = {
Unit: function(suite) {
var contents = suite.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1];
var suite = new Function("matchers", "specifications",
"with (specifications) { with (matchers) { " + contents + " } }"
);
if(!Screw.global_suite_added) {
$(function() {
var body = $('body');
$('body')
.addClass('describe')
.append($('<h3 class="status">'))
.append('<ol class="befores">')
.append('<ul class="its">')
.append('<ul class="describes">')
.append('<ol class="afters">')
Screw.Specifications.context.push(body);
})
Screw.global_suite_added = true
}
Screw.last_suite = suite;
$(function() {
suite.call(this, Screw.Matchers, Screw.Specifications);
if(Screw.last_suite == suite) {
$(Screw).trigger('loaded')
}
})
},
global_suite_added: false,
last_suite: null,
Specifications: {
context: [],
describe: function(name, fn) {
var describe = $('<li class="describe">');
$('<h1>').text(name).appendTo(describe);
describe
.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 it = $('<li class="it">');
$('<h2>').text(name).appendTo(it);
it.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]
.find('.befores')
.append(before);
},
after: function(fn) {
var after = $('<li class="after">')
.data('screwunit.run', fn);
this.context[this.context.length-1]
.find('.afters')
.append(after);
}
},
Matchers: {
expect: function(actual) {
return {
to: function(matcher, expected, not) {
var matched = matcher.match(expected, actual);
if (not ? matched : !matched)
throw(matcher.failure_message(expected, actual, not));
},
to_not: function(matcher, expected) {
this.to(matcher, expected, true);
}
}
},
equal: {
match: function(expected, actual) {
if (expected instanceof Array)
return Screw.Matchers.array_equal.match(actual, expected);
else
return actual == expected;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $([actual]).print() + (not ? ' to not equal ' : ' to equal ') + $([expected]).print();
}
},
array_equal: {
match: function(expected, actual) {
for(var i = 0; i < actual.length; i++)
if (actual[i] != expected[i]) { return false }
return actual.length == expected.length;
}
},
be_empty: {
match: function(expected, actual) {
if(actual.length == undefined) {
throw(actual.toString() + " does not respond to length");
}
return actual.length == 0;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $([actual]).print() + (not ? ' to not be empty' : ' to be empty');
}
}
}
};