2019-12-18 18:38:21 +08:00
|
|
|
package edit
|
2019-11-03 09:22:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-02-12 02:49:00 +08:00
|
|
|
"src.elv.sh/pkg/cli/tk"
|
2019-11-03 09:22:56 +08:00
|
|
|
)
|
|
|
|
|
2019-11-04 07:27:16 +08:00
|
|
|
func TestInsertAtDot(t *testing.T) {
|
2021-08-07 05:18:09 +08:00
|
|
|
f := setup(t)
|
2019-11-04 07:27:16 +08:00
|
|
|
|
2021-02-12 05:44:41 +08:00
|
|
|
f.SetCodeBuffer(tk.CodeBuffer{Content: "ab", Dot: 1})
|
2019-11-08 23:49:43 +08:00
|
|
|
evals(f.Evaler, `edit:insert-at-dot XYZ`)
|
2019-11-04 07:27:16 +08:00
|
|
|
|
2021-02-12 02:49:00 +08:00
|
|
|
testCodeBuffer(t, f.Editor, tk.CodeBuffer{Content: "aXYZb", Dot: 4})
|
2019-11-04 07:27:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplaceInput(t *testing.T) {
|
2021-08-07 05:18:09 +08:00
|
|
|
f := setup(t)
|
2019-11-04 07:27:16 +08:00
|
|
|
|
2021-02-12 05:44:41 +08:00
|
|
|
f.SetCodeBuffer(tk.CodeBuffer{Content: "ab", Dot: 1})
|
2019-11-08 23:49:43 +08:00
|
|
|
evals(f.Evaler, `edit:replace-input XYZ`)
|
2019-11-04 07:27:16 +08:00
|
|
|
|
2021-02-12 02:49:00 +08:00
|
|
|
testCodeBuffer(t, f.Editor, tk.CodeBuffer{Content: "XYZ", Dot: 3})
|
2019-11-04 07:27:16 +08:00
|
|
|
}
|
|
|
|
|
2019-11-03 09:22:56 +08:00
|
|
|
func TestDot(t *testing.T) {
|
2021-08-07 05:18:09 +08:00
|
|
|
f := setup(t)
|
2019-11-03 09:22:56 +08:00
|
|
|
|
2021-02-12 05:44:41 +08:00
|
|
|
f.SetCodeBuffer(tk.CodeBuffer{Content: "code", Dot: 4})
|
2022-01-03 08:10:40 +08:00
|
|
|
evals(f.Evaler, `set edit:-dot = 0`)
|
2019-11-04 07:27:16 +08:00
|
|
|
|
2021-02-12 02:49:00 +08:00
|
|
|
testCodeBuffer(t, f.Editor, tk.CodeBuffer{Content: "code", Dot: 0})
|
2019-11-03 09:22:56 +08:00
|
|
|
}
|
|
|
|
|
2024-04-11 06:11:21 +08:00
|
|
|
func TestDotOutOfBoundary(t *testing.T) {
|
|
|
|
f := setup(t)
|
|
|
|
|
|
|
|
f.SetCodeBuffer(tk.CodeBuffer{Content: "", Dot: 0})
|
2024-04-22 23:01:52 +08:00
|
|
|
evals(f.Evaler, "var err = ?(set edit:-dot = 10)[reason]")
|
|
|
|
testGlobal(t, f.Evaler, "err", errDotOutOfBoundary)
|
2024-04-11 06:11:21 +08:00
|
|
|
}
|
|
|
|
|
2019-11-03 09:22:56 +08:00
|
|
|
func TestCurrentCommand(t *testing.T) {
|
2021-08-07 05:18:09 +08:00
|
|
|
f := setup(t)
|
2019-11-03 09:22:56 +08:00
|
|
|
|
2022-01-03 08:10:40 +08:00
|
|
|
evals(f.Evaler, `set edit:current-command = code`)
|
2019-11-04 07:27:16 +08:00
|
|
|
|
2021-02-12 02:49:00 +08:00
|
|
|
testCodeBuffer(t, f.Editor, tk.CodeBuffer{Content: "code", Dot: 4})
|
2019-11-04 07:27:16 +08:00
|
|
|
}
|
|
|
|
|
2021-02-12 02:49:00 +08:00
|
|
|
func testCodeBuffer(t *testing.T, ed *Editor, wantBuf tk.CodeBuffer) {
|
2019-11-04 07:27:16 +08:00
|
|
|
t.Helper()
|
2021-09-02 05:41:39 +08:00
|
|
|
if buf := codeArea(ed.app).CopyState().Buffer; buf != wantBuf {
|
2019-11-03 09:22:56 +08:00
|
|
|
t.Errorf("content = %v, want %v", buf, wantBuf)
|
|
|
|
}
|
|
|
|
}
|