@@ -29,7 +29,11 @@ class TestNSAffineTransform : XCTestCase {
29
29
var allTests : [ ( String , ( ) -> ( ) ) ] {
30
30
return [
31
31
( " test_BasicConstruction " , test_BasicConstruction) ,
32
- ( " test_IdentityTransformation " , test_IdentityTransformation)
32
+ ( " test_IdentityTransformation " , test_IdentityTransformation) ,
33
+ ( " test_Translation " , test_Translation) ,
34
+ ( " test_Scale " , test_Scale) ,
35
+ ( " test_Rotation_Degrees " , test_Rotation_Degrees) ,
36
+ ( " test_Rotation_Radians " , test_Rotation_Radians)
33
37
]
34
38
}
35
39
@@ -52,23 +56,108 @@ class TestNSAffineTransform : XCTestCase {
52
56
let identityTransform = NSAffineTransform ( )
53
57
54
58
func checkIdentityPointTransformation( point: NSPoint ) {
55
- let newPoint = identityTransform. transformPoint ( point)
56
- XCTAssertEqualWithAccuracy ( Double ( newPoint. x) , Double ( point. x) , accuracy: accuracyThreshold)
57
- XCTAssertEqualWithAccuracy ( Double ( newPoint. y) , Double ( point. y) , accuracy: accuracyThreshold)
59
+ checkPointTransformation ( identityTransform, point: point, expectedPoint: point)
58
60
}
59
-
61
+
60
62
checkIdentityPointTransformation ( NSPoint ( ) )
61
63
checkIdentityPointTransformation ( NSMakePoint ( CGFloat ( 24.5 ) , CGFloat ( 10.0 ) ) )
62
64
checkIdentityPointTransformation ( NSMakePoint ( CGFloat ( - 7.5 ) , CGFloat ( 2.0 ) ) )
63
65
64
66
func checkIdentitySizeTransformation( size: NSSize ) {
65
- let newSize = identityTransform. transformSize ( size)
66
- XCTAssertEqualWithAccuracy ( Double ( newSize. width) , Double ( size. width) , accuracy: accuracyThreshold)
67
- XCTAssertEqualWithAccuracy ( Double ( newSize. height) , Double ( size. height) , accuracy: accuracyThreshold)
67
+ checkSizeTransformation ( identityTransform, size: size, expectedSize: size)
68
68
}
69
69
70
70
checkIdentitySizeTransformation ( NSSize ( ) )
71
71
checkIdentitySizeTransformation ( NSMakeSize ( CGFloat ( 13.0 ) , CGFloat ( 12.5 ) ) )
72
72
checkIdentitySizeTransformation ( NSMakeSize ( CGFloat ( 100.0 ) , CGFloat ( - 100.0 ) ) )
73
73
}
74
+
75
+ func test_Translation( ) {
76
+ let point = NSPoint ( x: CGFloat ( 0.0 ) , y: CGFloat ( 0.0 ) )
77
+
78
+ let noop = NSAffineTransform ( )
79
+ noop. translateXBy ( CGFloat ( ) , yBy: CGFloat ( ) )
80
+ checkPointTransformation ( noop, point: point, expectedPoint: point)
81
+
82
+ let translateH = NSAffineTransform ( )
83
+ translateH. translateXBy ( CGFloat ( 10.0 ) , yBy: CGFloat ( ) )
84
+ checkPointTransformation ( translateH, point: point, expectedPoint: NSPoint ( x: CGFloat ( 10.0 ) , y: CGFloat ( ) ) )
85
+
86
+ let translateV = NSAffineTransform ( )
87
+ translateV. translateXBy ( CGFloat ( ) , yBy: CGFloat ( 20.0 ) )
88
+ checkPointTransformation ( translateV, point: point, expectedPoint: NSPoint ( x: CGFloat ( ) , y: CGFloat ( 20.0 ) ) )
89
+
90
+ let translate = NSAffineTransform ( )
91
+ translate. translateXBy ( CGFloat ( - 30.0 ) , yBy: CGFloat ( 40.0 ) )
92
+ checkPointTransformation ( translate, point: point, expectedPoint: NSPoint ( x: CGFloat ( - 30.0 ) , y: CGFloat ( 40.0 ) ) )
93
+ }
94
+
95
+ func test_Scale( ) {
96
+ let size = NSSize ( width: CGFloat ( 10.0 ) , height: CGFloat ( 10.0 ) )
97
+
98
+ let noop = NSAffineTransform ( )
99
+ noop. scaleBy ( CGFloat ( 1.0 ) )
100
+ checkSizeTransformation ( noop, size: size, expectedSize: size)
101
+
102
+ let shrink = NSAffineTransform ( )
103
+ shrink. scaleBy ( CGFloat ( 0.5 ) )
104
+ checkSizeTransformation ( shrink, size: size, expectedSize: NSSize ( width: CGFloat ( 5.0 ) , height: CGFloat ( 5.0 ) ) )
105
+
106
+ let grow = NSAffineTransform ( )
107
+ grow. scaleBy ( CGFloat ( 3.0 ) )
108
+ checkSizeTransformation ( grow, size: size, expectedSize: NSSize ( width: CGFloat ( 30.0 ) , height: CGFloat ( 30.0 ) ) )
109
+
110
+ let stretch = NSAffineTransform ( )
111
+ stretch. scaleXBy ( CGFloat ( 2.0 ) , yBy: CGFloat ( 0.5 ) )
112
+ checkSizeTransformation ( stretch, size: size, expectedSize: NSSize ( width: CGFloat ( 20.0 ) , height: CGFloat ( 5.0 ) ) )
113
+ }
114
+
115
+ func test_Rotation_Degrees( ) {
116
+ let point = NSPoint ( x: CGFloat ( 10.0 ) , y: CGFloat ( 10.0 ) )
117
+
118
+ let noop = NSAffineTransform ( )
119
+ noop. rotateByDegrees ( CGFloat ( ) )
120
+ checkPointTransformation ( noop, point: point, expectedPoint: point)
121
+
122
+ let tenEighty = NSAffineTransform ( )
123
+ tenEighty. rotateByDegrees ( CGFloat ( 1080.0 ) )
124
+ checkPointTransformation ( tenEighty, point: point, expectedPoint: point)
125
+
126
+ let reflectAboutOrigin = NSAffineTransform ( )
127
+ reflectAboutOrigin. rotateByDegrees ( CGFloat ( 180.0 ) )
128
+ checkPointTransformation ( reflectAboutOrigin, point: point, expectedPoint: NSPoint ( x: CGFloat ( - 10.0 ) , y: CGFloat ( - 10.0 ) ) )
129
+ }
130
+
131
+ func test_Rotation_Radians( ) {
132
+ let point = NSPoint ( x: CGFloat ( 10.0 ) , y: CGFloat ( 10.0 ) )
133
+
134
+ let noop = NSAffineTransform ( )
135
+ noop. rotateByRadians ( CGFloat ( ) )
136
+ checkPointTransformation ( noop, point: point, expectedPoint: point)
137
+
138
+ let tenEighty = NSAffineTransform ( )
139
+ tenEighty. rotateByRadians ( CGFloat ( 6 * M_PI) )
140
+ checkPointTransformation ( tenEighty, point: point, expectedPoint: point)
141
+
142
+ let reflectAboutOrigin = NSAffineTransform ( )
143
+ reflectAboutOrigin. rotateByRadians ( CGFloat ( M_PI) )
144
+ checkPointTransformation ( reflectAboutOrigin, point: point, expectedPoint: NSPoint ( x: CGFloat ( - 10.0 ) , y: CGFloat ( - 10.0 ) ) )
145
+ }
146
+ }
147
+
148
+
149
+ // Test helper functions
150
+
151
+ private extension TestNSAffineTransform {
152
+ func checkPointTransformation( transform: NSAffineTransform , point: NSPoint , expectedPoint: NSPoint , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
153
+ let newPoint = transform. transformPoint ( point)
154
+ XCTAssertEqualWithAccuracy ( Double ( newPoint. x) , Double ( expectedPoint. x) , accuracy: accuracyThreshold, " x: \( message) " , file: file, line: line)
155
+ XCTAssertEqualWithAccuracy ( Double ( newPoint. y) , Double ( expectedPoint. y) , accuracy: accuracyThreshold, " y: \( message) " , file: file, line: line)
156
+ }
157
+
158
+ func checkSizeTransformation( transform: NSAffineTransform , size: NSSize , expectedSize: NSSize , _ message: String = " " , file: StaticString = __FILE__, line: UInt = __LINE__) {
159
+ let newSize = transform. transformSize ( size)
160
+ XCTAssertEqualWithAccuracy ( Double ( newSize. width) , Double ( expectedSize. width) , accuracy: accuracyThreshold, " width: \( message) " , file: file, line: line)
161
+ XCTAssertEqualWithAccuracy ( Double ( newSize. height) , Double ( expectedSize. height) , accuracy: accuracyThreshold, " height: \( message) " , file: file, line: line)
162
+ }
74
163
}
0 commit comments