MASCompositeConstraintSpec.m
6.63 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
//
// MASCompositeConstraintSpec.m
// Masonry
//
// Created by Jonas Budelmann on 22/07/13.
// Copyright (c) 2013 cloudling. All rights reserved.
//
#import "MASCompositeConstraint.h"
#import "MASViewConstraint.h"
@interface MASCompositeConstraint () <MASConstraintDelegate>
@property (nonatomic, strong) NSMutableArray *completedChildConstraints;
@property (nonatomic, strong) NSMutableArray *currentChildConstraints;
@property (nonatomic, assign) BOOL added;
@end
@interface MASViewConstraint ()
@property (nonatomic, assign) CGFloat layoutConstant;
@end
SpecBegin(MASCompositeConstraint)
__block id<MASConstraintDelegate> delegate;
__block UIView *superview;
__block UIView *view;
__block MASCompositeConstraint *composite;
beforeEach(^{
composite = nil;
delegate = mockProtocol(@protocol(MASConstraintDelegate));
view = UIView.new;
superview = UIView.new;
[superview addSubview:view];
});
it(@"should create centerY and centerX children", ^{
composite = [[MASCompositeConstraint alloc] initWithView:view type:MASCompositeViewConstraintTypeCenter];
expect(composite.currentChildConstraints).to.haveCountOf(2);
MASViewConstraint *viewConstraint = composite.currentChildConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeCenterX);
viewConstraint = composite.currentChildConstraints[1];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeCenterY);
});
it(@"should create top, left, bottom, right children", ^{
UIView *newView = UIView.new;
composite = [[MASCompositeConstraint alloc] initWithView:view type:MASCompositeViewConstraintTypeEdges];
composite.equalTo(newView);
expect(composite.completedChildConstraints).to.haveCountOf(4);
//top
MASViewConstraint *viewConstraint = composite.completedChildConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeTop);
//left
viewConstraint = composite.completedChildConstraints[1];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeLeft);
//bottom
viewConstraint = composite.completedChildConstraints[2];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeBottom);
//right
viewConstraint = composite.completedChildConstraints[3];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeRight);
});
it(@"should create width and height children", ^{
composite = [[MASCompositeConstraint alloc] initWithView:view type:MASCompositeViewConstraintTypeSize];
expect(composite.currentChildConstraints).to.haveCountOf(2);
MASViewConstraint *viewConstraint = composite.currentChildConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
viewConstraint = composite.currentChildConstraints[1];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeHeight);
});
it(@"should complete children", ^{
composite = [[MASCompositeConstraint alloc] initWithView:view type:MASCompositeViewConstraintTypeSize];
composite.delegate = delegate;
UIView *newView = UIView.new;
//first equality statement
composite.equalTo(newView).sizeOffset(CGSizeMake(90, 30));
[verify(delegate) addConstraint:(id)composite];
expect(composite.completedChildConstraints).to.haveCountOf(2);
expect(composite.currentChildConstraints).to.haveCountOf(2);
MASViewConstraint *viewConstraint = composite.completedChildConstraints[0];
expect(viewConstraint.secondViewAttribute.view).to.beIdenticalTo(newView);
expect(viewConstraint.secondViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
expect(viewConstraint.layoutConstant).to.equal(90);
viewConstraint = composite.completedChildConstraints[1];
expect(viewConstraint.secondViewAttribute.view).to.beIdenticalTo(newView);
expect(viewConstraint.secondViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeHeight);
expect(viewConstraint.layoutConstant).to.equal(30);
//chain another equality statement
composite.greaterThanOrEqualTo(@6);
expect(composite.completedChildConstraints).to.haveCountOf(4);
expect(composite.currentChildConstraints).to.haveCountOf(2);
viewConstraint = composite.completedChildConstraints[2];
expect(viewConstraint.secondViewAttribute.view).to.beNil();
expect(viewConstraint.secondViewAttribute.layoutAttribute).to.equal(0);
expect(viewConstraint.layoutConstant).to.equal(6);
viewConstraint = composite.completedChildConstraints[2];
expect(viewConstraint.secondViewAttribute.view).to.beNil();
expect(viewConstraint.secondViewAttribute.layoutAttribute).to.equal(0);
expect(viewConstraint.layoutConstant).to.equal(6);
//still referencing same view
viewConstraint = composite.currentChildConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
viewConstraint = composite.currentChildConstraints[1];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeHeight);
});
it(@"should remove all on commit", ^{
composite = [[MASCompositeConstraint alloc] initWithView:view type:MASCompositeViewConstraintTypeSize];
composite.delegate = delegate;
UIView *newView = UIView.new;
[superview addSubview:newView];
//first equality statement
composite.equalTo(newView).sizeOffset(CGSizeMake(90, 30));
[verify(delegate) addConstraint:(id)composite];
expect(composite.completedChildConstraints).to.haveCountOf(2);
expect(composite.currentChildConstraints).to.haveCountOf(2);
[composite commit];
expect(composite.completedChildConstraints).to.haveCountOf(0);
expect(composite.currentChildConstraints).to.haveCountOf(0);
});
SpecEnd