1
1
mirror of https://github.com/github/semantic.git synced 2024-12-20 21:31:48 +03:00
semantic/prototype/DoubtTests/SESTests.swift

72 lines
2.1 KiB
Swift
Raw Normal View History

2015-10-06 21:25:18 +03:00
final class SESTests: XCTestCase {
func testSESOverEmptyCollectionsIsEmpty() {
assert(SES([], []), ==, [])
}
func testSESOverEmptyAndNonEmptyCollectionsIsInsertions() {
2015-10-06 20:22:54 +03:00
assert(SES([], [ a, b ]), ==, [ insert(a), insert(b) ])
}
2015-09-16 17:04:30 +03:00
func testSESOverNonEmptyAndEmptyCollectionsIsDeletions() {
assert(SES([ a, b ], []), ==, [ delete(a), delete(b) ])
2015-09-16 17:04:30 +03:00
}
2015-09-17 22:55:30 +03:00
func testSESCanInsertAtHead() {
2015-10-07 00:48:17 +03:00
assert(SES([ a, b, c ], [ d, a, b, c ]), ==, [ insert(d), Diff(a), Diff(b), Diff(c) ])
2015-09-17 22:55:30 +03:00
}
2015-09-17 22:56:01 +03:00
func testSESCanDeleteAtHead() {
2015-10-07 00:48:17 +03:00
assert(SES([ d, a, b, c ], [ a, b, c ]), ==, [ delete(d), Diff(a), Diff(b), Diff(c) ])
2015-09-17 22:56:01 +03:00
}
func testSESCanInsertInMiddle() {
2015-10-07 00:48:17 +03:00
assert(SES([ a, b, c ], [ a, d, b, c ]), ==, [ Diff(a), insert(d), Diff(b), Diff(c) ])
}
func testSESCanDeleteInMiddle() {
2015-10-07 00:48:17 +03:00
assert(SES([ a, d, b, c ], [ a, b, c ]), ==, [ Diff(a), delete(d), Diff(b), Diff(c) ])
}
2015-10-07 00:29:06 +03:00
func testInsertsAtEnd() {
2015-10-07 00:48:17 +03:00
assert(SES([ a, b, c ], [ a, b, c, d ]), ==, [ Diff(a), Diff(b), Diff(c), insert(d) ])
2015-10-07 00:29:06 +03:00
}
2015-10-07 00:28:25 +03:00
func testDeletesAtEnd() {
2015-10-07 00:48:17 +03:00
assert(SES([ a, b, c, d ], [ a, b, c ]), ==, [ Diff(a), Diff(b), Diff(c), delete(d) ])
2015-10-07 00:28:25 +03:00
}
func testSESOfLongerSequences() {
assert(SES([ a, b, c, a, b, b, a ], [ c, b, a, b, a, c ]), ==, [ insert(c), delete(a), Diff(b), delete(c), Diff(a), delete(b), Diff(b), Diff(a), insert(c) ])
}
}
2015-10-06 20:22:37 +03:00
private func insert(term: Term) -> Diff {
return Diff.Pure(.Insert(term))
}
private func delete(term: Term) -> Diff {
return Diff.Pure(.Delete(term))
}
2015-10-15 16:08:30 +03:00
private typealias Term = Cofree<String, ()>
2015-10-14 19:19:38 +03:00
private typealias Diff = Free<String, Patch<Term>>
2015-10-06 19:57:48 +03:00
2015-10-15 16:08:30 +03:00
private let a = Term((), .Leaf("a"))
private let b = Term((), .Leaf("b"))
private let c = Term((), .Leaf("c"))
private let d = Term((), .Leaf("d"))
2015-10-06 19:58:07 +03:00
private func SES(a: [Term], _ b: [Term]) -> [Diff] {
2015-10-15 16:08:30 +03:00
return SES(a, b) { Cofree.equals(annotation: const(true), leaf: ==)($0, $1) ? Diff($1) : nil }
}
private func == (a: [Diff], b: [Diff]) -> Bool {
2015-10-15 16:08:30 +03:00
return a.count == b.count && zip(a, b).lazy.map(Diff.equals(ifPure: Patch.equals(Cofree.equals(annotation: const(true), leaf: ==)), ifRoll: ==)).reduce(true) { $0 && $1 }
}
2015-10-06 20:16:23 +03:00
import Assertions
2015-10-06 20:10:45 +03:00
@testable import Doubt
import Prelude
import XCTest