1
+ // RUN: %{swiftc} %s -o %{built_tests_dir}/Notifications
2
+ // RUN: %{built_tests_dir}/Notifications > %t || true
3
+ // RUN: %{xctest_checker} %t %s
4
+
5
+ #if os(Linux) || os(FreeBSD)
6
+ import XCTest
7
+ import Foundation
8
+ #else
9
+ import SwiftXCTest
10
+ import SwiftFoundation
11
+ #endif
12
+
13
+ class NotificationExpectationsTestCase : XCTestCase {
14
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithName_passes' started.
15
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithName_passes' passed \(\d+\.\d+ seconds\).
16
+ func test_observeNotificationWithName_passes( ) {
17
+ let notificationName = " notificationWithNameTest "
18
+ expectationForNotification ( notificationName, object: nil , handler: nil )
19
+ NSNotificationCenter . defaultCenter ( ) . postNotificationName ( notificationName, object: nil )
20
+ waitForExpectationsWithTimeout ( 0.0 , handler: nil )
21
+ }
22
+
23
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithNameAndObject_passes' started.
24
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithNameAndObject_passes' passed \(\d+\.\d+ seconds\).
25
+ func test_observeNotificationWithNameAndObject_passes( ) {
26
+ let notificationName = " notificationWithNameAndObjectTest "
27
+ let dummyObject = NSObject ( )
28
+ expectationForNotification ( notificationName, object: dummyObject, handler: nil )
29
+ NSNotificationCenter . defaultCenter ( ) . postNotificationName ( notificationName, object: dummyObject)
30
+ waitForExpectationsWithTimeout ( 0.0 , handler: nil )
31
+ }
32
+
33
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithNameAndObject_butExpectingNoObject_passes' started.
34
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithNameAndObject_butExpectingNoObject_passes' passed \(\d+\.\d+ seconds\).
35
+ func test_observeNotificationWithNameAndObject_butExpectingNoObject_passes( ) {
36
+ let notificationName = " notificationWithNameAndObject_expectNoObjectTest "
37
+ expectationForNotification ( notificationName, object: nil , handler: nil )
38
+ let dummyObject = NSObject ( )
39
+ NSNotificationCenter . defaultCenter ( ) . postNotificationName ( notificationName, object: dummyObject)
40
+ waitForExpectationsWithTimeout ( 0.0 , handler: nil )
41
+ }
42
+
43
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithIncorrectName_fails' started.
44
+ // CHECK: .*/Tests/Functional/Asynchronous/Notifications/Expectations/main.swift:49: error: NotificationExpectationsTestCase.test_observeNotificationWithIncorrectName_fails : Asynchronous wait failed - Exceeded timeout of 0.1 seconds, with unfulfilled expectations: Expect notification 'expectedName' from any object
45
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithIncorrectName_fails' failed \(\d+\.\d+ seconds\).
46
+ func test_observeNotificationWithIncorrectName_fails( ) {
47
+ expectationForNotification ( " expectedName " , object: nil , handler: nil )
48
+ NSNotificationCenter . defaultCenter ( ) . postNotificationName ( " actualName " , object: nil )
49
+ waitForExpectationsWithTimeout ( 0.1 , handler: nil )
50
+ }
51
+
52
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithIncorrectObject_fails' started.
53
+ // CHECK: .*/Tests/Functional/Asynchronous/Notifications/Expectations/main.swift:61: error: NotificationExpectationsTestCase.test_observeNotificationWithIncorrectObject_fails : Asynchronous wait failed - Exceeded timeout of 0.1 seconds, with unfulfilled expectations: Expect notification 'notificationWithIncorrectObjectTest' from dummyObject
54
+ // CHECK: Test Case 'NotificationExpectationsTestCase.test_observeNotificationWithIncorrectObject_fails' failed \(\d+\.\d+ seconds\).
55
+ func test_observeNotificationWithIncorrectObject_fails( ) {
56
+ let notificationName = " notificationWithIncorrectObjectTest "
57
+ let dummyObject : NSString = " dummyObject "
58
+ let anotherDummyObject = NSObject ( )
59
+ expectationForNotification ( notificationName, object: dummyObject, handler: nil )
60
+ NSNotificationCenter . defaultCenter ( ) . postNotificationName ( notificationName, object: anotherDummyObject)
61
+ waitForExpectationsWithTimeout ( 0.1 , handler: nil )
62
+ }
63
+
64
+ static var allTests : [ ( String , NotificationExpectationsTestCase -> ( ) throws -> Void ) ] {
65
+ return [
66
+ ( " test_observeNotificationWithName_passes " , test_observeNotificationWithName_passes) ,
67
+ ( " test_observeNotificationWithNameAndObject_passes " , test_observeNotificationWithNameAndObject_passes) ,
68
+ ( " test_observeNotificationWithNameAndObject_butExpectingNoObject_passes " , test_observeNotificationWithNameAndObject_butExpectingNoObject_passes) ,
69
+ ( " test_observeNotificationWithIncorrectName_fails " , test_observeNotificationWithIncorrectName_fails) ,
70
+ ( " test_observeNotificationWithIncorrectObject_fails " , test_observeNotificationWithIncorrectObject_fails) ,
71
+ ]
72
+ }
73
+ }
74
+
75
+ XCTMain ( [ testCase ( NotificationExpectationsTestCase . allTests) ] )
76
+
77
+ // CHECK: Executed 5 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
78
+ // CHECK: Total executed 5 tests, with 2 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
0 commit comments