Skip to content

Commit 35ddb1e

Browse files
authored
[FEAT] Example Package for Testing and Documentation (#4)
1 parent 2ff1597 commit 35ddb1e

6 files changed

Lines changed: 154 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ go.work.sum
3030
# Editor/IDE
3131
# .idea/
3232
# .vscode/
33+
34+
# Do not track generated example code in order to ensure tests pass.
35+
example/*_gen.go

cmd/enumify/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
package main
22

3-
func main() {}
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func main() {
9+
fname := os.Getenv("GOFILE")
10+
pkg := os.Getenv("GOPACKAGE")
11+
fmt.Println(fname, pkg)
12+
}

example/days.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package example
2+
3+
// Day is an enum type that should be implemented by the enumify generator.
4+
// It uses a 1D array of strings for the names, which should be discovered by the
5+
// enumify generator due to the go:generate directive above the Day type declaration.
6+
//
7+
//go:generate go run ../cmd/enumify
8+
type Day uint8
9+
10+
// Constants for the Day enum values.
11+
// These values should be discovered by the enumify generator since they use the same
12+
// type as the Day enum, which is the type being generated.
13+
const (
14+
Unknown Day = iota
15+
Monday
16+
Tuesday
17+
Wednesday
18+
Thursday
19+
Friday
20+
Saturday
21+
Sunday
22+
)
23+
24+
// 1D array of strings for the names of the Day enum values.
25+
// This should be discovered by the enumify generator due to the go:generate directive
26+
// and because it matches the dayNames pattern to connect it with the Day enum.
27+
//
28+
//lint:ignore U1000 this is used by the enumify generator
29+
var dayNames = []string{
30+
"unknown",
31+
"Monday",
32+
"Tuesday",
33+
"Wednesday",
34+
"Thursday",
35+
"Friday",
36+
"Saturday",
37+
"Sunday",
38+
}

example/example.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package example
2+
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
// This is an unrelated type that should be ignored by the enumify generator.
9+
type Example struct {
10+
Name string
11+
Day Day
12+
Status Status
13+
Date time.Time
14+
Tags []string
15+
}
16+
17+
// This is an unrelated function that should be ignored by the enumify generator.
18+
func New() (*Example, error) {
19+
adjectives := exampleNames[0]
20+
nouns := exampleNames[1]
21+
name := adjectives[rand.Intn(len(adjectives))] + " " + nouns[rand.Intn(len(nouns))]
22+
23+
day := Day(rand.Intn(int(Sunday) + 1))
24+
status := Status(rand.Intn(int(StatusCancelled) + 1))
25+
26+
now := time.Now()
27+
date := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
28+
29+
n := rand.Intn(6) // 0–5 tags
30+
tags := make([]string, n)
31+
for i := range tags {
32+
tags[i] = exampleTags[rand.Intn(len(exampleTags))]
33+
}
34+
35+
return &Example{
36+
Name: name,
37+
Day: day,
38+
Status: status,
39+
Date: date,
40+
Tags: tags,
41+
}, nil
42+
}
43+
44+
// This is an unrelated variable that should be ignored by the enumify generator.
45+
var exampleTags = []string{
46+
"low", "medium", "high",
47+
"red", "green", "blue",
48+
"primary", "secondary", "success", "danger", "warning", "info",
49+
"foo", "bar", "baz",
50+
}
51+
52+
// This is an unrelated 2D array that should be ignored by the enumify generator.
53+
var exampleNames = [][]string{
54+
{"curious", "ancient", "vibrant", "subtle", "brittle", "serene", "chaotic", "luminous", "hollow", "nimble", "terse", "ornate"},
55+
{"mountain", "river", "castle", "lighthouse", "violin", "compass", "telescope", "orchard", "glacier", "harbor", "cathedral", "parchment"},
56+
}

example/example_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package example_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"go.rtnl.ai/enumify/example"
8+
)
9+
10+
// This is an unrelated test that should be ignored by the enumify generator.
11+
func TestExample(t *testing.T) {
12+
example, err := example.New()
13+
require.NoError(t, err)
14+
require.NotNil(t, example)
15+
require.NotZero(t, example.Name)
16+
require.False(t, example.Date.IsZero())
17+
}

example/status.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package example
2+
3+
// Status is an enum type that should be implemented by the enumify generator.
4+
// It uses a 2D array of strings for the names, which should be discovered by the
5+
// enumify generator due to the go:generate directive above the Status type declaration.
6+
//
7+
//go:generate go run ../cmd/enumify -names statusTable
8+
type Status uint8
9+
10+
// Constants for the Status enum values.
11+
// These values should be discovered by the enumify generator since they use the same
12+
// type as the Status enum, which is the type being generated.
13+
const (
14+
StatusUnknown Status = iota
15+
StatusPending
16+
StatusRunning
17+
StatusFailed
18+
StatusSuccess
19+
StatusCancelled
20+
)
21+
22+
// 2D array of strings for the names of the Status enum values.
23+
// This should be discovered by the enumify generator due to the go:generate directive
24+
// that specifies this variable as the names for the Status enum.
25+
//
26+
//lint:ignore U1000 this is used by the enumify generator
27+
var statusTable = [][]string{
28+
{"unknown", "pending", "running", "failed", "success", "cancelled"},
29+
{"text-secondary", "text-info", "text-primary", "text-danger", "text-success", "text-warning"},
30+
}

0 commit comments

Comments
 (0)