Skip to content

Commit 3629e3f

Browse files
committed
docs: add constructor-first rule to test ordering conventions
Update CLAUDE.md and MAINTAINERS_GUIDE.md to state that constructor functions (NewXYZ) should always be declared first at the top of test files, before alphabetically ordered test functions. Also allow exceptions when alphabetical ordering doesn't work well. Apply the new rule to iostreams_test.go by moving Test_IOSteams_NewIOStreams to the top of the file.
1 parent 5552c10 commit 3629e3f

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

.claude/CLAUDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func Test_getKeyLength(t *testing.T) { ... } // package-level function
146146

147147
### Test Ordering Conventions
148148

149-
Test functions should be ordered alphabetically within each file. When a file has logical sections (separated by comments), tests should be alphabetical within each section. Getter and setter functions are grouped together under the base name — ignore the `Get` or `Set` prefix when determining order (e.g. `Test_AppName` and `Test_SetAppName` both sort under `A`).
149+
Constructor functions (`NewXYZ`) should always be declared first, at the top of the test file. After constructors, test functions should be ordered alphabetically within each file. When a file has logical sections (separated by comments), tests should be alphabetical within each section. Getter and setter functions are grouped together under the base name — ignore the `Get` or `Set` prefix when determining order (e.g. `Test_AppName` and `Test_SetAppName` both sort under `A`). Exceptions to alphabetical ordering can be made when it doesn't work well for readability or logical grouping.
150150

151151
### Table-Driven Test Conventions
152152

.github/MAINTAINERS_GUIDE.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,18 @@ func Test_getKeyLength(t *testing.T) { ... }
399399
400400
#### Test ordering conventions
401401
402-
Test functions should be ordered alphabetically within each file. When a file has
403-
logical sections (separated by comments), tests should be alphabetical within each
404-
section.
402+
Constructor functions (`NewXYZ`) should always be declared first, at the top of the
403+
test file. After constructors, test functions should be ordered alphabetically. When
404+
a file has logical sections (separated by comments), tests should be alphabetical
405+
within each section.
405406
406407
Getter and setter functions should be grouped together under the base name. Ignore
407408
the `Get` or `Set` prefix when determining alphabetical order. For example,
408409
`Test_AppName` and `Test_SetAppName` are both sorted under `A` for `AppName`.
409410
411+
Exceptions to alphabetical ordering can be made when it doesn't work well for
412+
readability or logical grouping.
413+
410414
#### Contributing tests
411415

412416
If you'd like to add tests, please review our

internal/iostreams/iostreams_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ import (
2525
"github.com/stretchr/testify/require"
2626
)
2727

28+
func Test_IOSteams_NewIOStreams(t *testing.T) {
29+
var io *IOStreams
30+
fsMock := slackdeps.NewFsMock()
31+
osMock := slackdeps.NewOsMock()
32+
config := config.NewConfig(fsMock, osMock)
33+
config.DebugEnabled = true
34+
io = NewIOStreams(config, fsMock, osMock)
35+
require.True(t, io.config.DebugEnabled, "iostreams references config")
36+
}
37+
2838
func Test_IOStreams_ExitCode(t *testing.T) {
2939
tests := map[string]struct {
3040
setCode ExitCode
@@ -55,16 +65,6 @@ func Test_IOStreams_ExitCode(t *testing.T) {
5565
}
5666
}
5767

58-
func Test_IOSteams_NewIOStreams(t *testing.T) {
59-
var io *IOStreams
60-
fsMock := slackdeps.NewFsMock()
61-
osMock := slackdeps.NewOsMock()
62-
config := config.NewConfig(fsMock, osMock)
63-
config.DebugEnabled = true
64-
io = NewIOStreams(config, fsMock, osMock)
65-
require.True(t, io.config.DebugEnabled, "iostreams references config")
66-
}
67-
6868
func Test_IOStreams_IsTTY(t *testing.T) {
6969
tests := map[string]struct {
7070
fileInfo os.FileInfo

0 commit comments

Comments
 (0)