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

108 lines
3.4 KiB
Swift
Raw Normal View History

2015-11-05 18:56:15 +03:00
final class LocationTests: XCTestCase {
2015-11-05 18:56:55 +03:00
func testExplorationOfATermBeginsAtTheExploredTerm() {
assert(term.explore().it, ==, term)
}
func testCannotMoveUpwardsAtTheStartOfAnExploration() {
2015-11-05 20:06:39 +03:00
assert(term.explore().outOf?.it, ==, nil)
}
func testCannotMoveSidewaysAtTheStartOfAnExploration() {
assert(term.explore().left?.it, ==, nil)
assert(term.explore().right?.it, ==, nil)
}
2015-11-05 18:57:41 +03:00
func testCannotMoveDownwardsFromLeaves() {
2015-11-05 20:05:28 +03:00
assert(leafA.explore().into?.it, ==, nil)
2015-11-05 18:57:41 +03:00
}
2015-11-05 19:10:46 +03:00
func testCanMoveDownwardsIntoBranches() {
2015-11-05 20:05:28 +03:00
assert(term.explore().into?.it, ==, leafA)
2015-11-05 19:10:46 +03:00
}
func testCanMoveBackUpwards() {
2015-11-05 20:06:39 +03:00
assert(term.explore().into?.outOf?.it, ==, term)
}
func testCannotMoveLeftwardsFromFirstChildOfBranch() {
2015-11-05 20:05:28 +03:00
assert(term.explore().into?.left?.it, ==, nil)
}
func testCanMoveRightwardsFromLeftmostChildOfLongBranch() {
2015-11-05 20:05:28 +03:00
assert(term.explore().into?.right?.it, ==, leafB)
}
func testCanExploreBranchesDeeply() {
2015-11-05 20:05:28 +03:00
assert(term.explore().into?.right?.right?.into?.it, ==, innerLeafB)
}
func testCanMoveBackUpwardsFromDeepExplorations() {
2015-11-05 20:06:39 +03:00
assert(term.explore().into?.right?.right?.into?.right?.outOf?.outOf?.it, ==, term)
}
2015-11-05 19:36:30 +03:00
func testCanReturnToStartOfExplorationFromArbitrarilyDeepNodes() {
2015-11-05 20:05:28 +03:00
assert(term.explore().into?.right?.right?.into?.right?.root.it, ==, term)
2015-11-05 19:36:30 +03:00
}
func testSequenceIsPreOrderDepthFirstTraversal() {
assert(term.explore().map { $0.it.extract }, ==, [ 0, 1, 2, 3, 5, 4 ])
}
2015-11-05 19:46:01 +03:00
func testModifyReplacesSubtrees() {
2015-11-05 20:06:39 +03:00
assert(term.explore().into?.modify(const(leafB)).right?.outOf?.it, ==, Cofree(0, .Indexed([ leafB, leafB, keyed ])))
2015-11-05 19:46:01 +03:00
}
func testMultipleModificationsReplaceMultipleSubtrees() {
2015-11-05 20:06:39 +03:00
assert(term.explore().into?.modify(const(leafB)).right?.modify(const(leafA)).outOf?.it, ==, Cofree(0, .Indexed([ leafB, leafA, keyed ])))
}
2015-11-05 19:50:37 +03:00
func testModificationsPreserveKeys() {
2015-11-05 20:05:28 +03:00
assert(keyed.explore().into?.modify(const(leafA)).root.it, ==, Cofree(3, .Keyed([ "a": innerLeafA, "b": leafA ])))
2015-11-05 19:50:37 +03:00
}
func testDeepModificationsReplaceDeepSubtrees() {
2015-11-05 20:05:28 +03:00
assert(term.explore().into?.modify(const(leafB)).right?.modify(const(leafA)).right?.into?.modify(const(innerLeafA)).right?.modify(const(innerLeafB)).root.it, ==, Cofree(0, .Indexed([ leafB, leafA, Cofree(3, .Keyed([ "a": innerLeafB, "b": innerLeafA ])) ])))
}
func testModificationsCanRefineDiffs() {
2015-11-05 20:05:28 +03:00
assert(diff.explore().into?.right?.modify(const(refined)).root.it, ==, Free.Roll(0, .Indexed([ Free.Roll(1, .Leaf("a string")), refined ])))
}
2015-11-05 18:56:15 +03:00
}
2015-11-05 18:56:36 +03:00
2015-11-05 19:14:04 +03:00
private let leafA = Cofree(1, .Leaf("a string"))
2015-11-05 19:14:27 +03:00
private let leafB = Cofree(2, .Leaf("b string"))
2015-11-05 19:33:53 +03:00
private let innerLeafA = Cofree(4, .Leaf("a nested string"))
2015-11-05 19:34:18 +03:00
private let innerLeafB = Cofree(5, .Leaf("b nested string"))
2015-11-05 19:32:00 +03:00
private let keyed = Cofree(3, .Keyed([
2015-11-05 19:33:53 +03:00
"a": innerLeafA,
2015-11-05 19:34:18 +03:00
"b": innerLeafB,
2015-11-05 19:32:00 +03:00
]))
2015-11-05 18:56:36 +03:00
private let term: Cofree<String, Int> = Cofree(0, .Indexed([
2015-11-05 19:14:04 +03:00
leafA,
2015-11-05 19:14:27 +03:00
leafB,
2015-11-05 19:32:00 +03:00
keyed,
2015-11-05 18:56:36 +03:00
]))
2015-11-05 19:52:22 +03:00
private let diff: Free<String, Int, Patch<Cofree<String, Int>>> = Free.Roll(0, .Indexed([
Free.Roll(1, .Leaf("a string")),
2015-11-05 20:02:04 +03:00
Free.Pure(Patch.Replace(leafA, leafB)), // coarse-grained diff of two leaf nodes
2015-11-05 19:52:22 +03:00
]))
2015-11-05 20:01:47 +03:00
// fine-grained diff of same
private let refined = Free.Roll(2, .Indexed([
Free.Pure(Patch.Replace(Cofree(1, .Leaf("a")), Cofree(2, .Leaf("b")))),
Free.Roll(4, .Leaf(" ")),
Free.Roll(5, .Leaf("s")),
Free.Roll(6, .Leaf("t")),
Free.Roll(7, .Leaf("r")),
Free.Roll(8, .Leaf("i")),
Free.Roll(9, .Leaf("n")),
Free.Roll(10, .Leaf("g")),
]))
2015-11-05 19:52:22 +03:00
2015-11-05 18:56:36 +03:00
2015-11-05 18:56:55 +03:00
import Assertions
2015-11-05 18:56:15 +03:00
@testable import Doubt
2015-11-05 19:46:01 +03:00
import Prelude
2015-11-05 18:56:15 +03:00
import XCTest