1
1
mirror of https://github.com/github/semantic.git synced 2024-12-18 20:31:55 +03:00
semantic/prototype/DoubtTests/DiffTests.swift

61 lines
1.8 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
typealias Diff = Free<String, 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
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:04 +03:00
equal($0, diff.a.term.map(const(())))
} ?? 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:04 +03:00
equal($0, diff.b.term.map(const(())))
} ?? 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(ifPure: Patch.equals(Cofree.equals(annotation: ==, leaf: ==)), ifRoll: ==)(a, b)
}
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