Skip to content

Commit

Permalink
Sync with upstream (#6)
Browse files Browse the repository at this point in the history
Sync with upstream
  • Loading branch information
stefanvanburen authored Oct 7, 2019
2 parents b69fb7c + 6f022fd commit 40b198b
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 164 deletions.
10 changes: 0 additions & 10 deletions .github/main.workflow

This file was deleted.

1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ jobs:
- name: Test
run: |
cd api
go test -v -race ./...
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ A fork of [alvaroloes/enumer](https://github.com/alvaroloes/enumer)
Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type).
It started as a fork of [Rob Pike’s Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer).

## Install
Enumer can be installed as any other go command:

```
go get github.com/svanburen/enumer
```
After that, the `enumer` executable will be in "$GOPATH/bin" folder and you can use it with `go generate`


## Generated functions and methods

Expand Down
7 changes: 7 additions & 0 deletions endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ func copy(to, from string) error {
// run runs a single command and returns an error if it does not succeed.
// os/exec should have this function, to be honest.
func run(name string, arg ...string) error {
return runInDir(".", name, arg...)
}

// runInDir runs a single command in directory dir and returns an error if
// it does not succeed.
func runInDir(dir, name string, arg ...string) error {
cmd := exec.Command(name, arg...)
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
Expand Down
2 changes: 1 addition & 1 deletion enumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThresh
// Print the basic extra methods
g.Printf(stringNameToValueMethod, typeName)
g.Printf(stringValuesMethod, typeName)
if len(runs) < runsThreshold {
if len(runs) <= runsThreshold {
g.Printf(stringBelongsMethodLoop, typeName)
} else { // There is a map of values, the code is simpler then
g.Printf(stringBelongsMethodSet, typeName)
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/svanburen/enumer

go 1.13

require github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1
require (
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1
golang.org/x/tools v0.0.0-20191007185444-6536af71d98a
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1 h1:/I3lTljEEDNYLho3/FUB7iD/oc2cEFgVmbHzV+O0PtU=
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1/go.mod h1:eD5JxqMiuNYyFNmyY9rkJ/slN8y59oEu4Ei7F8OoKWQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191007185444-6536af71d98a h1:mtF1GhqcFEC1RVSQxvgrZWOM22dax6fiM9VfcQoTv6U=
golang.org/x/tools v0.0.0-20191007185444-6536af71d98a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
191 changes: 116 additions & 75 deletions golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
Expand All @@ -18,7 +21,7 @@ import (
type Golden struct {
name string
input string // input; the package clause is provided when running the test.
output string // exected output.
output string // expected output.
}

var golden = []Golden{
Expand All @@ -31,23 +34,23 @@ var golden = []Golden{
}

var goldenJSON = []Golden{
{"prime", primeJsonIn, primeJsonOut},
{"prime with JSON", primeJsonIn, primeJsonOut},
}

var goldenText = []Golden{
{"prime", primeTextIn, primeTextOut},
{"prime with Text", primeTextIn, primeTextOut},
}

var goldenYAML = []Golden{
{"prime", primeYamlIn, primeYamlOut},
{"prime with YAML", primeYamlIn, primeYamlOut},
}

var goldenSQL = []Golden{
{"prime", primeSqlIn, primeSqlOut},
{"prime with SQL", primeSqlIn, primeSqlOut},
}

var goldenJSONAndSQL = []Golden{
{"prime", primeJsonAndSqlIn, primeJsonAndSqlOut},
{"prime with JSONAndSQL", primeJsonAndSqlIn, primeJsonAndSqlOut},
}

var goldenPrefix = []Golden{
Expand All @@ -58,6 +61,10 @@ var goldenGraphQL = []Golden{
{"day", dayIn, dayGraphQLOut},
}

var goldenWithLineComments = []Golden{
{"primer with line Comments", primeWithLineCommentIn, primeWithLineCommentOut},
}

// Each example starts with "type XXX [u]int", with a single space separating them.

// Simple test: enumeration of type int starting at 0.
Expand Down Expand Up @@ -1111,6 +1118,90 @@ const (
)
`

const primeWithLineCommentIn = `type Prime int
const (
p2 Prime = 2
p3 Prime = 3
p5 Prime = 5
p7 Prime = 7
p77 Prime = 7 // Duplicate; note that p77 doesn't appear below.
p11 Prime = 11
p13 Prime = 13
p17 Prime = 17
p19 Prime = 19
p23 Prime = 23
p29 Prime = 29
p37 Prime = 31
p41 Prime = 41
p43 Prime = 43
)
`

const primeWithLineCommentOut = `
const _PrimeName = "p2p3GoodPrimep7p11p13p17p19p23p29p37TwinPrime41Twin prime 43"
var _PrimeMap = map[Prime]string{
2: _PrimeName[0:2],
3: _PrimeName[2:4],
5: _PrimeName[4:13],
7: _PrimeName[13:15],
11: _PrimeName[15:18],
13: _PrimeName[18:21],
17: _PrimeName[21:24],
19: _PrimeName[24:27],
23: _PrimeName[27:30],
29: _PrimeName[30:33],
31: _PrimeName[33:36],
41: _PrimeName[36:47],
43: _PrimeName[47:60],
}
func (i Prime) String() string {
if str, ok := _PrimeMap[i]; ok {
return str
}
return fmt.Sprintf("Prime(%d)", i)
}
var _PrimeValues = []Prime{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43}
var _PrimeNameToValueMap = map[string]Prime{
_PrimeName[0:2]: 2,
_PrimeName[2:4]: 3,
_PrimeName[4:13]: 5,
_PrimeName[13:15]: 7,
_PrimeName[15:18]: 11,
_PrimeName[18:21]: 13,
_PrimeName[21:24]: 17,
_PrimeName[24:27]: 19,
_PrimeName[27:30]: 23,
_PrimeName[30:33]: 29,
_PrimeName[33:36]: 31,
_PrimeName[36:47]: 41,
_PrimeName[47:60]: 43,
}
// PrimeString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func PrimeString(s string) (Prime, error) {
if val, ok := _PrimeNameToValueMap[s]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Prime values", s)
}
// PrimeValues returns all values of the enum
func PrimeValues() []Prime {
return _PrimeValues
}
// IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Prime) IsAPrime() bool {
_, ok := _PrimeMap[i]
return ok
}
`

func TestGolden(t *testing.T) {
for _, test := range golden {
runGoldenTest(t, test, false, false, false, false, false, "")
Expand Down Expand Up @@ -1142,82 +1233,32 @@ func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, genera
var g Generator
input := "package test\n" + test.input
file := test.name + ".go"
g.parsePackage(".", []string{file}, input)

dir, err := ioutil.TempDir("", "stringer")
if err != nil {
t.Error(err)
}
defer func() {
err = os.RemoveAll(dir)
if err != nil {
t.Error(err)
}
}()

absFile := filepath.Join(dir, file)
err = ioutil.WriteFile(absFile, []byte(input), 0644)
if err != nil {
t.Error(err)
}
g.parsePackage([]string{absFile})
// Extract the name and type of the constant from the first line.
tokens := strings.SplitN(test.input, " ", 3)
if len(tokens) != 3 {
t.Fatalf("%s: need type declaration on first line", test.name)
}
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateGraphQLGo, generateText, "noop", prefix)
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateGraphQLGo, generateText, "noop", prefix, false)
got := string(g.format())
if got != test.output {
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output)
}
}

func runGoldenBench(b *testing.B, test Golden, generateJSON, generateYAML, generateSQL, generateGraphQLGo, generateText bool, prefix string) {
for n := 0; n < b.N; n++ {
var g Generator
input := "package test\n" + test.input
file := test.name + ".go"
g.parsePackage(".", []string{file}, input)
// Extract the name and type of the constant from the first line.
tokens := strings.SplitN(test.input, " ", 3)
if len(tokens) != 3 {
b.Fatalf("%s: need type declaration on first line", test.name)
}
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateGraphQLGo, generateText, "noop", prefix)
got := string(g.format())
if got != test.output {
b.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output)
}
}
}

func BenchmarkGolden(b *testing.B) {
for _, test := range golden {
runGoldenBench(b, test, false, false, false, false, false, "")
}
}

func BenchmarkGoldenJSON(b *testing.B) {
for _, test := range goldenJSON {
runGoldenBench(b, test, true, false, false, false, false, "")
}
}

func BenchmarkGoldenText(b *testing.B) {
for _, test := range goldenText {
runGoldenBench(b, test, false, false, false, false, true, "")
}
}

func BenchmarkGoldenYAML(b *testing.B) {
for _, test := range goldenYAML {
runGoldenBench(b, test, false, true, false, false, false, "")
}
}

func BenchmarkGoldenSQL(b *testing.B) {
for _, test := range goldenSQL {
runGoldenBench(b, test, false, false, true, false, false, "")
}
}

func BenchmarkGoldenJSONAndSQL(b *testing.B) {
for _, test := range goldenJSONAndSQL {
runGoldenBench(b, test, true, false, true, false, false, "")
}
}

func BenchmarkGoldenPrefix(b *testing.B) {
for _, test := range goldenPrefix {
runGoldenBench(b, test, false, false, false, false, false, "Day")
}
}

func BenchmarkGoldenGraphQL(b *testing.B) {
for _, test := range goldenGraphQL {
runGoldenBench(b, test, false, false, false, true, false, "")
}
}
Loading

0 comments on commit 40b198b

Please sign in to comment.