turbo-editors/turbo-moonbitpublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-moonbit.git
git clone ssh://git@rickub.com/turbo-editors/turbo-moonbit.git

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

📦 Turbo MoonBit cc1f595 · on main · k33g · 10h ago
profile_test.go · 247 lines · 7.4 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package moonbitlang_test

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

	"rickub.com/turbo-editors/turbo-core/profile"
	"rickub.com/turbo-editors/turbo-core/syntax"

	"rickub.com/turbo-editors/turbo-moonbit/internal/moonbitlang"
)

// fixedMenuHotKeys are the hot keys turbo-core's own menus take. The toolchain
// menu may not claim one of them, or one of the two would be unreachable from
// the keyboard and nothing would say so.
var fixedMenuHotKeys = []rune{'F', 'E', 'S', 'R', 'C', 'O', 'W', 'N', 'H'}

func TestProfileNamesTheEditor(t *testing.T) {
	p := moonbitlang.Profile()

	if p.Name != "Turbo MoonBit" {
		t.Errorf("Name = %q, want %q", p.Name, "Turbo MoonBit")
	}
	if p.Slug != "turbo-moonbit" {
		t.Errorf("Slug = %q, want %q", p.Slug, "turbo-moonbit")
	}
	// The About box and the status bar read this out, so it is the language's
	// own spelling rather than the registry's lower-case name.
	if p.Language != "MoonBit" {
		t.Errorf("Language = %q, want %q", p.Language, "MoonBit")
	}
}

func TestSlugDerivesEveryPath(t *testing.T) {
	p := moonbitlang.Profile()

	if got := p.ProjectDir(); got != ".turbo-moonbit" {
		t.Errorf("ProjectDir() = %q, want %q", got, ".turbo-moonbit")
	}
	for name, got := range map[string]string{
		"DirEnvVar":        p.DirEnvVar(),
		"ThemeDirEnvVar":   p.ThemeDirEnvVar(),
		"SnippetDirEnvVar": p.SnippetDirEnvVar(),
	} {
		if !strings.HasPrefix(got, "TURBO_MOONBIT_") {
			t.Errorf("%s() = %q, want a TURBO_MOONBIT_ prefix", name, got)
		}
	}
}

func TestThemeDirFollowsItsEnvironmentVariable(t *testing.T) {
	p := moonbitlang.Profile()
	want := t.TempDir()
	t.Setenv(p.ThemeDirEnvVar(), want)

	if got := p.ThemeDir(); got != want {
		t.Errorf("ThemeDir() = %q, want %q", got, want)
	}
}

func TestToolsMenuHotKeyClashesWithNoFixedMenu(t *testing.T) {
	label := moonbitlang.Profile().ToolsMenu

	hotKey, ok := hotKeyOf(label)
	if !ok {
		t.Fatalf("ToolsMenu = %q, which marks no hot key between tildes", label)
	}
	if slices.Contains(fixedMenuHotKeys, hotKey) {
		t.Errorf("ToolsMenu hot key %q is already taken by a fixed menu", hotKey)
	}
	if got := strings.ReplaceAll(label, "~", ""); got != "MoonBit" {
		t.Errorf("ToolsMenu reads %q once the tildes are removed, want %q", got, "MoonBit")
	}
}

// hotKeyOf returns the upper-case letter a menu label marks between tildes.
func hotKeyOf(label string) (rune, bool) {
	open := strings.Index(label, "~")
	if open < 0 {
		return 0, false
	}
	rest := label[open+1:]
	shut := strings.Index(rest, "~")
	if shut != 1 {
		return 0, false
	}
	return []rune(strings.ToUpper(rest))[0], true
}

func TestRootMarkersAreMoonModules(t *testing.T) {
	want := []string{"moon.mod", "moon.mod.json"}

	if got := moonbitlang.Profile().RootMarkers; !slices.Equal(got, want) {
		t.Errorf("RootMarkers = %v, want %v", got, want)
	}
}

func TestServerIsMoonLSPOverStdio(t *testing.T) {
	server := moonbitlang.Profile().Server

	if server.Command != "moon-lsp" {
		t.Errorf("Server.Command = %q, want %q", server.Command, "moon-lsp")
	}
	// moon-lsp with no argument prints its usage and exits, which the editor
	// would see as a server that died at once.
	if !slices.Contains(server.Args, "--stdio") {
		t.Errorf("Server.Args = %v, want it to contain --stdio", server.Args)
	}
	if server.InstallHint == "" {
		t.Error("Server.InstallHint is empty; a missing server would say nothing useful")
	}
	// It is shown on the status bar, so it has to fit on a narrow line.
	if len(server.InstallHint) > 72 {
		t.Errorf("InstallHint is %d characters, too long for a status bar", len(server.InstallHint))
	}
}

func TestServerArgsCannotBeAppendedToByACaller(t *testing.T) {
	first := moonbitlang.ServerArgs()
	first = append(first, "--nonsense")

	if second := moonbitlang.ServerArgs(); slices.Contains(second, "--nonsense") {
		t.Errorf("ServerArgs() = %v after a caller appended to an earlier result", second)
	}
}

func TestServerIsLookedForInMoonHome(t *testing.T) {
	home := t.TempDir()
	t.Setenv("MOON_HOME", home)

	dirs := moonbitlang.Profile().Server.Dirs
	want := filepath.Join(home, "bin")
	if !slices.Contains(dirs, want) {
		t.Errorf("Server.Dirs = %v, want it to contain %q", dirs, want)
	}
}

func TestServerDirsFallBackToTheDefaultInstallDirectory(t *testing.T) {
	t.Setenv("MOON_HOME", "")

	home, err := os.UserHomeDir()
	if err != nil {
		t.Skip("no home directory on this machine")
	}
	want := filepath.Join(home, ".moon", "bin")

	if dirs := moonbitlang.ServerDirs(); !slices.Contains(dirs, want) {
		t.Errorf("ServerDirs() = %v, want it to contain %q", dirs, want)
	}
}

func TestServerDirsDoNotRepeatOneDirectory(t *testing.T) {
	home, err := os.UserHomeDir()
	if err != nil {
		t.Skip("no home directory on this machine")
	}
	t.Setenv("MOON_HOME", filepath.Join(home, ".moon"))

	dirs := moonbitlang.ServerDirs()
	if len(dirs) != 1 {
		t.Errorf("ServerDirs() = %v with MOON_HOME at the default, want one entry", dirs)
	}
}

func TestProfileIsReadAfreshEveryTime(t *testing.T) {
	// Server.Dirs comes out of the environment, so a package-level variable
	// would freeze whatever MOON_HOME said when the binary was linked.
	first := t.TempDir()
	t.Setenv("MOON_HOME", first)
	before := moonbitlang.Profile().Server.Dirs

	second := t.TempDir()
	t.Setenv("MOON_HOME", second)
	after := moonbitlang.Profile().Server.Dirs

	if slices.Equal(before, after) {
		t.Errorf("Profile().Server.Dirs = %v both times; it did not follow MOON_HOME", after)
	}
}

func TestTemplatesAreAllFilledIn(t *testing.T) {
	templates := moonbitlang.Profile().Templates

	for name, template := range map[string]string{
		"Settings": templates.Settings,
		"Snippets": templates.Snippets,
		"Tools":    templates.Tools,
	} {
		if template == "" {
			t.Errorf("Templates.%s is empty; the editor would offer to write nothing", name)
		}
	}
}

func TestRegisterTeachesTheLibraryMoonBit(t *testing.T) {
	moonbitlang.Register()

	if !slices.Contains(syntax.Registered(), moonbitlang.Language) {
		t.Fatalf("Registered() = %v, want it to contain %q", syntax.Registered(), moonbitlang.Language)
	}

	for _, path := range []string{"main.mbt", "pkg.mbti", "script.mbtx", "DEEP/nested/x.MBT"} {
		if got := syntax.LanguageOf(path, ""); got != moonbitlang.Language {
			t.Errorf("LanguageOf(%q) = %q, want %q", path, got, moonbitlang.Language)
		}
	}
}

func TestRegisterLeavesOtherFilesAlone(t *testing.T) {
	moonbitlang.Register()

	cases := map[string]syntax.Language{
		// A .mbt.md is a Markdown document with MoonBit in its fences. Its
		// extension is .md, and Markdown is what should colour it.
		"README.mbt.md": syntax.LanguageMarkdown,
		"moon.mod.json": syntax.LanguageNone,
		"Dockerfile":    syntax.LanguageDockerfile,
		"main.py":       syntax.LanguageNone,
	}
	for path, want := range cases {
		if got := syntax.LanguageOf(path, ""); got != want {
			t.Errorf("LanguageOf(%q) = %q, want %q", path, got, want)
		}
	}
}

func TestNoShebangIsClaimed(t *testing.T) {
	moonbitlang.Register()

	// MoonBit has no interpreter line: a file opening with #! would lex as an
	// attribute named ! and fail. Claiming one would take a shell script away
	// from the scanner that can actually colour it.
	if got := syntax.LanguageOf("script", "#!/usr/bin/env moon"); got == moonbitlang.Language {
		t.Errorf("LanguageOf with a moon shebang = %q, want anything but MoonBit", got)
	}
}

// A profile is a literal, so this example is the whole of how one is used.
func ExampleProfile() {
	moonbitlang.Register()

	var p profile.Profile = moonbitlang.Profile()
	_ = p.ProjectDir() // ".turbo-moonbit"
}