This repository was archived by the owner on Nov 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_spec.py
199 lines (139 loc) · 6.13 KB
/
spec_spec.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from spec import describe, done, Target
#
# Specifies and test-runs the SPEC framework using itself
#
# ------------------------------------------------------------------------------
# Mock for verifying how the result printer is used by the test runner
# (and for avoiding STDOUT-interference with the system-under-test)
class MockResult(object):
'''Mocks the interface used internally for reporting'''
def enter_feature(self, feature):
self.entered_feature = feature
def enter_case(self, case):
self.entered_case = case
def success(self, target):
self.succeeded_target = target
def failure(self, target):
self.failed_target = target
def finish(self):
self.finished = True
result = MockResult()
# ------------------------------------------------------------------------------
# Define meta-expectations over an expectation
def succeed(self):
self.do_assertion(self.subject.done and self.subject.success, "succeed")
def fail(self):
self.do_assertion(self.subject.done and not self.subject.success, "fail")
# Extend the Target class
Target.succeed = succeed
Target.fail = fail
# ------------------------------------------------------------------------------
# The example from the readme file
print "START OF EXAMPLE"
# the unit under test
def factorial(n):
if n < 0:
raise ValueError, "n must be non-negative"
else:
return n * factorial(n - 1) if n else 1
# the spec
with describe("The factorial function") as it:
with it("is defined at zero") as then:
then(factorial(0)).should.be(1)
with it("rejects a negative argument") as then:
then(lambda: factorial(-1)).should.throw(ValueError)
with it("yields 720 if input is 6") as then:
then(factorial(6)).should.be(720)
with it("grows fast") as then:
then(factorial(100)).should > 100000
done(exit = False)
print "END OF EXAMPLE"
print
# ------------------------------------------------------------------------------
with describe("A Feature") as it:
with it("is obtained by calling 'describe'") as then:
desc = describe("a feature", result)
then(desc.what).should.be("a feature")
with it("is a context handler") as then:
then(desc).should.have('__enter__')
then(desc).should.have('__exit__')
with it("reports entering its with-context") as then:
with desc:
then(result.entered_feature).should.be(desc)
with it("is callable") as then:
then(callable(desc)).should.hold()
with describe("A Test Case") as it:
with it("is obtained by calling a feature") as then:
case = desc("behaves properly")
then(case.how_to_behave).should.be("behaves properly")
with it("is also a context handler") as then:
then(case).should.have('__enter__')
then(case).should.have('__exit__')
with it("reports entering its with-context") as then:
with case:
then(result.entered_case).should.be(case)
with it("is callable") as then:
then(callable(desc)).should.hold()
with describe("A Test Target") as it:
with it("is obtained by calling a test case") as then:
target = case(42)
with it("stores the subject") as then:
then(target.subject).should.be(42)
with it("generates a proxy on calling 'should'") as then:
proxy = target.should
then(proxy.subject).should.be(42)
then(proxy.negated).should_not.hold()
with it("generates a negated proxy on calling 'should_not'") as then:
proxy = target.should_not
then(proxy.subject).should.be(42)
then(proxy.negated).should.hold()
with it("does not report before any assertion") as then:
then(result).should_not.have('succeeded_target')
then(result).should_not.have('failed_target')
with it("reports succeeding assertions") as then:
target.should.be(42)
then(result.succeeded_target.subject).should.be(42)
then(result.succeeded_target.verb).should.be('be')
then(result.succeeded_target.object).should.be(42)
with it("reports failing assertions") as then:
target.should.be(21)
then(result.failed_target.subject).should.be(42)
then(result.failed_target.verb).should.be('be')
then(result.failed_target.object).should.be(21)
with it("reports negated failing assertions as success") as then:
target.should_not.be(21)
then(result.succeeded_target.subject).should.be(42)
then(result.succeeded_target.verb).should.be('be')
then(result.succeeded_target.object).should.be(21)
with describe("A Test Target's assertions") as it:
with it("check equality") as then:
then(case(42).should.be(42)).should.succeed()
then(case(42).should.be(21)).should.fail()
with it("check less (or equal)") as then:
then(case(42).should < 43).should.succeed()
then(case(42).should < 42).should.fail()
then(case(42).should <= 42).should.succeed()
with it("check greater (or equal)") as then:
then(case(42).should > 41).should.succeed()
then(case(42).should > 42).should.fail()
then(case(42).should >= 42).should.succeed()
with it("check truth") as then:
then(case(True).should.hold()).should.succeed()
then(case(False).should.hold()).should.fail()
with it("duck-type") as then:
# beware of meta!
then(case(then(42)).should.have('should')).should.succeed()
then(case(then(42)).should.have('random crap')).should.fail()
with it("find an element in a collection") as then:
then(case([1, 2, 3]).should.contain(1)).should.succeed()
then(case([1, 2, 3]).should.contain(4)).should.fail()
with it("check for exceptions"):
def epic_fail():
raise ValueError
then(case(epic_fail).should.throw(ValueError)).should.succeed()
then(case(epic_fail).should.throw(MemoryError)).should.fail()
with it("match regular expressions"):
then(case('hello world').should.match('w.rld')).should.succeed()
then(case('hello world').should.match('^hello$')).should.fail()
if __name__ == '__main__':
done()