MASViewConstraintSpec.m
6.84 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
//
// MASViewConstraintSpec.m
// Masonry
//
// Created by Jonas Budelmann on 21/07/13.
// Copyright (c) 2013 cloudling. All rights reserved.
//
#import "MASViewConstraint.h"
#import "MASConstraint.h"
@interface MASViewConstraint ()
@property (nonatomic, strong) NSLayoutConstraint *layoutConstraint;
@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
SpecBegin(MASViewConstraint)
__block UIView *superview;
__block MASViewConstraint *constraint;
__block id<MASConstraintDelegate> delegate;
__block MASViewAttribute *secondViewAttribute;
beforeEach(^{
superview = mock(UIView.class);
UIView *secondView = mock(UIView.class);
[given(secondView.superview) willReturn:superview];
secondViewAttribute = [[MASViewAttribute alloc] initWithView:secondView layoutAttribute:NSLayoutAttributeHeight];
delegate = mockProtocol(@protocol(MASConstraintDelegate));
UIView *firstView = mock(UIView.class);
[given(firstView.superview) willReturn:superview];
MASViewAttribute *firstViewAttribute = [[MASViewAttribute alloc] initWithView:firstView layoutAttribute:NSLayoutAttributeWidth];
constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:firstViewAttribute];
constraint.delegate = delegate;
});
describe(@"equality chaining", ^{
it(@"should return same constraint when encountering equal for first time", ^{
MASViewConstraint *newConstraint = constraint.equal(secondViewAttribute);
[verify(delegate) addConstraint:(id)constraint];
expect(newConstraint).to.beIdenticalTo(constraint);
expect(constraint.secondViewAttribute).to.beIdenticalTo(secondViewAttribute);
expect(constraint.layoutRelation).to.ex_equal(NSLayoutRelationEqual);
});
it(@"should start new constraint when encountering equal subsequently", ^{
constraint.greaterThanOrEqual(secondViewAttribute);
MASViewConstraint *newConstraint = constraint.equal(secondViewAttribute);
[verify(delegate) addConstraint:(id)constraint];
[verify(delegate) addConstraint:(id)newConstraint];
expect(newConstraint).notTo.beIdenticalTo(constraint);
});
it(@"should return same constraint when encountering greaterThanOrEqual for first time", ^{
MASViewConstraint *newConstraint = constraint.greaterThanOrEqual(secondViewAttribute);
[verify(delegate) addConstraint:(id)constraint];
expect(newConstraint).to.beIdenticalTo(constraint);
expect(constraint.secondViewAttribute).to.beIdenticalTo(secondViewAttribute);
expect(constraint.layoutRelation).to.ex_equal(NSLayoutRelationGreaterThanOrEqual);
});
it(@"should start new constraint when encountering greaterThanOrEqual subsequently", ^{
constraint.lessThanOrEqual(secondViewAttribute);
MASViewConstraint *newConstraint = constraint.greaterThanOrEqual(secondViewAttribute);
[verify(delegate) addConstraint:(id)constraint];
[verify(delegate) addConstraint:(id)newConstraint];
expect(newConstraint).notTo.beIdenticalTo(constraint);
});
it(@"should return same constraint when encountering lessThanOrEqual for first time", ^{
MASViewConstraint *newConstraint = constraint.lessThanOrEqual(secondViewAttribute);
[verify(delegate) addConstraint:(id)constraint];
expect(newConstraint).to.beIdenticalTo(constraint);
expect(constraint.secondViewAttribute).to.beIdenticalTo(secondViewAttribute);
expect(constraint.layoutRelation).to.ex_equal(NSLayoutRelationLessThanOrEqual);
});
it(@"should start new constraint when encountering lessThanOrEqual subsequently", ^{
constraint.equal(secondViewAttribute);
MASViewConstraint *newConstraint = constraint.lessThanOrEqual(secondViewAttribute);
[verify(delegate) addConstraint:(id)constraint];
[verify(delegate) addConstraint:(id)newConstraint];
expect(newConstraint).notTo.beIdenticalTo(constraint);
});
it(@"should not allow update of equal once layoutconstraint is created", ^{
[constraint commit];
expect(^{
constraint.equal(secondViewAttribute);
}).to.raise(@"NSInternalInconsistencyException");
});
it(@"should not allow update of lessThanOrEqual once layoutconstraint is created", ^{
[constraint commit];
expect(^{
constraint.lessThanOrEqual(secondViewAttribute);
}).to.raise(@"NSInternalInconsistencyException");
});
it(@"should not allow update of greaterThanOrEqual once layoutconstraint is created", ^{
[constraint commit];
expect(^{
constraint.greaterThanOrEqual(secondViewAttribute);
}).to.raise(@"NSInternalInconsistencyException");
});
xit(@"should create composite when passed array of views", ^{
});
});
describe(@"multiplier & constant", ^{
it(@"should not allow update of multiplier after layoutconstraint is created", ^{
[constraint commit];
expect(^{
constraint.percent(0.9);
}).to.raise(@"NSInternalInconsistencyException");
});
it(@"should allow update of constant after layoutconstraint is created", ^{
[constraint commit];
constraint.offset(10);
expect(constraint.layoutConstant).to.ex_equal(10);
expect(constraint.layoutConstraint.constant).to.ex_equal(10);
});
xit(@"should update sides only", ^{});
xit(@"should update center only", ^{});
xit(@"should update size only", ^{});
});
describe(@"commit", ^{
it(@"should create layout constraint", ^{
constraint.equal(secondViewAttribute);
constraint.percent(0.5);
constraint.offset(10);
constraint.priority(345);
[constraint commit];
expect(constraint.layoutConstraint.firstAttribute).to.ex_equal(NSLayoutAttributeWidth);
expect(constraint.layoutConstraint.secondAttribute).to.ex_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.ex_equal(NSLayoutRelationEqual);
expect(constraint.layoutConstraint.constant).to.ex_equal(10);
expect(constraint.layoutConstraint.priority).to.ex_equal(345);
expect(constraint.layoutConstraint.multiplier).to.ex_equal(0.5);
[verify(superview) addConstraint:constraint.layoutConstraint];
});
});
SpecEnd