2015-10-16 19:28:16 +03:00
|
|
|
struct RangedTerm {
|
2015-10-16 20:23:38 +03:00
|
|
|
typealias Term = Cofree<String, (String, Range<Int>)>
|
|
|
|
let term: Term
|
2015-10-16 19:28:16 +03:00
|
|
|
}
|
|
|
|
|
2015-10-16 21:24:17 +03:00
|
|
|
struct UnannotatedTerm {
|
|
|
|
typealias Term = Cofree<String, ()>
|
|
|
|
let term: Term
|
2015-10-16 21:40:18 +03:00
|
|
|
|
|
|
|
var source: String {
|
|
|
|
return term.cata {
|
|
|
|
switch $0 {
|
|
|
|
case let .Leaf(s):
|
|
|
|
return s
|
|
|
|
case let .Indexed(s):
|
|
|
|
return "[\n\t" + s.joinWithSeparator(",\n\t") + "\n]"
|
|
|
|
case let .Keyed(s):
|
|
|
|
return "{\n\t" + s.map { "\"\($0)\": \($1)" }.joinWithSeparator(",\n\t") + "\n}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 21:24:17 +03:00
|
|
|
}
|
2015-10-16 19:28:16 +03:00
|
|
|
|
2015-10-16 21:26:32 +03:00
|
|
|
|
|
|
|
extension UnannotatedTerm: Arbitrary {
|
|
|
|
static var arbitrary: Gen<UnannotatedTerm> {
|
|
|
|
let leaf: Gen<Term> = String.arbitrary.fmap { Term((), .Leaf($0)) }
|
2015-10-16 21:26:58 +03:00
|
|
|
let indexed: Gen<Term> = Gen.sized { n in
|
2015-10-16 21:26:46 +03:00
|
|
|
Gen<Int>.choose((0, n)).bind { n in
|
|
|
|
sequence((0..<n).map(const(UnannotatedTerm.arbitrary))).fmap {
|
|
|
|
Term((), .Indexed($0.map { $0.term }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 21:30:53 +03:00
|
|
|
let keyed: Gen<Term> = Gen.sized { n in
|
|
|
|
Gen<Int>.choose((0, n)).bind { n in
|
|
|
|
sequence((0..<n).map { _ in String.arbitrary.bind { key in UnannotatedTerm.arbitrary.fmap { (key, $0.term) } } }).fmap {
|
|
|
|
Term((), .Keyed(Dictionary(elements: $0)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 21:26:32 +03:00
|
|
|
return Gen.oneOf([
|
|
|
|
leaf,
|
2015-10-16 21:26:58 +03:00
|
|
|
indexed,
|
2015-10-16 21:30:53 +03:00
|
|
|
keyed,
|
2015-10-16 21:26:32 +03:00
|
|
|
]).fmap {
|
|
|
|
UnannotatedTerm(term: $0)
|
2015-10-16 20:23:38 +03:00
|
|
|
}
|
2015-10-16 19:28:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-16 19:28:16 +03:00
|
|
|
@testable import Doubt
|
2015-10-16 21:26:46 +03:00
|
|
|
import Prelude
|
2015-10-16 19:28:49 +03:00
|
|
|
import SwiftCheck
|