turbo-editors/turbo-corepublic Fork 0
v1.0.1
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

tree_test.go · 191 lines · 5.0 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 18h ago1package app
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "github.com/gdamore/tcell/v2"
10)
11
12// newTreeApp returns an editor whose working directory is a small project.
13func newTreeApp(t *testing.T, names ...string) (*App, tcell.SimulationScreen, string) {
14 t.Helper()
15
16 root := testDirectory(t, names...)
17 t.Chdir(root)
18
19 a, screen := newTestApp(t)
20 return a, screen, root
21}
22
23func TestTheProjectTreeOpensAWindowNamedAfterTheProject(t *testing.T) {
24 a, _, root := newTreeApp(t, "main.go")
25
26 a.ProjectTree()
27
28 if a.Desktop().Count() != 1 {
29 t.Fatalf("Count() = %d, want the tree window", a.Desktop().Count())
30 }
31 if got := a.Desktop().Active().Title(); got != filepath.Base(root) {
32 t.Errorf("the window is called %q, want %q", got, filepath.Base(root))
33 }
34 if a.activeView() != nil {
35 t.Error("activeView() returned an editor for the tree window")
36 }
37}
38
39func TestTheTreeIsRootedWhereTheEditorWasStarted(t *testing.T) {
40 // The same rule .turbo-go follows, so "the project" means one thing.
41 a, screen, _ := newTreeApp(t, "internal/", "main.go")
42
43 a.ProjectTree()
44 lines := render(t, a, screen)
45
46 for _, want := range []string{"internal", "main.go"} {
47 if !containsAnyLine(lines, want) {
48 t.Errorf("the tree does not show %q", want)
49 }
50 }
51}
52
53func TestOpeningTheTreeTwiceRaisesTheSameWindow(t *testing.T) {
54 a, _, _ := newTreeApp(t, "main.go")
55
56 a.ProjectTree()
57 first := a.Desktop().Active()
58 a.NewFile()
59 a.ProjectTree()
60
61 if a.Desktop().Count() != 2 {
62 t.Fatalf("Count() = %d, want the tree and the file", a.Desktop().Count())
63 }
64 if a.Desktop().Active() != first {
65 t.Error("a second tree window was opened instead of raising the first")
66 }
67}
68
69func TestF9OpensTheProjectTree(t *testing.T) {
70 a, _, root := newTreeApp(t, "main.go")
71
72 press(a, tcell.KeyF9, 0, tcell.ModNone)
73
74 if a.Desktop().Count() != 1 {
75 t.Fatalf("Count() = %d, want F9 to have opened the tree", a.Desktop().Count())
76 }
77 if got := a.Desktop().Active().Title(); got != filepath.Base(root) {
78 t.Errorf("the window is called %q", got)
79 }
80}
81
82func TestChoosingAFileInTheTreeOpensIt(t *testing.T) {
83 a, _, root := newTreeApp(t, "main.go")
84 writeTestFile(t, filepath.Join(root, "main.go"), "package main\n")
85 a.ProjectTree()
86
87 press(a, tcell.KeyEnter, 0, tcell.ModNone) // the highlight starts on main.go
88
89 if a.Desktop().Count() != 2 {
90 t.Fatalf("Count() = %d, want the tree and the opened file", a.Desktop().Count())
91 }
92 if got := a.Desktop().Active().Title(); got != "main.go" {
93 t.Errorf("the front window is %q, want the file the tree opened", got)
94 }
95 if got := activeBuffer(t, a).Text(); got != "package main\n" {
96 t.Errorf("the buffer holds %q", got)
97 }
98}
99
100func TestChoosingADirectoryInTheTreeOpensNoWindow(t *testing.T) {
101 a, _, _ := newTreeApp(t, "internal/", "main.go")
102 a.ProjectTree()
103
104 press(a, tcell.KeyEnter, 0, tcell.ModNone) // the highlight starts on internal/
105
106 if a.Desktop().Count() != 1 {
107 t.Errorf("Count() = %d; choosing a directory opened a window", a.Desktop().Count())
108 }
109}
110
111func TestClosingTheTreeLetsANewOneOpen(t *testing.T) {
112 a, _, _ := newTreeApp(t, "main.go")
113 a.ProjectTree()
114
115 a.CloseFile()
116
117 if a.Desktop().Count() != 0 {
118 t.Fatalf("Count() = %d, want the tree closed", a.Desktop().Count())
119 }
120 if a.Modals() != 0 {
121 t.Error("closing the tree asked about unsaved changes")
122 }
123
124 a.ProjectTree()
125 if a.Desktop().Count() != 1 {
126 t.Error("the tree could not be opened again after being closed")
127 }
128}
129
130func TestTheFileActionsLeaveTheTreeAlone(t *testing.T) {
131 a, _, _ := newTreeApp(t, "main.go")
132 a.ProjectTree()
133
134 a.SaveFile()
135 a.SaveFileAs()
136 a.ToggleLineNumbers()
137 a.RequestCompletion()
138
139 if a.Modals() != 0 {
140 t.Errorf("%d dialogs opened over the tree window", a.Modals())
141 }
142 if a.Desktop().Count() != 1 {
143 t.Errorf("Count() = %d, want the tree still there", a.Desktop().Count())
144 }
145}
146
147func TestSavingAFileRefreshesTheTree(t *testing.T) {
148 a, screen, root := newTreeApp(t, "main.go")
149 writeTestFile(t, filepath.Join(root, "main.go"), "package main\n")
150 a.ProjectTree()
151 a.Open(filepath.Join(root, "main.go"))
152
153 // A file that appeared after the tree was read — as a build in a terminal
154 // window would leave behind.
155 if err := os.WriteFile(filepath.Join(root, "made-later.go"), nil, 0o644); err != nil {
156 t.Fatalf("creating the file: %v", err)
157 }
158 if containsAnyLine(render(t, a, screen), "made-later.go") {
159 t.Fatal("the tree showed the new file before anything refreshed it")
160 }
161
162 a.SaveFile()
163
164 if !containsAnyLine(render(t, a, screen), "made-later.go") {
165 t.Error("saving did not refresh the tree")
166 }
167}
168
169func TestLeavingTheEditorWithATreeOpenAsksNothing(t *testing.T) {
170 a, _, _ := newTreeApp(t, "main.go")
171 a.ProjectTree()
172
173 a.Quit()
174
175 if !a.Quitting() {
176 t.Error("Quit() did not leave with only a tree open")
177 }
178 if a.Modals() != 0 {
179 t.Errorf("Quit() opened %d dialogs for a window with no unsaved work", a.Modals())
180 }
181}
182
183// containsAnyLine reports whether any drawn row holds a piece of text.
184func containsAnyLine(lines []string, want string) bool {
185 for _, line := range lines {
186 if strings.Contains(line, want) {
187 return true
188 }
189 }
190 return false
191}