This commit is contained in:
Luca Osti 2019-03-22 11:36:20 +01:00
parent 5511caae23
commit d806ebd41f
1 changed files with 58 additions and 2 deletions

View File

@ -57,6 +57,10 @@ var goldenWithPrefix = []Golden{
{"with prefix", dayIn, prefixedDayOut},
}
var goldenTrimAndAddPrefix = []Golden{
{"trim and add prefix", trimPrefixIn, trimmedPrefixedDayOut},
}
// Each example starts with "type XXX [u]int", with a single space separating them.
// Simple test: enumeration of type int starting at 0.
@ -170,6 +174,55 @@ func (i Day) IsADay() bool {
}
`
const trimmedPrefixedDayOut = `
const _DayName = "NightMondayNightTuesdayNightWednesdayNightThursdayNightFridayNightSaturdayNightSunday"
var _DayIndex = [...]uint8{0, 11, 23, 37, 50, 61, 74, 85}
func (i Day) String() string {
if i < 0 || i >= Day(len(_DayIndex)-1) {
return fmt.Sprintf("Day(%d)", i)
}
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
}
var _DayValues = []Day{0, 1, 2, 3, 4, 5, 6}
var _DayNameToValueMap = map[string]Day{
_DayName[0:11]: 0,
_DayName[11:23]: 1,
_DayName[23:37]: 2,
_DayName[37:50]: 3,
_DayName[50:61]: 4,
_DayName[61:74]: 5,
_DayName[74:85]: 6,
}
// DayString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
func DayString(s string) (Day, error) {
if val, ok := _DayNameToValueMap[s]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to Day values", s)
}
// DayValues returns all values of the enum
func DayValues() []Day {
return _DayValues
}
// IsADay returns "true" if the value is listed in the enum definition. "false" otherwise
func (i Day) IsADay() bool {
for _, v := range _DayValues {
if i == v {
return true
}
}
return false
}
`
// Enumeration with an offset.
// Also includes a duplicate.
const offsetIn = `type Number int
@ -1100,6 +1153,9 @@ func TestGolden(t *testing.T) {
for _, test := range goldenWithPrefix {
runGoldenTest(t, test, false, false, false, false, "", "Day")
}
for _, test := range goldenTrimAndAddPrefix {
runGoldenTest(t, test, false, false, false, false, "Day", "Night")
}
}
func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, generateSQL, generateText bool, trimPrefix string, prefix string) {