From ff97375b22fdf6a28a06e7f06823ce97b44c5037 Mon Sep 17 00:00:00 2001 From: Alex Manzella Date: Wed, 22 Feb 2017 03:07:09 +0100 Subject: [PATCH] Add `Strideble` conformance to `ConstraintPriority ` (#409) This enables sugar like: `.priority(.low + 1)` --- Source/ConstraintPriority.swift | 13 +++++++++++-- Tests/Tests.swift | 7 ++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Source/ConstraintPriority.swift b/Source/ConstraintPriority.swift index 7299fa9..f9dab16 100644 --- a/Source/ConstraintPriority.swift +++ b/Source/ConstraintPriority.swift @@ -27,8 +27,7 @@ import AppKit #endif - -public struct ConstraintPriority : ExpressibleByFloatLiteral, Equatable { +public struct ConstraintPriority : ExpressibleByFloatLiteral, Equatable, Strideable { public typealias FloatLiteralType = Float public let value: Float @@ -65,4 +64,14 @@ public struct ConstraintPriority : ExpressibleByFloatLiteral, Equatable { public static func ==(lhs: ConstraintPriority, rhs: ConstraintPriority) -> Bool { return lhs.value == rhs.value } + + // MARK: Strideable + + public func advanced(by n: FloatLiteralType) -> ConstraintPriority { + return ConstraintPriority(floatLiteral: value + n) + } + + public func distance(to other: ConstraintPriority) -> FloatLiteralType { + return other.value - value + } } diff --git a/Tests/Tests.swift b/Tests/Tests.swift index ed6df40..e232fcc 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -539,5 +539,10 @@ class SnapKitTests: XCTestCase { XCTAssertEqual(self.container.snp_constraints.count, 1, "Should have 1 constraint") XCTAssertEqual(self.container.snp_constraints.first?.priority, ConstraintPriority.low.value + 1) } - + + func testPriorityStride() { + let highPriority: ConstraintPriority = .high + let higherPriority: ConstraintPriority = .high + 1 + XCTAssertEqual(higherPriority.value, highPriority.value + 1) + } }