summaryrefslogtreecommitdiff
path: root/internal/enumtest/enumtest_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/enumtest/enumtest_test.go')
-rw-r--r--internal/enumtest/enumtest_test.go31
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")
+ }
+}