turbo-editors/turbo-corepublic Fork 0
d662cebdb65b319885da903daf7eff9ab1bfbb78
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.

🛟 Updated. 28d5985 · on d662cebdb65b319885da903daf7eff9ab1bfbb78 · k33g · 21h ago
tree_test.go · 191 lines · 5.0 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package app

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/gdamore/tcell/v2"
)

// newTreeApp returns an editor whose working directory is a small project.
func newTreeApp(t *testing.T, names ...string) (*App, tcell.SimulationScreen, string) {
	t.Helper()

	root := testDirectory(t, names...)
	t.Chdir(root)

	a, screen := newTestApp(t)
	return a, screen, root
}

func TestTheProjectTreeOpensAWindowNamedAfterTheProject(t *testing.T) {
	a, _, root := newTreeApp(t, "main.go")

	a.ProjectTree()

	if a.Desktop().Count() != 1 {
		t.Fatalf("Count() = %d, want the tree window", a.Desktop().Count())
	}
	if got := a.Desktop().Active().Title(); got != filepath.Base(root) {
		t.Errorf("the window is called %q, want %q", got, filepath.Base(root))
	}
	if a.activeView() != nil {
		t.Error("activeView() returned an editor for the tree window")
	}
}

func TestTheTreeIsRootedWhereTheEditorWasStarted(t *testing.T) {
	// The same rule .turbo-go follows, so "the project" means one thing.
	a, screen, _ := newTreeApp(t, "internal/", "main.go")

	a.ProjectTree()
	lines := render(t, a, screen)

	for _, want := range []string{"internal", "main.go"} {
		if !containsAnyLine(lines, want) {
			t.Errorf("the tree does not show %q", want)
		}
	}
}

func TestOpeningTheTreeTwiceRaisesTheSameWindow(t *testing.T) {
	a, _, _ := newTreeApp(t, "main.go")

	a.ProjectTree()
	first := a.Desktop().Active()
	a.NewFile()
	a.ProjectTree()

	if a.Desktop().Count() != 2 {
		t.Fatalf("Count() = %d, want the tree and the file", a.Desktop().Count())
	}
	if a.Desktop().Active() != first {
		t.Error("a second tree window was opened instead of raising the first")
	}
}

func TestF9OpensTheProjectTree(t *testing.T) {
	a, _, root := newTreeApp(t, "main.go")

	press(a, tcell.KeyF9, 0, tcell.ModNone)

	if a.Desktop().Count() != 1 {
		t.Fatalf("Count() = %d, want F9 to have opened the tree", a.Desktop().Count())
	}
	if got := a.Desktop().Active().Title(); got != filepath.Base(root) {
		t.Errorf("the window is called %q", got)
	}
}

func TestChoosingAFileInTheTreeOpensIt(t *testing.T) {
	a, _, root := newTreeApp(t, "main.go")
	writeTestFile(t, filepath.Join(root, "main.go"), "package main\n")
	a.ProjectTree()

	press(a, tcell.KeyEnter, 0, tcell.ModNone) // the highlight starts on main.go

	if a.Desktop().Count() != 2 {
		t.Fatalf("Count() = %d, want the tree and the opened file", a.Desktop().Count())
	}
	if got := a.Desktop().Active().Title(); got != "main.go" {
		t.Errorf("the front window is %q, want the file the tree opened", got)
	}
	if got := activeBuffer(t, a).Text(); got != "package main\n" {
		t.Errorf("the buffer holds %q", got)
	}
}

func TestChoosingADirectoryInTheTreeOpensNoWindow(t *testing.T) {
	a, _, _ := newTreeApp(t, "internal/", "main.go")
	a.ProjectTree()

	press(a, tcell.KeyEnter, 0, tcell.ModNone) // the highlight starts on internal/

	if a.Desktop().Count() != 1 {
		t.Errorf("Count() = %d; choosing a directory opened a window", a.Desktop().Count())
	}
}

func TestClosingTheTreeLetsANewOneOpen(t *testing.T) {
	a, _, _ := newTreeApp(t, "main.go")
	a.ProjectTree()

	a.CloseFile()

	if a.Desktop().Count() != 0 {
		t.Fatalf("Count() = %d, want the tree closed", a.Desktop().Count())
	}
	if a.Modals() != 0 {
		t.Error("closing the tree asked about unsaved changes")
	}

	a.ProjectTree()
	if a.Desktop().Count() != 1 {
		t.Error("the tree could not be opened again after being closed")
	}
}

func TestTheFileActionsLeaveTheTreeAlone(t *testing.T) {
	a, _, _ := newTreeApp(t, "main.go")
	a.ProjectTree()

	a.SaveFile()
	a.SaveFileAs()
	a.ToggleLineNumbers()
	a.RequestCompletion()

	if a.Modals() != 0 {
		t.Errorf("%d dialogs opened over the tree window", a.Modals())
	}
	if a.Desktop().Count() != 1 {
		t.Errorf("Count() = %d, want the tree still there", a.Desktop().Count())
	}
}

func TestSavingAFileRefreshesTheTree(t *testing.T) {
	a, screen, root := newTreeApp(t, "main.go")
	writeTestFile(t, filepath.Join(root, "main.go"), "package main\n")
	a.ProjectTree()
	a.Open(filepath.Join(root, "main.go"))

	// A file that appeared after the tree was read — as a build in a terminal
	// window would leave behind.
	if err := os.WriteFile(filepath.Join(root, "made-later.go"), nil, 0o644); err != nil {
		t.Fatalf("creating the file: %v", err)
	}
	if containsAnyLine(render(t, a, screen), "made-later.go") {
		t.Fatal("the tree showed the new file before anything refreshed it")
	}

	a.SaveFile()

	if !containsAnyLine(render(t, a, screen), "made-later.go") {
		t.Error("saving did not refresh the tree")
	}
}

func TestLeavingTheEditorWithATreeOpenAsksNothing(t *testing.T) {
	a, _, _ := newTreeApp(t, "main.go")
	a.ProjectTree()

	a.Quit()

	if !a.Quitting() {
		t.Error("Quit() did not leave with only a tree open")
	}
	if a.Modals() != 0 {
		t.Errorf("Quit() opened %d dialogs for a window with no unsaved work", a.Modals())
	}
}

// containsAnyLine reports whether any drawn row holds a piece of text.
func containsAnyLine(lines []string, want string) bool {
	for _, line := range lines {
		if strings.Contains(line, want) {
			return true
		}
	}
	return false
}