-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from CodaFi/an-ownership-model
Remove dependence on self in ArrowOf and IsoOf
- Loading branch information
Showing
3 changed files
with
113 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// FormatterSpec.swift | ||
// SwiftCheck | ||
// | ||
// Created by Robert Widmann on 11/30/16. | ||
// Copyright © 2016 Typelift. All rights reserved. | ||
// | ||
// Spec due to broomburgo (https://github.com/broomburgo) meant to test lifetime | ||
// issues in ArrowOf and IsoOf | ||
|
||
import SwiftCheck | ||
import XCTest | ||
|
||
struct Formatter<Value> { | ||
let lengthLimit : UInt | ||
let makeString : (Value) -> String | ||
let makeValue : (String) -> Value | ||
|
||
init(lengthLimit : UInt, makeString : @escaping (Value) -> String, makeValue : @escaping (String) -> Value) { | ||
self.lengthLimit = lengthLimit | ||
self.makeString = makeString | ||
self.makeValue = makeValue | ||
} | ||
|
||
func format(_ value : Value) -> String { | ||
let formatted = makeString(value) | ||
let maxIndex = formatted.index(formatted.startIndex, offsetBy: String.IndexDistance(lengthLimit)) | ||
if maxIndex >= formatted.endIndex { | ||
return formatted | ||
} else { | ||
return formatted.substring(to: maxIndex) | ||
} | ||
} | ||
|
||
func unFormat(_ string : String) -> Value { | ||
let value = makeValue(string) | ||
return value | ||
} | ||
} | ||
|
||
struct ArbitraryFormatter<Value : Arbitrary & CoArbitrary & Hashable> : Arbitrary { | ||
private let formatter : Formatter<Value> | ||
init(_ formatter : Formatter<Value>) { | ||
self.formatter = formatter | ||
} | ||
|
||
var get : Formatter<Value> { | ||
return formatter | ||
} | ||
|
||
static var arbitrary : Gen<ArbitraryFormatter<Value>> { | ||
return Gen.one(of: [ | ||
Gen<(UInt, ArrowOf<Value, String>, ArrowOf<String, Value>)> | ||
.zip(UInt.arbitrary, ArrowOf<Value,String>.arbitrary, ArrowOf<String,Value>.arbitrary) | ||
.map { Formatter<Value>(lengthLimit: $0, makeString: $1.getArrow, makeValue: $2.getArrow) } | ||
.map(ArbitraryFormatter.init), | ||
Gen<(UInt, IsoOf<Value, String>)> | ||
.zip(UInt.arbitrary, IsoOf<Value, String>.arbitrary) | ||
.map { Formatter<Value>(lengthLimit: $0, makeString: $1.getTo, makeValue: $1.getFrom) } | ||
.map(ArbitraryFormatter.init) | ||
]) | ||
} | ||
} | ||
|
||
class FormatterSpec : XCTestCase { | ||
func testAlwaysCorrectLength() { | ||
property( | ||
"Any formatted string is shorter or equal than the provided length" | ||
) <- forAll { (x: Int, af: ArbitraryFormatter<Int>) in | ||
let formatter = af.get | ||
let string = formatter.format(x) | ||
_ = formatter.unFormat(string) | ||
return string.distance(from: string.startIndex, to: string.endIndex) <= String.IndexDistance(formatter.lengthLimit) | ||
} | ||
} | ||
|
||
|
||
#if !(os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) | ||
static var allTests = testCase([ | ||
("testAlwaysCorrectLength", testAlwaysCorrectLength), | ||
]) | ||
#endif | ||
} |