Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
// go command is not available on android

//go:build !android
// +build !android

package main

import (
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -22,11 +20,9 @@ import (
"testing"
)

var (
// GOEXE defines the executable file name suffix (".exe" on Windows, "" on other systems).
// Must be defined here, cannot be read from ENVIRONMENT variables
GOEXE = ""
)
// GOEXE defines the executable file name suffix (".exe" on Windows, "" on other systems).
// Must be defined here, cannot be read from ENVIRONMENT variables
var GOEXE = ""

func init() {
// Set GOEXE for Windows platform
Expand All @@ -41,15 +37,11 @@ func init() {
// binary panics if the String method for X is not correct, including for error cases.

func TestEndToEnd(t *testing.T) {
dir, err := ioutil.TempDir("", "stringer")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

// Create stringer in temporary directory.
stringer := filepath.Join(dir, fmt.Sprintf("stringer%s", GOEXE))
err = run("go", "build", "-o", stringer)
err := run("go", "build", "-o", stringer)
if err != nil {
t.Fatalf("building stringer: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions enumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (g *Generator) printValueMap(runs [][]Value, typeName string, runsThreshold
}
g.Printf("}\n\n")
}

func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThreshold int) {
thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold
g.Printf("\nvar _%sNames = []string{\n", typeName)
Expand Down
48 changes: 22 additions & 26 deletions golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package main

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

var golden = []Golden{
Expand All @@ -36,6 +36,7 @@ var golden = []Golden{
var goldenJSON = []Golden{
{"primeJson", primeJsonIn},
}

var goldenText = []Golden{
{"primeText", primeTextIn},
}
Expand Down Expand Up @@ -369,25 +370,16 @@ func TestGolden(t *testing.T) {

func runGoldenTest(t *testing.T, test Golden,
generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateValuesMethod bool,
trimPrefix string, prefix string, useTypedErrors bool) {
trimPrefix, prefix string, useTypedErrors bool) {

var g Generator
file := test.name + ".go"
input := "package test\n" + test.input

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

absFile := filepath.Join(dir, file)
err = os.WriteFile(absFile, []byte(input), 0644)
err := os.WriteFile(absFile, []byte(input), 0o644)
if err != nil {
t.Error(err)
}
Expand All @@ -399,27 +391,31 @@ func runGoldenTest(t *testing.T, test Golden,
}
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment, generateValuesMethod, useTypedErrors)
got := string(g.format())
if got != loadGolden(test.name) {
if expected := loadGolden(t, test.name); got != expected {
// Use this to help build a golden text when changes are needed
// goldenFile := fmt.Sprintf("./testdata/%v.golden", test.name)
// err = os.WriteFile(goldenFile, []byte(got), 0644)
// if err != nil {
// t.Error(err)
// }
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, loadGolden(test.name))
//goldenFile := fmt.Sprintf("./testdata/%v.golden", test.name)
//err = os.WriteFile(goldenFile, []byte(got), 0644)
//if err != nil {
// t.Error(err)
//}
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, expected)
}
}

func loadGolden(name string) string {
func loadGolden(t *testing.T, name string) string {
t.Helper()

fh, err := os.Open("testdata/" + name + ".golden")
if err != nil {
return ""
t.Fatalf("unable to open golden file for %s: %v", name, err)
}

defer fh.Close()
b, err := ioutil.ReadAll(fh)

b, err := io.ReadAll(fh)
if err != nil {
return ""
t.Fatalf("unable to read golden file for %s: %v", name, err)
}
return string(b)

return string(b)
}
17 changes: 9 additions & 8 deletions stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"go/importer"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -154,7 +153,7 @@ func main() {
}

// Write to tmpfile first
tmpFile, err := ioutil.TempFile(dir, fmt.Sprintf("%s_enumer_", typs[0]))
tmpFile, err := os.CreateTemp(dir, fmt.Sprintf("%s_enumer_", typs[0]))
if err != nil {
log.Fatalf("creating temporary file for output: %s", err)
}
Expand Down Expand Up @@ -250,7 +249,7 @@ type Package struct {

// parsePackage analyzes the single package constructed from the patterns and tags.
// parsePackage exits if there is an error.
func (g *Generator) parsePackage(patterns []string, tags []string) {
func (g *Generator) parsePackage(patterns, tags []string) {
cfg := &packages.Config{
Mode: packages.LoadSyntax,
// TODO: Need to think about constants in test files. Maybe write type_string_test.go
Expand Down Expand Up @@ -420,7 +419,9 @@ func (g *Generator) prefixValueNames(values []Value, prefix string) {
// generate produces the String method for the named type.
func (g *Generator) generate(typeName string,
includeJSON, includeYAML, includeSQL, includeText, includeGQLGen bool,
transformMethod string, trimPrefix string, addPrefix string, lineComment bool, includeValuesMethod bool, useTypedErrors bool) {
transformMethod, trimPrefix, addPrefix string,
lineComment, includeValuesMethod, useTypedErrors bool,
) {
values := make([]Value, 0, 100)
for _, file := range g.pkg.files {
file.lineComment = lineComment
Expand Down Expand Up @@ -696,11 +697,11 @@ func (g *Generator) declareIndexAndNameVar(run []Value, typeName string) {
g.Printf("var %s\n", index)
index, n = g.createLowerIndexAndNameDecl(run, typeName, "")
g.Printf("const %s\n", n)
//g.Printf("var %s\n", index)
// g.Printf("var %s\n", index)
}

// createIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var".
func (g *Generator) createLowerIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string) {
func (g *Generator) createLowerIndexAndNameDecl(run []Value, typeName, suffix string) (string, string) {
b := new(bytes.Buffer)
indexes := make([]int, len(run))
for i := range run {
Expand All @@ -722,7 +723,7 @@ func (g *Generator) createLowerIndexAndNameDecl(run []Value, typeName string, su
}

// createIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var".
func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string) {
func (g *Generator) createIndexAndNameDecl(run []Value, typeName, suffix string) (string, string) {
b := new(bytes.Buffer)
indexes := make([]int, len(run))
for i := range run {
Expand All @@ -744,7 +745,7 @@ func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix
}

// declareNameVars declares the concatenated names string representing all the values in the runs.
func (g *Generator) declareNameVars(runs [][]Value, typeName string, suffix string) {
func (g *Generator) declareNameVars(runs [][]Value, typeName, suffix string) {
g.Printf("const _%sName%s = \"", typeName, suffix)
for _, run := range runs {
for i := range run {
Expand Down
6 changes: 4 additions & 2 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
)

// Helpers to save typing in the test cases.
type u []uint64
type uu [][]uint64
type (
u []uint64
uu [][]uint64
)

type SplitTest struct {
input u
Expand Down