2015-12-29 16:14:54 +03:00
|
|
|
// 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"
|
2015-12-29 16:14:54 +03:00
|
|
|
"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.
|
2015-12-29 16:14:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var golden = []Golden{
|
2020-08-09 07:14:17 +03:00
|
|
|
{"day", dayIn},
|
|
|
|
{"offset", offsetIn},
|
|
|
|
{"gap", gapIn},
|
|
|
|
{"num", numIn},
|
|
|
|
{"unum", unumIn},
|
|
|
|
{"prime", primeIn},
|
2015-12-29 16:14:54 +03:00
|
|
|
}
|
|
|
|
|
2024-02-15 10:19:41 +03:00
|
|
|
var goldenToString = []Golden{
|
|
|
|
{"tostring_day", dayIn},
|
|
|
|
{"tostring_offset", offsetIn},
|
|
|
|
{"tostring_gap", gapIn},
|
|
|
|
{"tostring_num", numIn},
|
|
|
|
{"tostring_unum", unumIn},
|
|
|
|
{"tostring_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
|
|
|
}
|
2018-03-08 22:51:11 +03:00
|
|
|
var goldenText = []Golden{
|
2020-08-09 07:14:17 +03:00
|
|
|
{"primeText", primeTextIn},
|
2018-03-08 22:51:11 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-09-18 16:34:49 +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
|
|
|
}
|
|
|
|
|
2020-02-24 00:18:32 +03:00
|
|
|
var goldenTrimPrefixMultiple = []Golden{
|
2020-08-09 07:14:17 +03:00
|
|
|
{"trimPrefixMultiple", trimPrefixMultipleIn},
|
2020-02-24 00:18:32 +03:00
|
|
|
}
|
|
|
|
|
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},
|
2018-02-06 03:48:43 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 21:16:02 +03:00
|
|
|
var goldenLinecomment = []Golden{
|
2020-08-09 07:14:17 +03:00
|
|
|
{"dayWithLinecomment", linecommentIn},
|
2020-06-08 21:16:02 +03:00
|
|
|
}
|
|
|
|
|
2015-12-29 16:14:54 +03:00
|
|
|
// 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
|
2015-12-29 16:14:54 +03:00
|
|
|
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
|
2015-12-29 16:14:54 +03:00
|
|
|
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
|
2015-12-29 16:14:54 +03:00
|
|
|
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
|
2015-12-29 16:14:54 +03:00
|
|
|
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
|
2015-12-29 16:14:54 +03:00
|
|
|
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
|
2015-12-29 16:14:54 +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 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
|
2018-03-08 22:51:11 +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 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
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
2020-09-18 16:34:49 +03:00
|
|
|
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
|
2018-02-06 03:48:43 +03:00
|
|
|
const (
|
|
|
|
DayMonday Day = iota
|
|
|
|
DayTuesday
|
|
|
|
DayWednesday
|
|
|
|
DayThursday
|
|
|
|
DayFriday
|
|
|
|
DaySaturday
|
|
|
|
DaySunday
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
2020-02-24 00:18:32 +03:00
|
|
|
const trimPrefixMultipleIn = `type Day int
|
|
|
|
const (
|
|
|
|
DayMonday Day = iota
|
|
|
|
NightTuesday
|
|
|
|
DayWednesday
|
|
|
|
NightThursday
|
|
|
|
DayFriday
|
|
|
|
NightSaturday
|
|
|
|
DaySunday
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
2020-06-08 21:16:02 +03:00
|
|
|
const linecommentIn = `type Day int
|
|
|
|
const (
|
|
|
|
Monday Day = iota // lunes
|
|
|
|
Tuesday
|
|
|
|
Wednesday
|
|
|
|
Thursday
|
|
|
|
Friday // viernes
|
|
|
|
Saturday
|
|
|
|
Sunday
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
2015-12-29 16:14:54 +03:00
|
|
|
func TestGolden(t *testing.T) {
|
|
|
|
for _, test := range golden {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, false, false, false, false, false, true, false, "", "")
|
|
|
|
}
|
|
|
|
for _, test := range goldenToString {
|
|
|
|
runGoldenTest(t, test, false, false, false, false, false, false, false, true, "", "")
|
2016-01-19 22:54:00 +03:00
|
|
|
}
|
|
|
|
for _, test := range goldenJSON {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, true, false, false, false, false, false, false, false, "", "")
|
2018-03-08 22:51:11 +03:00
|
|
|
}
|
|
|
|
for _, test := range goldenText {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, false, false, true, false, false, false, false, "", "")
|
2016-12-20 15:57:15 +03:00
|
|
|
}
|
|
|
|
for _, test := range goldenYAML {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, true, false, false, false, false, false, false, "", "")
|
2016-10-26 18:59:55 +03:00
|
|
|
}
|
|
|
|
for _, test := range goldenSQL {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, false, true, false, false, false, false, false, "", "")
|
2016-10-26 18:59:55 +03:00
|
|
|
}
|
|
|
|
for _, test := range goldenJSONAndSQL {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, true, false, true, false, false, false, false, false, "", "")
|
2020-09-18 16:34:49 +03:00
|
|
|
}
|
|
|
|
for _, test := range goldenGQLGen {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, false, false, false, false, true, false, false, "", "")
|
2018-02-06 03:48:43 +03:00
|
|
|
}
|
2019-03-22 12:54:07 +03:00
|
|
|
for _, test := range goldenTrimPrefix {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, false, false, false, false, false, false, false, "Day", "")
|
2018-02-06 03:48:43 +03:00
|
|
|
}
|
2020-02-24 00:18:32 +03:00
|
|
|
for _, test := range goldenTrimPrefixMultiple {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, false, false, false, false, false, false, false, "Day,Night", "")
|
2020-02-24 00:18:32 +03:00
|
|
|
}
|
2019-03-22 12:54:07 +03:00
|
|
|
for _, test := range goldenWithPrefix {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, 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 {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, false, false, false, false, false, false, false, "Day", "Night")
|
2020-06-08 21:16:02 +03:00
|
|
|
}
|
|
|
|
for _, test := range goldenLinecomment {
|
2024-02-15 10:19:41 +03:00
|
|
|
runGoldenTest(t, test, false, false, false, false, true, false, false, false, "", "")
|
2016-01-19 22:54:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:34:49 +03:00
|
|
|
func runGoldenTest(t *testing.T, test Golden,
|
2024-02-15 10:19:41 +03:00
|
|
|
generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateValuesMethod, toString bool,
|
2020-09-18 16:34:49 +03:00
|
|
|
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)
|
|
|
|
}
|
2024-02-15 10:19:41 +03:00
|
|
|
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment, generateValuesMethod, toString)
|
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) {
|
2019-05-04 05:47:28 +03:00
|
|
|
// 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)
|
2019-05-04 05:47:28 +03:00
|
|
|
//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))
|
2015-12-29 16:14:54 +03:00
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
}
|