MASConstraintMakerSpec.m
5.38 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
//
// MASConstraintMakerSpec.m
// Masonry
//
// Created by Jonas Budelmann on 25/08/13.
// Copyright (c) 2013 Jonas Budelmann. All rights reserved.
//
#import "MASConstraintMaker.h"
#import "MASCompositeConstraint.h"
#import "MASViewConstraint.h"
@interface MASConstraintMaker () <MASConstraintDelegate>
@property (nonatomic, weak) MAS_VIEW *view;
@property (nonatomic, strong) NSMutableArray *constraints;
@end
@interface MASCompositeConstraint ()
@property (nonatomic, strong) NSMutableArray *childConstraints;
@end
SpecBegin(MASConstraintMaker)
__block MASConstraintMaker *maker;
__block MAS_VIEW *superview;
__block MAS_VIEW *view;
__block MASCompositeConstraint *composite;
beforeEach(^{
composite = nil;
view = MAS_VIEW.new;
superview = MAS_VIEW.new;
[superview addSubview:view];
maker = [[MASConstraintMaker alloc] initWithView:view];
});
it(@"should create centerY and centerX children", ^{
composite = maker.center;
expect(composite.childConstraints).to.haveCountOf(2);
MASViewConstraint *viewConstraint = composite.childConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeCenterX);
viewConstraint = composite.childConstraints[1];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeCenterY);
});
it(@"should create top, left, bottom, right children", ^{
MAS_VIEW *newView = MAS_VIEW.new;
composite = maker.edges;
composite.equalTo(newView);
expect(composite.childConstraints).to.haveCountOf(4);
//top
MASViewConstraint *viewConstraint = composite.childConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeTop);
//left
viewConstraint = composite.childConstraints[1];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeLeft);
//bottom
viewConstraint = composite.childConstraints[2];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeBottom);
//right
viewConstraint = composite.childConstraints[3];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeRight);
});
it(@"should create width and height children", ^{
composite = maker.size;
expect(composite.childConstraints).to.haveCountOf(2);
MASViewConstraint *viewConstraint = composite.childConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
viewConstraint = composite.childConstraints[1];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeHeight);
});
it(@"should install constraints", ^{
MAS_VIEW *newView = MAS_VIEW.new;
[superview addSubview:newView];
maker.edges.equalTo(newView);
maker.centerX.equalTo(@[newView, @10]);
expect([maker install]).to.haveCountOf(2);
});
it(@"should update constraints", ^{
MAS_VIEW *newView = MAS_VIEW.new;
[superview addSubview:newView];
maker.updateExisting = YES;
maker.left.equalTo(newView).offset(10);
[maker install];
NSLayoutConstraint *constraint1 = superview.constraints[0];
expect(constraint1.constant).to.equal(10);
maker.left.equalTo(newView).offset(20);
[maker install];
expect(superview.constraints).to.haveCountOf(1);
NSLayoutConstraint *constraint2 = superview.constraints[0];
expect(constraint2.constant).to.equal(20);
expect(constraint2).to.beIdenticalTo(constraint2);
});
it(@"should not update constraint", ^{
MAS_VIEW *newView = MAS_VIEW.new;
[superview addSubview:newView];
maker.updateExisting = YES;
maker.left.equalTo(newView).offset(10);
[maker install];
NSLayoutConstraint *constraint1 = superview.constraints[0];
expect(constraint1.constant).to.equal(10);
maker.right.equalTo(newView).offset(20);
[maker install];
expect(superview.constraints).to.haveCountOf(2);
NSLayoutConstraint *constraint2 = superview.constraints[1];
expect(constraint1.constant).to.equal(10);
expect(constraint2.constant).to.equal(20);
});
it(@"should create new constraints", ^{
expect(maker.left).notTo.beIdenticalTo(maker.left);
expect(maker.right).notTo.beIdenticalTo(maker.right);
expect(maker.top).notTo.beIdenticalTo(maker.top);
expect(maker.bottom).notTo.beIdenticalTo(maker.bottom);
expect(maker.baseline).notTo.beIdenticalTo(maker.baseline);
expect(maker.leading).notTo.beIdenticalTo(maker.leading);
expect(maker.trailing).notTo.beIdenticalTo(maker.trailing);
expect(maker.width).notTo.beIdenticalTo(maker.width);
expect(maker.height).notTo.beIdenticalTo(maker.height);
expect(maker.centerX).notTo.beIdenticalTo(maker.centerX);
expect(maker.centerY).notTo.beIdenticalTo(maker.centerY);
});
SpecEnd