MASCompositeConstraintSpec.m
5.21 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
//
// MASCompositeConstraintSpec.m
// Masonry
//
// Created by Jonas Budelmann on 22/07/13.
// Copyright (c) 2013 cloudling. All rights reserved.
//
#import "MASCompositeConstraint.h"
#import "MASViewConstraint.h"
#import "MASConstraintDelegateMock.h"
@interface MASCompositeConstraint () <MASConstraintDelegate>
@property (nonatomic, strong) NSMutableArray *childConstraints;
@end
@interface MASViewConstraint ()
@property (nonatomic, assign) CGFloat layoutConstant;
@property (nonatomic, assign) MASLayoutPriority layoutPriority;
@end
SpecBegin(MASCompositeConstraint)
__block MASConstraintDelegateMock *delegate;
__block UIView *superview;
__block UIView *view;
__block MASCompositeConstraint *composite;
beforeEach(^{
composite = nil;
delegate = MASConstraintDelegateMock.new;
view = UIView.new;
superview = UIView.new;
[superview addSubview:view];
});
it(@"should create centerY and centerX children", ^{
composite = [[MASCompositeConstraint alloc] initWithView:view type:MASCompositeConstraintTypeCenter];
expect(composite.childConstraints).to.haveCountOf(2);
MASViewConstraint *viewConstraint = composite.childConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeCenterX);
viewConstraint = composite.childConstraints[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:MASCompositeConstraintTypeEdges];
composite.equalTo(newView);
expect(composite.childConstraints).to.haveCountOf(4);
//top
MASViewConstraint *viewConstraint = composite.childConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeTop);
//left
viewConstraint = composite.childConstraints[1];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeLeft);
//bottom
viewConstraint = composite.childConstraints[2];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeBottom);
//right
viewConstraint = composite.childConstraints[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:MASCompositeConstraintTypeSize];
expect(composite.childConstraints).to.haveCountOf(2);
MASViewConstraint *viewConstraint = composite.childConstraints[0];
expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(composite.view);
expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
viewConstraint = composite.childConstraints[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:MASCompositeConstraintTypeSize];
composite.delegate = delegate;
UIView *newView = UIView.new;
//first equality statement
composite.equalTo(newView).sizeOffset(CGSizeMake(90, 30)).priorityLow();
expect(composite.childConstraints).to.haveCountOf(2);
MASViewConstraint *viewConstraint = composite.childConstraints[0];
expect(viewConstraint.secondViewAttribute.view).to.beIdenticalTo(newView);
expect(viewConstraint.secondViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
expect(viewConstraint.layoutConstant).to.equal(90);
expect(viewConstraint.layoutPriority).to.equal(MASLayoutPriorityDefaultLow);
viewConstraint = composite.childConstraints[1];
expect(viewConstraint.secondViewAttribute.view).to.beIdenticalTo(newView);
expect(viewConstraint.secondViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeHeight);
expect(viewConstraint.layoutConstant).to.equal(30);
expect(viewConstraint.layoutPriority).to.equal(MASLayoutPriorityDefaultLow);
});
it(@"should not remove on commit", ^{
composite = [[MASCompositeConstraint alloc] initWithView:view type:MASCompositeConstraintTypeSize];
composite.delegate = delegate;
UIView *newView = UIView.new;
[superview addSubview:newView];
//first equality statement
composite.equalTo(newView).sizeOffset(CGSizeMake(90, 30));
[composite commit];
expect(composite.childConstraints).to.haveCountOf(2);
expect(delegate.constraints).to.contain(composite.childConstraints[0]);
expect(delegate.constraints).to.contain(composite.childConstraints[1]);
});
SpecEnd