Skip to content

Commit 7b7844c

Browse files
committed
Add unit test for mixed nesting of batch creation methods
Since both +[NSLayoutConstraint autoCreateAndInstallConstraints:] and +[NSLayoutConstraint autoCreateConstraintsWithoutInstalling:] methods rely on the same underlying data structure, add a unit test to ensure that nesting them both together works correctly.
1 parent 2bd241c commit 7b7844c

1 file changed

Lines changed: 53 additions & 4 deletions

File tree

PureLayout/PureLayoutTests/PureLayoutBatchTests.m

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ - (BOOL)noConstraintsAreActivated:(__NSArray_of(NSLayoutConstraint *) *)constrai
7474
}
7575

7676
/**
77-
Test the +[autoCreateAndInstallConstraints:] method on UIView/NSView.
77+
Test the +[NSLayoutConstraint autoCreateAndInstallConstraints:] method.
7878
*/
7979
- (void)testCreateAndInstallConstraints
8080
{
@@ -93,7 +93,7 @@ - (void)testCreateAndInstallConstraints
9393
}
9494

9595
/**
96-
Test nested calls to the +[autoCreateAndInstallConstraints:] method on UIView/NSView.
96+
Test nested calls to the +[NSLayoutConstraint autoCreateAndInstallConstraints:] method.
9797
- Constraints should be returned ONLY by the call that provided the immediate enclosing block.
9898
- Calls whose block contains other call(s) should not return constraints from within the blocks of nested call(s).
9999
- Constraints should be active when they are returned.
@@ -145,7 +145,7 @@ - (void)testCreateAndInstallConstraintsNested
145145
}
146146

147147
/**
148-
Test the +[autoCreateConstraintsWithoutInstalling:] method on UIView/NSView.
148+
Test the +[NSLayoutConstraint autoCreateConstraintsWithoutInstalling:] method.
149149
*/
150150
- (void)testCreateConstraintsWithoutInstalling
151151
{
@@ -164,7 +164,7 @@ - (void)testCreateConstraintsWithoutInstalling
164164
}
165165

166166
/**
167-
Test nested calls to the +[autoCreateConstraintsWithoutInstalling:] method on UIView/NSView.
167+
Test nested calls to the +[NSLayoutConstraint autoCreateConstraintsWithoutInstalling:] method.
168168
- Constraints should be returned ONLY by the call that provided the immediate enclosing block.
169169
- Calls whose block contains other call(s) should not return constraints from within the blocks of nested call(s).
170170
- Constraints should never be active.
@@ -215,4 +215,53 @@ - (void)testCreateConstraintsWithoutInstallingNested
215215
XCTAssert([self noConstraintsAreActivated:returnedConstraints1_2]);
216216
}
217217

218+
/**
219+
Test nested calls to both +[NSLayoutConstraint autoCreateAndInstallConstraints:] and +[NSLayoutConstraint autoCreateConstraintsWithoutInstalling:] methods.
220+
*/
221+
- (void)testBothBatchCreateMethodsNested
222+
{
223+
__block __NSArray_of(NSLayoutConstraint *) *returnedConstraints1_1 = nil;
224+
__block __NSArray_of(NSLayoutConstraint *) *returnedConstraints1_1_1 = nil;
225+
__block __NSArray_of(NSLayoutConstraint *) *returnedConstraints1_2 = nil;
226+
227+
__NSArray_of(NSLayoutConstraint *) *returnedConstraints1 = [NSLayoutConstraint autoCreateConstraintsWithoutInstalling:^{
228+
[self.viewA autoPinEdgeToSuperviewEdge:ALEdgeTop];
229+
230+
returnedConstraints1_1 = [NSLayoutConstraint autoCreateAndInstallConstraints:^{
231+
[self.viewB autoSetDimension:ALDimensionWidth toSize:100.0];
232+
[self.viewC autoSetDimension:ALDimensionHeight toSize:100.0];
233+
234+
returnedConstraints1_1_1 = [NSLayoutConstraint autoCreateConstraintsWithoutInstalling:^{
235+
[self.viewD autoConstrainAttribute:ALAttributeHeight toAttribute:ALAttributeWidth ofView:self.viewC withMultiplier:2.0];
236+
}];
237+
XCTAssertEqual(returnedConstraints1_1_1.count, 1);
238+
XCTAssert([self noConstraintsAreActivated:returnedConstraints1_1_1]);
239+
240+
[self.viewB autoSetDimension:ALDimensionHeight toSize:100.0];
241+
[self.viewC autoSetDimension:ALDimensionWidth toSize:100.0];
242+
}];
243+
XCTAssertEqual(returnedConstraints1_1.count, 4);
244+
XCTAssert([self allConstraintsAreActivated:returnedConstraints1_1]);
245+
246+
[self.viewA autoPinEdgeToSuperviewEdge:ALEdgeLeading];
247+
248+
returnedConstraints1_2 = [NSLayoutConstraint autoCreateAndInstallConstraints:^{
249+
[self.viewA autoPinEdgeToSuperviewEdge:ALEdgeBottom];
250+
[self.viewA autoAlignAxisToSuperviewAxis:ALAxisVertical];
251+
}];
252+
XCTAssertEqual(returnedConstraints1_2.count, 2);
253+
XCTAssert([self allConstraintsAreActivated:returnedConstraints1_2]);
254+
255+
[self.viewA autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
256+
}];
257+
XCTAssertEqual(returnedConstraints1.count, 3);
258+
XCTAssert([self noConstraintsAreActivated:returnedConstraints1]);
259+
XCTAssertEqual(returnedConstraints1_1.count, 4);
260+
XCTAssert([self allConstraintsAreActivated:returnedConstraints1_1]);
261+
XCTAssertEqual(returnedConstraints1_1_1.count, 1);
262+
XCTAssert([self noConstraintsAreActivated:returnedConstraints1_1_1]);
263+
XCTAssertEqual(returnedConstraints1_2.count, 2);
264+
XCTAssert([self allConstraintsAreActivated:returnedConstraints1_2]);
265+
}
266+
218267
@end

0 commit comments

Comments
 (0)