Browse Source

Add `Strideble` conformance to `ConstraintPriority ` (#409)

This enables sugar like: `.priority(.low + 1)`
Alex Manzella 9 years ago
parent
commit
ff97375b22
2 changed files with 17 additions and 3 deletions
  1. 11 2
      Source/ConstraintPriority.swift
  2. 6 1
      Tests/Tests.swift

+ 11 - 2
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
+    }
 }

+ 6 - 1
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)
+    }
 }