/
CPObject+Specs.j
119 lines (101 loc) · 3.41 KB
/
CPObject+Specs.j
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
SpecFailedException = "specFailedException";
var currentPreFn, currentPostFn;
// in case capp is outdated
if (! CPSelectorFromString) {
var CPSelectorFromString = function(sel) { return sel; };
}
if (! CPStringFromSelector) {
var CPStringFromSelector = function(sel) { return sel; };
}
@implementation CPObject (Specs)
/**
* Registers a given matcher class. By default, this converts the class name
* into a selector with one parameter (e.g., OSShouldBeInstanceOf becomes
* shouldBeInstanceOf:) that invokes the matches: method on a new instance of
* the given class.
*
* If you need more than just one parameter, or if you need no parameters, use
* registerMatcher:withSelector:.
*/
+ (void)registerMatcher:(Class)matcherClass
{
var name = class_getName(matcherClass);
var selName = name.substr(2, 1).toLowerCase() + name.substr(3) + ":";
selName = selName.replace(/Matcher/, "");
[self registerMatcher:matcherClass
withSelector:CPSelectorFromString(selName)];
}
/**
* Registers a given matcher class.
*/
+ (void)registerMatcher:(Class)matcherClass withSelector:(SEL)matcherSelector
{
if (class_getInstanceMethod(matcherClass, matcherSelector))
{
[CPException raise:OSMethodExistsException
reason:"Registering matcher with selector " + CPStringFromSelector(matcherSelector) +
" but that selector is already registered on class " +
class_getName(matcherClass)]
}
var selString = CPStringFromSelector(matcherSelector),
expectsMultipleArgs = (matcherSelector.split(":").length > 2);
class_addMethods(CPObject,
[new objj_method(matcherSelector,
function(self, _cmd, expected)
{
[[[matcherClass alloc] initWithExpected:expected] matches:self]
},
"")]);
}
/**
*
* @param specDescription A description of what this spec is testing. The
* description is automatically prefixed with ``should''.
* @param by A block of code to run for this spec. It should make calls to the
* shouldEqual: or shouldNotEqual: methods.
*/
+ (void)should:(CPString)specDescription by:(Function)specFn
{
var currentContext = {};
for (var prop in this)
currentContext[prop] = this[prop];
try
{
[Mock resetMockTracking]
if (currentPreFn) currentPreFn.call(currentContext);
specFn.call(currentContext);
if (currentPostFn) currentPostFn.call(currentContext);
[Mock freezeMockTracking]
[self ensureMockExpectationsWereMet]
[Test addSuccess:specDescription]
}
catch (obj)
{
if (obj.specFailure == SpecFailedException)
[Test addFailure:specDescription]
else
[Test addFailure:specDescription fromException:obj]
}
}
+ (void)should:(CPString)specDescription
{
[Test addResult:@"pending" forSpec:specDescription]
}
+ (void)setCurrentPre:(Function)preFn andPost:(Function)postFn
{
currentPreFn = preFn;
currentPostFn = postFn;
}
/**
* Throws an exception if the expectations of any mocks set up in the beforeEach
* block are not met.
*/
+ (void)ensureMockExpectationsWereMet
{
[Mock ensureTrackedExpectationsWereMet]
}
@end
@import "Matchers/OSShouldBeInstanceOf.j"
@import "Matchers/OSShouldBeNil.j"
@import "Matchers/OSShouldBeSameAs.j"
@import "Matchers/OSShouldEqual.j"