2015-10-16 19:28:16 +03:00
|
|
|
struct RangedTerm {
|
2015-10-16 22:05:01 +03:00
|
|
|
typealias Term = Cofree<String, Range<String.Index>>
|
2015-10-16 20:23:38 +03:00
|
|
|
let term: Term
|
2015-10-16 22:05:01 +03:00
|
|
|
let source: String
|
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
|
|
|
|
2015-10-16 22:05:01 +03:00
|
|
|
static let indexed = (prefix: "[\n\t", separator: ",\n\t", suffix: "\n]")
|
|
|
|
static let keyed = (prefix: "[\n\t", separator: ",\n\t", suffix: "\n}")
|
|
|
|
|
2015-10-16 21:40:18 +03:00
|
|
|
var source: String {
|
2015-10-16 22:05:01 +03:00
|
|
|
let indexed = UnannotatedTerm.indexed
|
|
|
|
let keyed = UnannotatedTerm.keyed
|
2015-10-16 21:40:18 +03:00
|
|
|
return term.cata {
|
|
|
|
switch $0 {
|
|
|
|
case let .Leaf(s):
|
|
|
|
return s
|
|
|
|
case let .Indexed(s):
|
2015-10-16 22:05:01 +03:00
|
|
|
return indexed.prefix + s.joinWithSeparator(indexed.separator) + indexed.suffix
|
2015-10-16 21:40:18 +03:00
|
|
|
case let .Keyed(s):
|
2015-10-16 22:05:01 +03:00
|
|
|
return keyed.prefix + s.map { "\"\($0)\": \($1)" }.joinWithSeparator(keyed.separator) + keyed.suffix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ranged: Cofree<String, Range<Int>> {
|
|
|
|
let indexed = UnannotatedTerm.indexed
|
|
|
|
let keyed = UnannotatedTerm.keyed
|
|
|
|
return term.cata {
|
|
|
|
switch $0 {
|
|
|
|
case let .Leaf(s):
|
|
|
|
return Cofree(0..<s.characters.count, .Leaf(s))
|
|
|
|
case let .Indexed(i):
|
|
|
|
var length = indexed.prefix.characters.count
|
|
|
|
var results: [Cofree<String, Range<Int>>] = []
|
|
|
|
for value in i {
|
|
|
|
if results.count > 0 {
|
|
|
|
length += indexed.separator.characters.count
|
|
|
|
}
|
|
|
|
results.append(value.map {
|
|
|
|
($0.startIndex + length)..<($0.endIndex + length)
|
|
|
|
})
|
|
|
|
length += value.extract.count
|
|
|
|
}
|
|
|
|
return Cofree(0..<(length + indexed.suffix.characters.count), .Indexed(results))
|
|
|
|
case let .Keyed(k):
|
|
|
|
var length = keyed.prefix.characters.count
|
|
|
|
var results: [(String, Cofree<String, Range<Int>>)] = []
|
|
|
|
for (key, value) in k {
|
|
|
|
if results.count > 0 {
|
|
|
|
length += keyed.separator.characters.count
|
|
|
|
}
|
|
|
|
results.append((key, value.map {
|
|
|
|
($0.startIndex + length)..<($0.endIndex + length)
|
|
|
|
}))
|
|
|
|
length += value.extract.count + 4 // for the characters around the key
|
|
|
|
}
|
|
|
|
return Cofree(0..<(length + keyed.suffix.characters.count), .Keyed(Dictionary(elements: results)))
|
2015-10-16 21:40:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 22:05:01 +03:00
|
|
|
|
|
|
|
var arranged: RangedTerm {
|
|
|
|
let source = self.source
|
|
|
|
return RangedTerm(
|
|
|
|
term: ranged.map {
|
|
|
|
source.startIndex.advancedBy($0.startIndex)..<source.startIndex.advancedBy($0.endIndex)
|
|
|
|
},
|
|
|
|
source: source)
|
|
|
|
}
|
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> {
|
2015-10-16 22:21:27 +03:00
|
|
|
let symbol: Gen<String> = Gen<Int>.choose((0, 15)).fmap { "_\($0)" }
|
2015-10-16 22:21:58 +03:00
|
|
|
let leaf: Gen<Term> = symbol.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
|
2015-10-16 22:17:00 +03:00
|
|
|
sequence((0..<n).map { _ in Gen.pure(()).bind { UnannotatedTerm.arbitrary } }).fmap {
|
2015-10-16 21:26:46 +03:00
|
|
|
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
|
2015-10-16 22:22:07 +03:00
|
|
|
sequence((0..<n).map { _ in symbol.bind { key in Gen.pure(()).bind { UnannotatedTerm.arbitrary }.fmap { (key, $0.term) } } }).fmap {
|
2015-10-16 21:30:53 +03:00
|
|
|
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 22:06:27 +03:00
|
|
|
extension RangedTerm: Arbitrary {
|
|
|
|
static var arbitrary: Gen<RangedTerm> {
|
|
|
|
return UnannotatedTerm.arbitrary.fmap { $0.arranged }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|