126 lines
4.8 KiB
Go
126 lines
4.8 KiB
Go
package tokenize
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestFork_CreatesForkOfInputAtSameCursorPosition(t *testing.T) {
|
|
// Create input, accept the first rune.
|
|
i := NewAPI("Testing")
|
|
i.NextRune()
|
|
i.Accept() // T
|
|
AssertEqual(t, "T", i.Result().String(), "accepted rune in input")
|
|
// Fork
|
|
f := i.Fork()
|
|
AssertEqual(t, f, i.child, "Input.child (must be f)")
|
|
AssertEqual(t, i, f.parent, "Input.parent (must be i)")
|
|
AssertEqual(t, 1, i.result.cursor.Byte, "i.child.cursor.Byte")
|
|
AssertEqual(t, 1, i.child.result.cursor.Byte, "i.child.cursor.Byte")
|
|
// Accept two runes via fork.
|
|
f.NextRune()
|
|
f.Accept() // e
|
|
f.NextRune()
|
|
f.Accept() // s
|
|
AssertEqual(t, "es", f.Result().String(), "result runes in fork")
|
|
AssertEqual(t, 1, i.result.cursor.Byte, "i.child.cursor.Byte")
|
|
AssertEqual(t, 3, i.child.result.cursor.Byte, "i.child.cursor.Byte")
|
|
// Merge fork back into parent
|
|
f.Merge()
|
|
AssertEqual(t, "Tes", i.Result().String(), "result runes in parent Input after Merge()")
|
|
AssertEqual(t, 3, i.result.cursor.Byte, "i.child.cursor.Byte")
|
|
}
|
|
|
|
func TestGivenForkedChildWhichAcceptedRune_AfterMerging_RuneEndsUpInParentResult(t *testing.T) {
|
|
i := NewAPI("Testing")
|
|
i.NextRune()
|
|
i.Accept()
|
|
f1 := i.Fork()
|
|
f1.NextRune()
|
|
f1.Accept()
|
|
f2 := f1.Fork()
|
|
f2.NextRune()
|
|
f2.Accept()
|
|
AssertEqual(t, "T", i.Result().String(), "i.Result().String()")
|
|
AssertEqual(t, 1, i.result.offset, "i.offset A")
|
|
AssertEqual(t, "e", f1.Result().String(), "f1.Result().String()")
|
|
AssertEqual(t, 2, f1.result.offset, "f1.offset A")
|
|
AssertEqual(t, "s", f2.Result().String(), "f2.Result().String()")
|
|
AssertEqual(t, 3, f2.result.offset, "f2.offset A")
|
|
f2.Merge()
|
|
AssertEqual(t, "T", i.Result().String(), "i.Result().String()")
|
|
AssertEqual(t, 1, i.result.offset, "i.offset B")
|
|
AssertEqual(t, "es", f1.Result().String(), "f1.Result().String()")
|
|
AssertEqual(t, 3, f1.result.offset, "f1.offset B")
|
|
AssertEqual(t, "", f2.Result().String(), "f2.Result().String()")
|
|
AssertEqual(t, 3, f2.result.offset, "f2.offset B")
|
|
f1.Merge()
|
|
AssertEqual(t, "Tes", i.Result().String(), "i.Result().String()")
|
|
AssertEqual(t, 3, i.result.offset, "i.offset C")
|
|
AssertEqual(t, "", f1.Result().String(), "f1.Result().String()")
|
|
AssertEqual(t, 3, f1.result.offset, "f1.offset C")
|
|
AssertEqual(t, "", f2.Result().String(), "f2.Result().String()")
|
|
AssertEqual(t, 3, f2.result.offset, "f2.offset C")
|
|
}
|
|
|
|
func TestGivenMultipleLevelsOfForks_WhenReturningToRootInput_ForksAreDetached(t *testing.T) {
|
|
i := NewAPI("Testing")
|
|
f1 := i.Fork()
|
|
f2 := f1.Fork()
|
|
f3 := f2.Fork()
|
|
f4 := f1.Fork() // secret subtest: this Fork() detaches both forks f2 and f3
|
|
f5 := f4.Fork()
|
|
AssertEqual(t, true, i.parent == nil, "i.parent == nil")
|
|
AssertEqual(t, true, i.child == f1, "i.child == f1")
|
|
AssertEqual(t, true, f1.parent == i, "f1.parent == i")
|
|
AssertEqual(t, true, f1.child == f4, "f1.child == f4")
|
|
AssertEqual(t, true, f2.child == nil, "f2.child == nil")
|
|
AssertEqual(t, true, f2.parent == nil, "f2.parent == nil")
|
|
AssertEqual(t, true, f3.child == nil, "f3.child == nil")
|
|
AssertEqual(t, true, f3.parent == nil, "f3.parent == nil")
|
|
AssertEqual(t, true, f4.parent == f1, "f4.parent == f1")
|
|
AssertEqual(t, true, f4.child == f5, "f4.child == f5")
|
|
AssertEqual(t, true, f5.parent == f4, "f5.parent == f4")
|
|
AssertEqual(t, true, f5.child == nil, "f5.child == nil")
|
|
|
|
i.NextRune()
|
|
|
|
AssertEqual(t, true, i.parent == nil, "i.parent == nil")
|
|
AssertEqual(t, true, i.child == nil, "i.child == nil")
|
|
AssertEqual(t, true, f1.parent == nil, "f1.parent == nil")
|
|
AssertEqual(t, true, f1.child == nil, "f1.child == nil")
|
|
AssertEqual(t, true, f2.child == nil, "f2.child == nil")
|
|
AssertEqual(t, true, f2.parent == nil, "f2.parent == nil")
|
|
AssertEqual(t, true, f3.child == nil, "f3.child == nil")
|
|
AssertEqual(t, true, f3.parent == nil, "f3.parent == nil")
|
|
AssertEqual(t, true, f4.parent == nil, "f4.parent == nil")
|
|
AssertEqual(t, true, f4.child == nil, "f4.child == nil")
|
|
AssertEqual(t, true, f5.parent == nil, "f5.parent == nil")
|
|
AssertEqual(t, true, f5.child == nil, "f5.child == nil")
|
|
}
|
|
|
|
func TestCallingAcceptAfterNextRune_AcceptsRuneAndMovesReadOffsetForward(t *testing.T) {
|
|
i := NewAPI("Testing")
|
|
r, _ := i.NextRune()
|
|
AssertEqual(t, 'T', r, "result from 1st call to NextRune()")
|
|
AssertTrue(t, i.result.lastRune != nil, "API.result.lastRune after NextRune() is not nil")
|
|
i.Accept()
|
|
AssertTrue(t, i.result.lastRune == nil, "API.result.lastRune after Accept() is nil")
|
|
AssertEqual(t, 1, i.result.offset, "API.result.offset")
|
|
r, _ = i.NextRune()
|
|
AssertEqual(t, 'e', r, "result from 2nd call to NextRune()")
|
|
}
|
|
|
|
func AssertEqual(t *testing.T, expected interface{}, actual interface{}, forWhat string) {
|
|
if expected != actual {
|
|
t.Errorf(
|
|
"Unexpected value for %s:\nexpected: %q\nactual: %q",
|
|
forWhat, expected, actual)
|
|
}
|
|
}
|
|
|
|
func AssertTrue(t *testing.T, b bool, assertion string) {
|
|
if !b {
|
|
t.Errorf("Assertion %s is false", assertion)
|
|
}
|
|
}
|