From 45ed2932c304c5c931b702fd81c6ed55ce064105 Mon Sep 17 00:00:00 2001 From: Benjamin Bengfort Date: Sat, 4 Apr 2026 13:35:06 -0500 Subject: [PATCH] [FEAT] Example Package for Testing and Documentation --- .gitignore | 3 +++ cmd/enumify/main.go | 11 +++++++- example/days.go | 38 ++++++++++++++++++++++++++++ example/example.go | 56 +++++++++++++++++++++++++++++++++++++++++ example/example_test.go | 17 +++++++++++++ example/status.go | 30 ++++++++++++++++++++++ 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 example/days.go create mode 100644 example/example.go create mode 100644 example/example_test.go create mode 100644 example/status.go diff --git a/.gitignore b/.gitignore index aaadf73..ae4211a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ go.work.sum # Editor/IDE # .idea/ # .vscode/ + +# Do not track generated example code in order to ensure tests pass. +example/*_gen.go diff --git a/cmd/enumify/main.go b/cmd/enumify/main.go index 38dd16d..c245260 100644 --- a/cmd/enumify/main.go +++ b/cmd/enumify/main.go @@ -1,3 +1,12 @@ package main -func main() {} +import ( + "fmt" + "os" +) + +func main() { + fname := os.Getenv("GOFILE") + pkg := os.Getenv("GOPACKAGE") + fmt.Println(fname, pkg) +} diff --git a/example/days.go b/example/days.go new file mode 100644 index 0000000..74c0464 --- /dev/null +++ b/example/days.go @@ -0,0 +1,38 @@ +package example + +// Day is an enum type that should be implemented by the enumify generator. +// It uses a 1D array of strings for the names, which should be discovered by the +// enumify generator due to the go:generate directive above the Day type declaration. +// +//go:generate go run ../cmd/enumify +type Day uint8 + +// Constants for the Day enum values. +// These values should be discovered by the enumify generator since they use the same +// type as the Day enum, which is the type being generated. +const ( + Unknown Day = iota + Monday + Tuesday + Wednesday + Thursday + Friday + Saturday + Sunday +) + +// 1D array of strings for the names of the Day enum values. +// This should be discovered by the enumify generator due to the go:generate directive +// and because it matches the dayNames pattern to connect it with the Day enum. +// +//lint:ignore U1000 this is used by the enumify generator +var dayNames = []string{ + "unknown", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday", +} diff --git a/example/example.go b/example/example.go new file mode 100644 index 0000000..435327b --- /dev/null +++ b/example/example.go @@ -0,0 +1,56 @@ +package example + +import ( + "math/rand" + "time" +) + +// This is an unrelated type that should be ignored by the enumify generator. +type Example struct { + Name string + Day Day + Status Status + Date time.Time + Tags []string +} + +// This is an unrelated function that should be ignored by the enumify generator. +func New() (*Example, error) { + adjectives := exampleNames[0] + nouns := exampleNames[1] + name := adjectives[rand.Intn(len(adjectives))] + " " + nouns[rand.Intn(len(nouns))] + + day := Day(rand.Intn(int(Sunday) + 1)) + status := Status(rand.Intn(int(StatusCancelled) + 1)) + + now := time.Now() + date := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local) + + n := rand.Intn(6) // 0–5 tags + tags := make([]string, n) + for i := range tags { + tags[i] = exampleTags[rand.Intn(len(exampleTags))] + } + + return &Example{ + Name: name, + Day: day, + Status: status, + Date: date, + Tags: tags, + }, nil +} + +// This is an unrelated variable that should be ignored by the enumify generator. +var exampleTags = []string{ + "low", "medium", "high", + "red", "green", "blue", + "primary", "secondary", "success", "danger", "warning", "info", + "foo", "bar", "baz", +} + +// This is an unrelated 2D array that should be ignored by the enumify generator. +var exampleNames = [][]string{ + {"curious", "ancient", "vibrant", "subtle", "brittle", "serene", "chaotic", "luminous", "hollow", "nimble", "terse", "ornate"}, + {"mountain", "river", "castle", "lighthouse", "violin", "compass", "telescope", "orchard", "glacier", "harbor", "cathedral", "parchment"}, +} diff --git a/example/example_test.go b/example/example_test.go new file mode 100644 index 0000000..dfced0b --- /dev/null +++ b/example/example_test.go @@ -0,0 +1,17 @@ +package example_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "go.rtnl.ai/enumify/example" +) + +// This is an unrelated test that should be ignored by the enumify generator. +func TestExample(t *testing.T) { + example, err := example.New() + require.NoError(t, err) + require.NotNil(t, example) + require.NotZero(t, example.Name) + require.False(t, example.Date.IsZero()) +} diff --git a/example/status.go b/example/status.go new file mode 100644 index 0000000..a994df3 --- /dev/null +++ b/example/status.go @@ -0,0 +1,30 @@ +package example + +// Status is an enum type that should be implemented by the enumify generator. +// It uses a 2D array of strings for the names, which should be discovered by the +// enumify generator due to the go:generate directive above the Status type declaration. +// +//go:generate go run ../cmd/enumify -names statusTable +type Status uint8 + +// Constants for the Status enum values. +// These values should be discovered by the enumify generator since they use the same +// type as the Status enum, which is the type being generated. +const ( + StatusUnknown Status = iota + StatusPending + StatusRunning + StatusFailed + StatusSuccess + StatusCancelled +) + +// 2D array of strings for the names of the Status enum values. +// This should be discovered by the enumify generator due to the go:generate directive +// that specifies this variable as the names for the Status enum. +// +//lint:ignore U1000 this is used by the enumify generator +var statusTable = [][]string{ + {"unknown", "pending", "running", "failed", "success", "cancelled"}, + {"text-secondary", "text-info", "text-primary", "text-danger", "text-success", "text-warning"}, +}