forked from kiwi-bdd/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathKWNotificationMatcherTest.m
135 lines (114 loc) · 6.62 KB
/
KWNotificationMatcherTest.m
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
//
// Licensed under the terms in License.txt
//
// Copyright 2010 Allen Ding. All rights reserved.
//
#import "Kiwi.h"
#import "KiwiTestConfiguration.h"
#import "TestClasses.h"
#if KW_TESTS_ENABLED
@interface KWNotificatoinMatcherTest : SenTestCase
@end
@implementation KWNotificatoinMatcherTest
- (void)testItShouldHaveTheRightMatcherStrings {
NSArray *matcherStrings = [KWNotificationMatcher matcherStrings];
NSArray *expectedStrings = @[@"bePosted", @"bePostedWithObject:", @"bePostedWithUserInfo:", @"bePostedWithObject:andUserInfo:", @"bePostedEvaluatingBlock:"];
STAssertEqualObjects([matcherStrings sortedArrayUsingSelector:@selector(compare:)],
[expectedStrings sortedArrayUsingSelector:@selector(compare:)],
@"expected specific matcher strings");
}
- (void)testEachMatcherStringIsANameOfAMatchingMethod {
NSArray *matcherStrings = [KWNotificationMatcher matcherStrings];
for (NSString *string in matcherStrings) {
STAssertTrue([KWNotificationMatcher instancesRespondToSelector:NSSelectorFromString(string)], @"expect to find an instance method: %@", string);
}
}
- (void)testItShouldMatchPostedNotificationWithAnyObject {
id object = [[[NSObject alloc] init] autorelease];
id subject = @"MyNotification";
id matcher = [KWNotificationMatcher matcherWithSubject:subject];
[matcher bePosted];
[[NSNotificationCenter defaultCenter] postNotificationName:subject object:object];
STAssertTrue([matcher evaluate], @"expected positive match");
}
- (void)testItShouldMatchPostedNotificationWithMatchingObject {
id object = [[[NSObject alloc] init] autorelease];
id subject = @"MyNotification";
id matcher = [KWNotificationMatcher matcherWithSubject:subject];
[matcher bePostedWithObject:object];
[[NSNotificationCenter defaultCenter] postNotificationName:subject object:object];
STAssertTrue([matcher evaluate], @"expected positive match");
}
- (void)testItShouldMatchPostedNotificationWithMatchingUserInfo {
id object = [[[NSObject alloc] init] autorelease];
id subject = @"MyNotification";
id matcher = [KWNotificationMatcher matcherWithSubject:subject];
[matcher bePostedWithUserInfo:@{@"a":@"b", @1:@2}];
[[NSNotificationCenter defaultCenter] postNotificationName:subject object:object userInfo:@{@"a":@"b", @1:@2}];
STAssertTrue([matcher evaluate], @"expected positive match");
}
- (void)testItShouldMatchPostedNotificationWithMatchingObjectAndUserInfo {
id object = [[[NSObject alloc] init] autorelease];
id subject = @"MyNotification";
id matcher = [KWNotificationMatcher matcherWithSubject:subject];
[matcher bePostedWithObject:object andUserInfo:@{@"a":@"b", @1:@2}];
[[NSNotificationCenter defaultCenter] postNotificationName:subject object:object userInfo:@{@"a":@"b", @1:@2}];
STAssertTrue([matcher evaluate], @"expected positive match");
}
- (void)testItShouldMatchPostedNotificationEvaluatingBlock {
id object = [[[NSObject alloc] init] autorelease];
id subject = @"MyNotification";
id matcher = [KWNotificationMatcher matcherWithSubject:subject];
[matcher bePostedEvaluatingBlock:^(NSNotification *note) {
STAssertEquals(subject, [note name], @"expected notification name");
STAssertEquals(object, [note object], @"expected notification object");
STAssertNotNil([note userInfo][@"PATH"], @"expected notification user info to include environment PATH");
}];
[[NSNotificationCenter defaultCenter] postNotificationName:subject object:object userInfo:[[NSProcessInfo processInfo] environment]];
STAssertTrue([matcher evaluate], @"expected positive match");
}
- (void)testItShouldNotMatchWhenNothingHaveBeenPosted {
id subject = @"MyNotification";
id matcher = [KWNotificationMatcher matcherWithSubject:subject];
[matcher bePosted];
STAssertFalse([matcher evaluate], @"expected negative match");
}
- (void)testItShouldHaveHumanReadableDescription {
id matcher = [KWNotificationMatcher matcherWithSubject:NSPortDidBecomeInvalidNotification];
[matcher bePosted];
STAssertEqualObjects(@"NSPortDidBecomeInvalidNotification be posted", [matcher description], @"description should match");
}
- (void)testItShouldHaveInformativeFailureMessageForShould {
id matcher = [KWNotificationMatcher matcherWithSubject:NSPortDidBecomeInvalidNotification];
[matcher bePosted];
STAssertEqualObjects([matcher failureMessageForShould], @"expect to receive \"NSPortDidBecomeInvalidNotification\" notification", @"failure message should match");
}
- (void)testItShouldHaveInformativeFailureMessageForShouldWithObject {
id matcher = [KWNotificationMatcher matcherWithSubject:NSPortDidBecomeInvalidNotification];
[matcher bePostedWithObject:@"sender"];
STAssertEqualObjects([matcher failureMessageForShould], @"expect to receive \"NSPortDidBecomeInvalidNotification\" "
"notification with object: sender", @"failure message should match");
}
- (void)testItShouldHaveInformativeFailureMessageForShouldWithUserInfo {
id matcher = [KWNotificationMatcher matcherWithSubject:NSPortDidBecomeInvalidNotification];
[matcher bePostedWithUserInfo:@{@"message":@"text"}];
STAssertEqualObjects([matcher failureMessageForShould], @"expect to receive \"NSPortDidBecomeInvalidNotification\" "
"notification with user info: {\n message = text;\n}", @"failure message should match");
}
- (void)testItShouldHaveInformativeFailureMessageForShouldWithObjectAndUserInfo {
id matcher = [KWNotificationMatcher matcherWithSubject:NSPortDidBecomeInvalidNotification];
[matcher bePostedWithObject:@"sender" andUserInfo:@{@"message":@"text"}];
STAssertEqualObjects([matcher failureMessageForShould], @"expect to receive \"NSPortDidBecomeInvalidNotification\" "
"notification with object: sender and user info: {\n message = text;\n}", @"failure message should match");
}
- (void)testItShouldHaveInformativeFailureMessageForShouldNot {
id matcher = [KWNotificationMatcher matcherWithSubject:@"MyNotification"];
[matcher bePosted];
NSNotification *notification = [[NSNotification alloc] initWithName:@"MyNotification" object:self userInfo:@{@"when":@"today"}];
NSString *description = [notification description];
[[NSNotificationCenter defaultCenter] postNotification:notification];
NSString *expectedDescription = [NSString stringWithFormat:@"expect not to receive \"MyNotification\" notification, but received: %@", description];
STAssertEqualObjects([matcher failureMessageForShouldNot], expectedDescription, @"failure message should match");
}
@end
#endif // #if KW_TESTS_ENABLED