MASViewConstraintSpec.m
10.7 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// MASViewConstraintSpec.m
// Masonry
//
// Created by Jonas Budelmann on 21/07/13.
// Copyright (c) 2013 cloudling. All rights reserved.
//
#import "MASViewConstraint.h"
#import "MASConstraint.h"
#import "UIView+MASAdditions.h"
#import "MASConstraintDelegateMock.h"
#import "MASCompositeConstraint.h"
@interface MASViewConstraint ()
@property (nonatomic, assign) NSLayoutRelation layoutRelation;
@property (nonatomic, assign) MASLayoutPriority layoutPriority;
@property (nonatomic, assign) CGFloat layoutMultiplier;
@property (nonatomic, assign) CGFloat layoutConstant;
@property (nonatomic, assign) BOOL hasLayoutRelation;
@end
@interface MASCompositeConstraint () <MASConstraintDelegate>
@property (nonatomic, strong) NSMutableArray *childConstraints;
@end
SpecBegin(MASViewConstraint)
__block MASConstraintDelegateMock *delegate;
__block UIView *superview;
__block MASViewConstraint *constraint;
__block UIView *otherView;
beforeEach(^{
superview = UIView.new;
delegate = MASConstraintDelegateMock.new;
UIView *view = UIView.new;
constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_width];
constraint.delegate = delegate;
[superview addSubview:view];
otherView = UIView.new;
[superview addSubview:otherView];
});
describe(@"create equality constraint", ^{
it(@"should create equal constraint", ^{
MASViewAttribute *secondViewAttribute = otherView.mas_top;
MASViewConstraint *newConstraint = (id)constraint.equalTo(secondViewAttribute);
expect(newConstraint).to.beIdenticalTo(constraint);
expect(constraint.secondViewAttribute).to.beIdenticalTo(secondViewAttribute);
expect(constraint.layoutRelation).to.equal(NSLayoutRelationEqual);
});
it(@"should create greaterThanOrEqual constraint", ^{
MASViewAttribute *secondViewAttribute = otherView.mas_top;
MASViewConstraint *newConstraint = (id)constraint.greaterThanOrEqualTo(secondViewAttribute);
expect(newConstraint).to.beIdenticalTo(constraint);
expect(constraint.secondViewAttribute).to.beIdenticalTo(secondViewAttribute);
expect(constraint.layoutRelation).to.equal(NSLayoutRelationGreaterThanOrEqual);
});
it(@"create lessThanOrEqual constraint", ^{
MASViewAttribute *secondViewAttribute = otherView.mas_top;
MASViewConstraint *newConstraint = (id)constraint.lessThanOrEqualTo(secondViewAttribute);
expect(newConstraint).to.beIdenticalTo(constraint);
expect(constraint.secondViewAttribute).to.beIdenticalTo(secondViewAttribute);
expect(constraint.layoutRelation).to.equal(NSLayoutRelationLessThanOrEqual);
});
it(@"should not allow update of equal", ^{
MASViewAttribute *secondViewAttribute = otherView.mas_top;
constraint.lessThanOrEqualTo(secondViewAttribute);
expect(^{
constraint.equalTo(secondViewAttribute);
}).to.raise(@"NSInternalInconsistencyException");
});
it(@"should not allow update of lessThanOrEqual", ^{
MASViewAttribute *secondViewAttribute = otherView.mas_top;
constraint.equalTo(secondViewAttribute);
expect(^{
constraint.lessThanOrEqualTo(secondViewAttribute);
}).to.raise(@"NSInternalInconsistencyException");
});
it(@"should not allow update of greaterThanOrEqual", ^{
MASViewAttribute *secondViewAttribute = otherView.mas_top;
constraint.greaterThanOrEqualTo(secondViewAttribute);
expect(^{
constraint.greaterThanOrEqualTo(secondViewAttribute);
}).to.raise(@"NSInternalInconsistencyException");
});
it(@"should accept view object", ^{
UIView *view = UIView.new;
constraint.equalTo(view);
expect(constraint.secondViewAttribute.view).to.beIdenticalTo(view);
expect(constraint.firstViewAttribute.layoutAttribute).to.equal(constraint.secondViewAttribute.layoutAttribute);
});
it(@"should create composite when passed array of views", ^{
NSArray *views = @[UIView.new, UIView.new, UIView.new];
[delegate.constraints addObject:constraint];
MASCompositeConstraint *composite = (id)constraint.equalTo(views).priorityMedium().offset(-10);
expect(delegate.constraints).to.haveCountOf(1);
expect(delegate.constraints[0]).to.beKindOf(MASCompositeConstraint.class);
for (MASViewConstraint *constraint in composite.childConstraints) {
int index = [composite.childConstraints indexOfObject:constraint];
expect(constraint.secondViewAttribute.view).to.beIdenticalTo(views[index]);
expect(constraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
expect(constraint.secondViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
expect(constraint.layoutPriority).to.equal(MASLayoutPriorityDefaultMedium);
expect(constraint.layoutConstant).to.equal(-10);
}
});
it(@"should create composite when passed array of attributes", ^{
NSArray *viewAttributes = @[UIView.new.mas_height, UIView.new.mas_left];
[delegate.constraints addObject:constraint];
MASCompositeConstraint *composite = (id)constraint.equalTo(viewAttributes).priority(60).offset(10);
expect(delegate.constraints).to.haveCountOf(1);
expect(delegate.constraints[0]).to.beKindOf(MASCompositeConstraint.class);
for (MASViewConstraint *constraint in composite.childConstraints) {
int index = [composite.childConstraints indexOfObject:constraint];
expect(constraint.secondViewAttribute.view).to.beIdenticalTo([viewAttributes[index] view]);
expect(constraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
expect(constraint.secondViewAttribute.layoutAttribute).to.equal([viewAttributes[index] layoutAttribute]);
expect(constraint.layoutPriority).to.equal(60);
expect(constraint.layoutConstant).to.equal(10);
}
});
});
describe(@"multiplier & constant", ^{
it(@"should not allow update of multiplier after layoutconstraint is created", ^{
[constraint installConstraint];
expect(^{
constraint.percent(0.9);
}).to.raise(@"NSInternalInconsistencyException");
});
it(@"should allow update of constant after layoutconstraint is created", ^{
[constraint installConstraint];
constraint.offset(10);
expect(constraint.layoutConstant).to.equal(10);
expect(constraint.layoutConstraint.constant).to.equal(10);
});
it(@"should update sides offset only", ^{
MASViewConstraint *centerY = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_centerY];
centerY.insets(UIEdgeInsetsMake(10, 10, 10, 10));
expect(centerY.layoutConstant).to.equal(0);
MASViewConstraint *top = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_top];
top.insets(UIEdgeInsetsMake(15, 10, 10, 10));
expect(top.layoutConstant).to.equal(15);
MASViewConstraint *left = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_left];
left.insets(UIEdgeInsetsMake(10, 15, 10, 10));
expect(left.layoutConstant).to.equal(15);
MASViewConstraint *bottom = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_bottom];
bottom.insets(UIEdgeInsetsMake(10, 10, 15, 10));
expect(bottom.layoutConstant).to.equal(-15);
MASViewConstraint *right = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_right];
right.insets(UIEdgeInsetsMake(10, 10, 10, 15));
expect(right.layoutConstant).to.equal(-15);
});
it(@"should update center offset only", ^{
MASViewConstraint *width = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_width];
width.centerOffset(CGPointMake(-20, -10));
expect(width.layoutConstant).to.equal(0);
MASViewConstraint *centerX = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_centerX];
centerX.centerOffset(CGPointMake(-20, -10));
expect(centerX.layoutConstant).to.equal(-20);
MASViewConstraint *centerY = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_centerY];
centerY.centerOffset(CGPointMake(-20, -10));
expect(centerY.layoutConstant).to.equal(-10);
});
it(@"should update size offset only", ^{
MASViewConstraint *bottom = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_bottom];
bottom.sizeOffset(CGSizeMake(-40, 55));
expect(bottom.layoutConstant).to.equal(0);
MASViewConstraint *width = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_width];
width.sizeOffset(CGSizeMake(-40, 55));
expect(width.layoutConstant).to.equal(-40);
MASViewConstraint *height = [[MASViewConstraint alloc] initWithFirstViewAttribute:otherView.mas_height];
height.sizeOffset(CGSizeMake(-40, 55));
expect(height.layoutConstant).to.equal(55);
});
});
describe(@"install", ^{
it(@"should create layout constraint on commit", ^{
MASViewAttribute *secondViewAttribute = otherView.mas_height;
constraint.equalTo(secondViewAttribute);
constraint.percent(0.5);
constraint.offset(10);
constraint.priority(345);
[constraint installConstraint];
expect(constraint.layoutConstraint.firstAttribute).to.equal(NSLayoutAttributeWidth);
expect(constraint.layoutConstraint.secondAttribute).to.equal(NSLayoutAttributeHeight);
expect(constraint.layoutConstraint.firstItem).to.beIdenticalTo(constraint.firstViewAttribute.view);
expect(constraint.layoutConstraint.secondItem).to.beIdenticalTo(constraint.secondViewAttribute.view);
expect(constraint.layoutConstraint.relation).to.equal(NSLayoutRelationEqual);
expect(constraint.layoutConstraint.constant).to.equal(10);
expect(constraint.layoutConstraint.priority).to.equal(345);
expect(constraint.layoutConstraint.multiplier).to.equal(0.5);
expect(superview.constraints[0]).to.beIdenticalTo(constraint.layoutConstraint);
});
it(@"should uninstall constraint", ^{
MASViewAttribute *secondViewAttribute = otherView.mas_height;
constraint.equalTo(secondViewAttribute);
[constraint installConstraint];
expect(superview.constraints).to.haveCountOf(1);
expect(superview.constraints[0]).to.equal(constraint.layoutConstraint);
[constraint uninstallConstraint];
expect(superview.constraints).to.haveCountOf(0);
});
});
SpecEnd