diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-14 20:13:11 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-14 20:13:11 +0200 |
| commit | 754f362da077b06420ab8350dfb62bd2d8d89d75 (patch) | |
| tree | 513d643876f12ad423f9338209ed9c0b607e7b39 /internal/enumtest/enumtest_test.go | |
| parent | 792e6e5416a51fb05f7171aa7ef4f9a284a8cf68 (diff) | |
| download | krino-754f362da077b06420ab8350dfb62bd2d8d89d75.tar.gz krino-754f362da077b06420ab8350dfb62bd2d8d89d75.zip | |
plan 8: enum values read from source must all be handled
Diffstat (limited to 'internal/enumtest/enumtest_test.go')
| -rw-r--r-- | internal/enumtest/enumtest_test.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/internal/enumtest/enumtest_test.go b/internal/enumtest/enumtest_test.go new file mode 100644 index 0000000..6a7bd69 --- /dev/null +++ b/internal/enumtest/enumtest_test.go @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package enumtest + +import ( + "os" + "path/filepath" + "reflect" + "testing" +) + +// TestNames: typed constants are found in declaration order, those an iota +// block types implicitly included; untyped constants and other types are +// not. +func TestNames(t *testing.T) { + src := "package x\n\ntype K int\ntype Other int\n\nconst (\n\tA K = iota\n\tB\n\tC\n)\n\nconst (\n\tX Other = iota\n\tY\n)\n\nconst Z K = 9\n\nconst (\n\tP K = 1\n\tQ = 2\n)\n" + path := filepath.Join(t.TempDir(), "x.go") + if err := os.WriteFile(path, []byte(src), 0o644); err != nil { + t.Fatal(err) + } + got, err := Names(path, "K") + if err != nil { + t.Fatal(err) + } + if want := []string{"A", "B", "C", "Z", "P"}; !reflect.DeepEqual(got, want) { + t.Errorf("Names = %v, want %v", got, want) + } + if _, err := Names(path, "Missing"); err == nil { + t.Error("a type with no constants should be an error") + } +} |
