Skip to content

Commit

Permalink
Fixes Single Equations issue + Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha committed Jan 27, 2017
1 parent f77da83 commit e64732d
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Source/Stevia+Equation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,23 @@ public func % (left: CGFloat, right: SteviaAttribute) -> SteviaAttribute {
}



// MARK: - Equations of type v.P == X

@discardableResult
public func == (left: SteviaAttribute, right: CGFloat) -> NSLayoutConstraint {
if let spv = left.view.superview {
var toItem: UIView? = spv
var constant: CGFloat = right
if left.attribute == .width || left.attribute == .height {
toItem = nil
}
if left.attribute == .bottom || left.attribute == .right {
constant = -constant
}
return spv.addConstraint(item: left.view,
attribute: left.attribute,
toItem: spv,
constant:right)
toItem: toItem,
constant:constant)
}
return NSLayoutConstraint()
}
Expand Down Expand Up @@ -176,5 +183,3 @@ public func <= (left: SteviaAttribute, right: CGFloat) -> NSLayoutConstraint {
}
return NSLayoutConstraint()
}


4 changes: 4 additions & 0 deletions Stevia.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
99585BF51D86ABFF004CE776 /* Stevia+Tap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99585BE31D86ABFF004CE776 /* Stevia+Tap.swift */; };
99585BF61D86ABFF004CE776 /* Stevia.h in Headers */ = {isa = PBXBuildFile; fileRef = 99585BE41D86ABFF004CE776 /* Stevia.h */; settings = {ATTRIBUTES = (Public, ); }; };
99B6217D1BF75D0E00A54B05 /* Stevia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9991C8551BDB6C8B007D9B75 /* Stevia.framework */; };
99BD931A1E3B746F0097A625 /* EquationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99BD93191E3B746F0097A625 /* EquationTests.swift */; };
99C5A4811C7A3524003EDE10 /* LayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99C5A4801C7A3524003EDE10 /* LayoutTests.swift */; };
99C5A4831C7A477C003EDE10 /* CenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99C5A4821C7A477C003EDE10 /* CenterTests.swift */; };
99C5A4851C7A4971003EDE10 /* FullLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99C5A4841C7A4971003EDE10 /* FullLayoutTests.swift */; };
Expand Down Expand Up @@ -74,6 +75,7 @@
9991C8551BDB6C8B007D9B75 /* Stevia.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Stevia.framework; sourceTree = BUILT_PRODUCTS_DIR; };
99B621781BF75D0E00A54B05 /* SteviaTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SteviaTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
99B6217C1BF75D0E00A54B05 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
99BD93191E3B746F0097A625 /* EquationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EquationTests.swift; sourceTree = "<group>"; };
99C5A4801C7A3524003EDE10 /* LayoutTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LayoutTests.swift; sourceTree = "<group>"; };
99C5A4821C7A477C003EDE10 /* CenterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CenterTests.swift; sourceTree = "<group>"; };
99C5A4841C7A4971003EDE10 /* FullLayoutTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FullLayoutTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,6 +173,7 @@
99C5A4841C7A4971003EDE10 /* FullLayoutTests.swift */,
99C5A4871C7A5B93003EDE10 /* FlexibleMarginTests.swift */,
1F00D53C1C9AD18A00B90965 /* StyleTests.swift */,
99BD93191E3B746F0097A625 /* EquationTests.swift */,
);
path = SteviaTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -328,6 +331,7 @@
buildActionMask = 2147483647;
files = (
CBC533541C6E2CCF0008F340 /* SizeTests.swift in Sources */,
99BD931A1E3B746F0097A625 /* EquationTests.swift in Sources */,
CBC533521C6E24970008F340 /* FillTests.swift in Sources */,
99C5A4881C7A5B93003EDE10 /* FlexibleMarginTests.swift in Sources */,
CBC5334E1C6E12890008F340 /* ContentTests.swift in Sources */,
Expand Down
125 changes: 125 additions & 0 deletions SteviaTests/EquationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//
// EquationTests.swift
// Stevia
//
// Created by Sacha Durand Saint Omer on 27/01/2017.
// Copyright © 2017 Sacha Durand Saint Omer. All rights reserved.
//

import XCTest
import Stevia

class EquationTests: XCTestCase {

var win: UIWindow!
var ctrler: UIViewController!

override func setUp() {
super.setUp()
win = UIWindow(frame: UIScreen.main.bounds)
ctrler = UIViewController()
win.rootViewController = ctrler
}

override func tearDown() {
super.tearDown()
}

func testTop() {
let v = UIView()
ctrler.view.sv(v)
v.Top == ctrler.view.Top + 10
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.origin.y, 10)
}

func testBottom() {
let v = UIView()
ctrler.view.sv(v)
v.Bottom == ctrler.view.Bottom - 23
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.origin.y, ctrler.view.frame.height - 23)
}

func testLeft() {
let v = UIView()
ctrler.view.sv(v)
v.Left == ctrler.view.Left + 72
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.origin.x, 72)
}

func testRight() {
let v = UIView()
ctrler.view.sv(v)
v.Right == ctrler.view.Right - 13
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.origin.x, ctrler.view.frame.width - 13)
}

func testWidth() {
let v = UIView()
ctrler.view.sv(v)
v.Width == ctrler.view.Width - 52
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.width, ctrler.view.frame.width - 52)
}

func testHeight() {
let v = UIView()
ctrler.view.sv(v)
v.Height == ctrler.view.Height + 34
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.height, ctrler.view.frame.height + 34)
}

// Single Value

func testSingleValueTop() {
let v = UIView()
ctrler.view.sv(v)
v.Top == 10
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.origin.y, 10)
}

func testSingleValueBottom() {
let v = UIView()
ctrler.view.sv(v)
v.Bottom == 23
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.origin.y, ctrler.view.frame.height - 23)
}

func testSingleValueLeft() {
let v = UIView()
ctrler.view.sv(v)
v.Left == 72
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.origin.x, 72)
}

func testSingleValueRight() {
let v = UIView()
ctrler.view.sv(v)
v.Right == 13
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.origin.x, ctrler.view.frame.width - 13)
}

func testSingleValueWidth() {
let v = UIView()
ctrler.view.sv(v)
v.Width == 23
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.width, 23)
}

func testSingleValueHeight() {
let v = UIView()
ctrler.view.sv(v)
v.Height == 94
ctrler.view.layoutIfNeeded() // This is needed to force auto-layout to kick-in
XCTAssertEqual(v.frame.height, 94)
}
}

0 comments on commit e64732d

Please sign in to comment.