MASConstraintMakerSpec.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // MASConstraintMakerSpec.m
  3. // Masonry
  4. //
  5. // Created by Jonas Budelmann on 25/08/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASConstraintMaker.h"
  9. #import "MASCompositeConstraint.h"
  10. #import "MASViewConstraint.h"
  11. @interface MASConstraintMaker () <MASConstraintDelegate>
  12. @property (nonatomic, weak) MAS_VIEW *view;
  13. @property (nonatomic, strong) NSMutableArray *constraints;
  14. @end
  15. @interface MASCompositeConstraint ()
  16. @property (nonatomic, strong) NSMutableArray *childConstraints;
  17. @end
  18. SpecBegin(MASConstraintMaker) {
  19. __strong MASConstraintMaker *maker;
  20. __strong MAS_VIEW *superview;
  21. __strong MAS_VIEW *view;
  22. __strong MASCompositeConstraint *composite;
  23. }
  24. - (void)setUp {
  25. composite = nil;
  26. view = MAS_VIEW.new;
  27. superview = MAS_VIEW.new;
  28. [superview addSubview:view];
  29. maker = [[MASConstraintMaker alloc] initWithView:view];
  30. }
  31. - (void)testCreateCenterYAndCenterXChildren {
  32. composite = maker.center;
  33. expect(composite.childConstraints).to.haveCountOf(2);
  34. MASViewConstraint *viewConstraint = composite.childConstraints[0];
  35. expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
  36. expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeCenterX);
  37. viewConstraint = composite.childConstraints[1];
  38. expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
  39. expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeCenterY);
  40. }
  41. - (void)testCreateAllEdges {
  42. MAS_VIEW *newView = MAS_VIEW.new;
  43. composite = maker.edges;
  44. composite.equalTo(newView);
  45. expect(composite.childConstraints).to.haveCountOf(4);
  46. //top
  47. MASViewConstraint *viewConstraint = composite.childConstraints[0];
  48. expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
  49. expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeTop);
  50. //left
  51. viewConstraint = composite.childConstraints[1];
  52. expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
  53. expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeLeft);
  54. //bottom
  55. viewConstraint = composite.childConstraints[2];
  56. expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
  57. expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeBottom);
  58. //right
  59. viewConstraint = composite.childConstraints[3];
  60. expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
  61. expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeRight);
  62. }
  63. - (void)testCreateWidthAndHeightChildren {
  64. composite = maker.size;
  65. expect(composite.childConstraints).to.haveCountOf(2);
  66. MASViewConstraint *viewConstraint = composite.childConstraints[0];
  67. expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
  68. expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeWidth);
  69. viewConstraint = composite.childConstraints[1];
  70. expect(viewConstraint.firstViewAttribute.view).to.beIdenticalTo(maker.view);
  71. expect(viewConstraint.firstViewAttribute.layoutAttribute).to.equal(NSLayoutAttributeHeight);
  72. }
  73. - (void)testInstallConstraints {
  74. MAS_VIEW *newView = MAS_VIEW.new;
  75. [superview addSubview:newView];
  76. maker.edges.equalTo(newView);
  77. maker.centerX.equalTo(@[newView, @10]);
  78. expect([maker install]).to.haveCountOf(2);
  79. }
  80. - (void)testUpdateConstraints {
  81. MAS_VIEW *newView = MAS_VIEW.new;
  82. [superview addSubview:newView];
  83. maker.updateExisting = YES;
  84. maker.left.equalTo(newView).offset(10);
  85. [maker install];
  86. NSLayoutConstraint *constraint1 = superview.constraints[0];
  87. expect(constraint1.constant).to.equal(10);
  88. maker.left.equalTo(newView).offset(20);
  89. [maker install];
  90. expect(superview.constraints).to.haveCountOf(1);
  91. NSLayoutConstraint *constraint2 = superview.constraints[0];
  92. expect(constraint2.constant).to.equal(20);
  93. expect(constraint2).to.beIdenticalTo(constraint2);
  94. }
  95. - (void)testDoNotUpdateConstraints {
  96. MAS_VIEW *newView = MAS_VIEW.new;
  97. [superview addSubview:newView];
  98. maker.updateExisting = YES;
  99. maker.left.equalTo(newView).offset(10);
  100. [maker install];
  101. NSLayoutConstraint *constraint1 = superview.constraints[0];
  102. expect(constraint1.constant).to.equal(10);
  103. maker.right.equalTo(newView).offset(20);
  104. [maker install];
  105. expect(superview.constraints).to.haveCountOf(2);
  106. NSLayoutConstraint *constraint2 = superview.constraints[1];
  107. expect(constraint1.constant).to.equal(10);
  108. expect(constraint2.constant).to.equal(20);
  109. }
  110. - (void)testCreateNewViewAttributes {
  111. expect(maker.left).notTo.beIdenticalTo(maker.left);
  112. expect(maker.right).notTo.beIdenticalTo(maker.right);
  113. expect(maker.top).notTo.beIdenticalTo(maker.top);
  114. expect(maker.bottom).notTo.beIdenticalTo(maker.bottom);
  115. expect(maker.baseline).notTo.beIdenticalTo(maker.baseline);
  116. expect(maker.leading).notTo.beIdenticalTo(maker.leading);
  117. expect(maker.trailing).notTo.beIdenticalTo(maker.trailing);
  118. expect(maker.width).notTo.beIdenticalTo(maker.width);
  119. expect(maker.height).notTo.beIdenticalTo(maker.height);
  120. expect(maker.centerX).notTo.beIdenticalTo(maker.centerX);
  121. expect(maker.centerY).notTo.beIdenticalTo(maker.centerY);
  122. }
  123. SpecEnd