aboutsummaryrefslogtreecommitdiff
path: root/internal/enumtest/enumtest_test.go
blob: 6a7bd693db65aa8157c5dcad33830d52b3abce2e (plain) (blame)
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
// 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")
	}
}