-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] Example Package for Testing and Documentation #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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", | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"}, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"}, | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test variable shadows imported package name
Low Severity
The local
examplevariable shadows the importedexamplepackage, making the package inaccessible for further calls withinTestExample. This is a confusing anti-pattern, especially in example code, and makes the test fragile if extended.Reviewed by Cursor Bugbot for commit 45ed293. Configure here.