mirror of
https://github.com/github/semantic.git
synced 2024-11-28 10:15:55 +03:00
67 lines
2.1 KiB
Swift
67 lines
2.1 KiB
Swift
final class DiffTests: XCTestCase {
|
|
override static func setUp() {
|
|
sranddev()
|
|
}
|
|
|
|
typealias Term = RangedTerm.Term
|
|
typealias Diff = Free<String, (Term.Annotation, Term.Annotation), Patch<Term>>
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
func testInequalTermsProduceNonIdentityDiffs() {
|
|
property("inequal terms produce non-identity diffs") <- forAll { (diff: RangedDiff) in
|
|
(!Term.equals(annotation: const(true), leaf: ==)(diff.a.term, diff.b.term)) ==> Diff.sum(const(1))(diff.diff) > 0
|
|
}
|
|
}
|
|
|
|
func testEqualityIsReflexive() {
|
|
property("equality is reflexive") <- forAll { (diff: RangedDiff) in
|
|
equal(diff.diff, diff.diff)
|
|
}
|
|
}
|
|
|
|
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
|
|
diff.diff.map { $0.map { $0.map(const(())) } }.before.map {
|
|
equal($0, diff.a.stripped.term)
|
|
} ?? false
|
|
}
|
|
|
|
property("after state is recoverable") <- forAll { (diff: RangedDiff) in
|
|
diff.diff.map { $0.map { $0.map(const(())) } }.after.map {
|
|
equal($0, diff.b.stripped.term)
|
|
} ?? false
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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)
|
|
}
|
|
|
|
|
|
@testable import Doubt
|
|
import Prelude
|
|
import SwiftCheck
|
|
import XCTest
|