1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 07:25:44 +03:00
semantic/prototype/DoubtTests/DiffTests.swift

67 lines
2.1 KiB
Swift
Raw Normal View History

2015-10-17 00:07:37 +03:00
final class DiffTests: XCTestCase {
override static func setUp() {
sranddev()
}
2015-10-17 00:13:51 +03:00
typealias Term = RangedTerm.Term
2015-10-22 23:02:03 +03:00
typealias Diff = Free<String, (Term.Annotation, Term.Annotation), Patch<Term>>
2015-10-17 00:13:59 +03:00
let interpreter = Interpreter<Term>(equal: ==, comparable: const(true), cost: Diff.sum(const(1)))
func testEqualTermsProduceIdentityDiffs() {
property("equal terms produce identity diffs") <- forAll { (term: RangedTerm) in
Diff.sum(const(1))(self.interpreter.run(term.term, term.term)) == 0
}
}
2015-10-17 00:26:35 +03:00
func testRecursivelyCopiedDiffsHaveNoPatches() {
property("recursively copying a term into a diff produces no patches") <- forAll { (term: RangedTerm) in
Free.sum(const(1))(Free<Term.Leaf, Term.Annotation, Patch<Term>>(term.term)) == 0
}
}
2015-10-17 00:46:12 +03:00
func testInequalTermsProduceNonIdentityDiffs() {
property("inequal terms produce non-identity diffs") <- forAll { (diff: RangedDiff) in
2015-10-19 18:47:52 +03:00
(!Term.equals(annotation: const(true), leaf: ==)(diff.a.term, diff.b.term)) ==> Diff.sum(const(1))(diff.diff) > 0
2015-10-17 00:46:12 +03:00
}
}
2015-10-17 00:26:35 +03:00
func testEqualityIsReflexive() {
property("equality is reflexive") <- forAll { (diff: RangedDiff) in
2015-10-17 00:44:29 +03:00
equal(diff.diff, diff.diff)
2015-10-17 00:26:35 +03:00
}
}
2015-10-17 00:46:25 +03:00
func testDoubleInversionIsIdempotent() {
property("double inversion is idempotent") <- forAll { (diff: RangedDiff) in
equal(diff.diff, diff.diff.inverse.inverse)
}
}
func testOriginalTermsAreRecoverable() {
let equal = Cofree<String, ()>.equals(annotation: const(true), leaf: ==)
property("before state is recoverable") <- forAll { (diff: RangedDiff) in
2015-10-19 17:35:55 +03:00
diff.diff.map { $0.map { $0.map(const(())) } }.before.map {
2015-10-19 19:30:39 +03:00
equal($0, diff.a.stripped.term)
} ?? false
}
property("after state is recoverable") <- forAll { (diff: RangedDiff) in
2015-10-19 17:35:55 +03:00
diff.diff.map { $0.map { $0.map(const(())) } }.after.map {
2015-10-19 19:30:39 +03:00
equal($0, diff.b.stripped.term)
} ?? false
}
}
2015-10-17 00:07:37 +03:00
}
2015-10-17 00:44:29 +03:00
private func equal(a: DiffTests.Diff, _ b: DiffTests.Diff) -> Bool {
return Free.equals(pure: Patch.equals(Cofree.equals(annotation: ==, leaf: ==)), leaf: ==, annotation: const(true))(a, b)
2015-10-17 00:44:29 +03:00
}
2015-10-17 00:07:37 +03:00
@testable import Doubt
2015-10-17 00:13:59 +03:00
import Prelude
2015-10-17 00:07:37 +03:00
import SwiftCheck
import XCTest