enumer/golden_test.go

411 lines
8.3 KiB
Go
Raw Normal View History

// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains simple golden tests for various examples.
// Besides validating the results when the implementation changes,
// it provides a way to look at the generated code without having
// to execute the print statements in one's head.
package main
import (
2018-12-29 20:20:08 +03:00
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
// Golden represents a test case.
type Golden struct {
2020-08-09 07:14:17 +03:00
name string
input string // input; the package clause is provided when running the test.
//output string // expected output.
}
var golden = []Golden{
2020-08-09 07:14:17 +03:00
{"day", dayIn},
{"offset", offsetIn},
{"gap", gapIn},
{"num", numIn},
{"unum", unumIn},
{"prime", primeIn},
}
2016-05-21 15:34:12 +03:00
var goldenJSON = []Golden{
2020-08-09 07:14:17 +03:00
{"primeJson", primeJsonIn},
2016-01-19 22:54:00 +03:00
}
var goldenText = []Golden{
2020-08-09 07:14:17 +03:00
{"primeText", primeTextIn},
}
2016-01-19 22:54:00 +03:00
2016-12-20 15:57:15 +03:00
var goldenYAML = []Golden{
2020-08-09 07:14:17 +03:00
{"primeYaml", primeYamlIn},
2016-12-20 15:57:15 +03:00
}
2016-10-26 18:59:55 +03:00
var goldenSQL = []Golden{
2020-08-09 07:14:17 +03:00
{"primeSql", primeSqlIn},
2016-10-26 18:59:55 +03:00
}
var goldenGQLGen = []Golden{
{"primeGQLGen", primeGQLGenIn},
}
2016-10-26 18:59:55 +03:00
var goldenJSONAndSQL = []Golden{
2020-08-09 07:14:17 +03:00
{"primeJsonAndSql", primeJsonAndSqlIn},
2016-10-26 18:59:55 +03:00
}
2019-03-22 12:54:07 +03:00
var goldenTrimPrefix = []Golden{
2020-08-09 07:14:17 +03:00
{"trimPrefix", trimPrefixIn},
2019-03-22 12:54:07 +03:00
}
var goldenTrimPrefixMultiple = []Golden{
2020-08-09 07:14:17 +03:00
{"trimPrefixMultiple", trimPrefixMultipleIn},
}
2019-03-22 12:54:07 +03:00
var goldenWithPrefix = []Golden{
2020-08-09 07:14:17 +03:00
{"dayWithPrefix", dayIn},
2019-03-22 13:36:20 +03:00
}
var goldenTrimAndAddPrefix = []Golden{
2020-08-09 07:14:17 +03:00
{"dayTrimAndPrefix", trimPrefixIn},
}
var goldenLinecomment = []Golden{
2020-08-09 07:14:17 +03:00
{"dayWithLinecomment", linecommentIn},
}
// Each example starts with "type XXX [u]int", with a single space separating them.
// Simple test: enumeration of type int starting at 0.
2018-03-09 19:55:00 +03:00
const dayIn = `type Day int
const (
Monday Day = iota
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
)
`
// Enumeration with an offset.
// Also includes a duplicate.
2018-03-09 19:55:00 +03:00
const offsetIn = `type Number int
const (
_ Number = iota
One
Two
Three
AnotherOne = One // Duplicate; note that AnotherOne doesn't appear below.
)
`
// Gaps and an offset.
2018-03-09 19:55:00 +03:00
const gapIn = `type Gap int
const (
Two Gap = 2
Three Gap = 3
Five Gap = 5
Six Gap = 6
Seven Gap = 7
Eight Gap = 8
Nine Gap = 9
Eleven Gap = 11
)
`
// Signed integers spanning zero.
2018-03-09 19:55:00 +03:00
const numIn = `type Num int
const (
m_2 Num = -2 + iota
m_1
m0
m1
m2
)
`
// Unsigned integers spanning zero.
2018-03-09 19:55:00 +03:00
const unumIn = `type Unum uint
const (
m_2 Unum = iota + 253
m_1
)
const (
m0 Unum = iota
m1
m2
)
`
// Enough gaps to trigger a map implementation of the method.
// Also includes a duplicate to test that it doesn't cause problems
2018-03-09 19:55:00 +03:00
const primeIn = `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
)
`
2018-03-09 19:55:00 +03:00
const primeJsonIn = `type Prime int
2016-01-19 22:54:00 +03:00
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
)
`
2018-03-09 19:55:00 +03:00
const primeTextIn = `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
)
`
2018-03-09 19:55:00 +03:00
const primeYamlIn = `type Prime int
2016-12-20 15:57:15 +03:00
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
)
`
2018-03-09 19:55:00 +03:00
const primeSqlIn = `type Prime int
2016-10-26 18:59:55 +03:00
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 primeGQLGenIn = `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
)
`
2018-03-09 19:55:00 +03:00
const primeJsonAndSqlIn = `type Prime int
2016-10-26 18:59:55 +03:00
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
)
`
2019-03-22 12:54:07 +03:00
const trimPrefixIn = `type Day int
const (
DayMonday Day = iota
DayTuesday
DayWednesday
DayThursday
DayFriday
DaySaturday
DaySunday
)
`
const trimPrefixMultipleIn = `type Day int
const (
DayMonday Day = iota
NightTuesday
DayWednesday
NightThursday
DayFriday
NightSaturday
DaySunday
)
`
const linecommentIn = `type Day int
const (
Monday Day = iota // lunes
Tuesday
Wednesday
Thursday
Friday // viernes
Saturday
Sunday
)
`
func TestGolden(t *testing.T) {
for _, test := range golden {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, false, false, false, false, true, "", "")
2016-01-19 22:54:00 +03:00
}
for _, test := range goldenJSON {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, true, false, false, false, false, false, false, "", "")
}
for _, test := range goldenText {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, false, true, false, false, false, "", "")
2016-12-20 15:57:15 +03:00
}
for _, test := range goldenYAML {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, true, false, false, false, false, false, "", "")
2016-10-26 18:59:55 +03:00
}
for _, test := range goldenSQL {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, true, false, false, false, false, "", "")
2016-10-26 18:59:55 +03:00
}
for _, test := range goldenJSONAndSQL {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, true, false, true, false, false, false, false, "", "")
}
for _, test := range goldenGQLGen {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, false, false, false, true, false, "", "")
}
2019-03-22 12:54:07 +03:00
for _, test := range goldenTrimPrefix {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, false, false, false, false, false, "Day", "")
}
for _, test := range goldenTrimPrefixMultiple {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, false, false, false, false, false, "Day,Night", "")
}
2019-03-22 12:54:07 +03:00
for _, test := range goldenWithPrefix {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, false, false, false, false, false, "", "Day")
2016-01-19 22:54:00 +03:00
}
2019-03-22 13:36:20 +03:00
for _, test := range goldenTrimAndAddPrefix {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, false, false, false, false, false, "Day", "Night")
}
for _, test := range goldenLinecomment {
2021-10-17 15:31:43 +03:00
runGoldenTest(t, test, false, false, false, false, true, false, false, "", "")
2016-01-19 22:54:00 +03:00
}
}
func runGoldenTest(t *testing.T, test Golden,
2021-10-17 15:31:43 +03:00
generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateValuesMethod bool,
trimPrefix string, prefix string) {
2016-01-19 22:54:00 +03:00
var g Generator
file := test.name + ".go"
2018-12-29 20:20:08 +03:00
input := "package test\n" + test.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}, nil)
2016-01-19 22:54:00 +03:00
// 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)
}
2021-10-17 15:31:43 +03:00
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment, generateValuesMethod)
2016-01-19 22:54:00 +03:00
got := string(g.format())
2020-08-09 07:14:17 +03:00
if got != loadGolden(test.name) {
// Use this to help build a golden text when changes are needed
2020-08-09 07:14:17 +03:00
//goldenFile := fmt.Sprintf("./testdata/%v.golden", test.name)
//err = ioutil.WriteFile(goldenFile, []byte(got), 0644)
//if err != nil {
// t.Error(err)
//}
2020-08-09 07:14:17 +03:00
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, loadGolden(test.name))
}
}
2020-08-09 07:14:17 +03:00
func loadGolden(name string) string {
fh, err := os.Open("testdata/" + name + ".golden")
if err != nil {
return ""
}
defer fh.Close()
b, err := ioutil.ReadAll(fh)
if err != nil {
return ""
}
return string(b)
}