forked from mirror/enumer
Updated tests
This commit is contained in:
parent
06fa207af0
commit
8ff8ff6be4
|
@ -30,6 +30,10 @@ var golden = []Golden{
|
||||||
{"prime", prime_in, prime_out},
|
{"prime", prime_in, prime_out},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var goldenJSON = []Golden {
|
||||||
|
{"prime", prime_json_in, prime_json_out},
|
||||||
|
}
|
||||||
|
|
||||||
// Each example starts with "type XXX [u]int", with a single space separating them.
|
// Each example starts with "type XXX [u]int", with a single space separating them.
|
||||||
|
|
||||||
// Simple test: enumeration of type int starting at 0.
|
// Simple test: enumeration of type int starting at 0.
|
||||||
|
@ -338,9 +342,100 @@ func PrimeString(s string) (Prime, error) {
|
||||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
const prime_json_in = `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 prime_json_out = `
|
||||||
|
const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||||
|
|
||||||
|
var _Prime_map = map[Prime]string{
|
||||||
|
2: _Prime_name[0:2],
|
||||||
|
3: _Prime_name[2:4],
|
||||||
|
5: _Prime_name[4:6],
|
||||||
|
7: _Prime_name[6:8],
|
||||||
|
11: _Prime_name[8:11],
|
||||||
|
13: _Prime_name[11:14],
|
||||||
|
17: _Prime_name[14:17],
|
||||||
|
19: _Prime_name[17:20],
|
||||||
|
23: _Prime_name[20:23],
|
||||||
|
29: _Prime_name[23:26],
|
||||||
|
31: _Prime_name[26:29],
|
||||||
|
41: _Prime_name[29:32],
|
||||||
|
43: _Prime_name[32:35],
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Prime) String() string {
|
||||||
|
if str, ok := _Prime_map[i]; ok {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("Prime(%d)", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _PrimeNameToValue_map = map[string]Prime{
|
||||||
|
_Prime_name[0:2]: 2,
|
||||||
|
_Prime_name[2:4]: 3,
|
||||||
|
_Prime_name[4:6]: 5,
|
||||||
|
_Prime_name[6:8]: 7,
|
||||||
|
_Prime_name[8:11]: 11,
|
||||||
|
_Prime_name[11:14]: 13,
|
||||||
|
_Prime_name[14:17]: 17,
|
||||||
|
_Prime_name[17:20]: 19,
|
||||||
|
_Prime_name[20:23]: 23,
|
||||||
|
_Prime_name[23:26]: 29,
|
||||||
|
_Prime_name[26:29]: 31,
|
||||||
|
_Prime_name[29:32]: 41,
|
||||||
|
_Prime_name[32:35]: 43,
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrimeString(s string) (Prime, error) {
|
||||||
|
if val, ok := _PrimeNameToValue_map[s]; ok {
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Prime) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(i.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Prime) UnmarshalJSON(data []byte) error {
|
||||||
|
var s string
|
||||||
|
if err := json.Unmarshal(data, &s); err != nil {
|
||||||
|
return fmt.Errorf("Prime should be a string, got %s", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
*i, err = PrimeString(s)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
func TestGolden(t *testing.T) {
|
func TestGolden(t *testing.T) {
|
||||||
for _, test := range golden {
|
for _, test := range golden {
|
||||||
|
runGoldenTest(t, test, false)
|
||||||
|
}
|
||||||
|
for _, test := range goldenJSON {
|
||||||
|
runGoldenTest(t, test, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runGoldenTest(t *testing.T, test Golden, generateJSON bool) {
|
||||||
var g Generator
|
var g Generator
|
||||||
input := "package test\n" + test.input
|
input := "package test\n" + test.input
|
||||||
file := test.name + ".go"
|
file := test.name + ".go"
|
||||||
|
@ -350,10 +445,9 @@ func TestGolden(t *testing.T) {
|
||||||
if len(tokens) != 3 {
|
if len(tokens) != 3 {
|
||||||
t.Fatalf("%s: need type declaration on first line", test.name)
|
t.Fatalf("%s: need type declaration on first line", test.name)
|
||||||
}
|
}
|
||||||
g.generate(tokens[1])
|
g.generate(tokens[1], generateJSON)
|
||||||
got := string(g.format())
|
got := string(g.format())
|
||||||
if got != test.output {
|
if got != test.output {
|
||||||
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output)
|
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -140,7 +140,7 @@ func main() {
|
||||||
|
|
||||||
// Run generate for each type.
|
// Run generate for each type.
|
||||||
for _, typeName := range types {
|
for _, typeName := range types {
|
||||||
g.generate(typeName)
|
g.generate(typeName, !*noJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format the output.
|
// Format the output.
|
||||||
|
@ -276,7 +276,7 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate produces the String method for the named type.
|
// generate produces the String method for the named type.
|
||||||
func (g *Generator) generate(typeName string) {
|
func (g *Generator) generate(typeName string, includeJSON bool) {
|
||||||
values := make([]Value, 0, 100)
|
values := make([]Value, 0, 100)
|
||||||
for _, file := range g.pkg.files {
|
for _, file := range g.pkg.files {
|
||||||
// Set the state for this run of the walker.
|
// Set the state for this run of the walker.
|
||||||
|
@ -315,7 +315,7 @@ func (g *Generator) generate(typeName string) {
|
||||||
}
|
}
|
||||||
// ENUMER part
|
// ENUMER part
|
||||||
g.buildValueToNameMap(runs, typeName, runsThreshold)
|
g.buildValueToNameMap(runs, typeName, runsThreshold)
|
||||||
if !*noJSON {
|
if includeJSON {
|
||||||
g.buildJSONMethods(runs, typeName, runsThreshold)
|
g.buildJSONMethods(runs, typeName, runsThreshold)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue