1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
package parse_test
import (
"io/ioutil"
"log"
"strings"
"testing"
"github.com/cheekybits/genny/parse"
"github.com/stretchr/testify/assert"
)
var tests = []struct {
// input
filename string
pkgName string
in string
types []map[string]string
// expectations
expectedOut string
expectedErr error
}{
{
filename: "generic_queue.go",
in: `test/queue/generic_queue.go`,
types: []map[string]string{{"Something": "int"}},
expectedOut: `test/queue/int_queue.go`,
},
{
filename: "generic_queue.go",
pkgName: "changed",
in: `test/queue/generic_queue.go`,
types: []map[string]string{{"Something": "int"}},
expectedOut: `test/queue/changed/int_queue.go`,
},
{
filename: "generic_queue.go",
in: `test/queue/generic_queue.go`,
types: []map[string]string{{"Something": "float32"}},
expectedOut: `test/queue/float32_queue.go`,
},
{
filename: "generic_simplemap.go",
in: `test/multipletypes/generic_simplemap.go`,
types: []map[string]string{{"KeyType": "string", "ValueType": "int"}},
expectedOut: `test/multipletypes/string_int_simplemap.go`,
},
{
filename: "generic_simplemap.go",
in: `test/multipletypes/generic_simplemap.go`,
types: []map[string]string{{"KeyType": "interface{}", "ValueType": "int"}},
expectedOut: `test/multipletypes/interface_int_simplemap.go`,
},
{
filename: "generic_simplemap.go",
in: `test/multipletypes/generic_simplemap.go`,
types: []map[string]string{{"KeyType": "*MyType1", "ValueType": "*MyOtherType"}},
expectedOut: `test/multipletypes/custom_types_simplemap.go`,
},
{
filename: "generic_internal.go",
in: `test/unexported/generic_internal.go`,
types: []map[string]string{{"secret": "*myType"}},
expectedOut: `test/unexported/mytype_internal.go`,
},
{
filename: "generic_simplemap.go",
in: `test/multipletypesets/generic_simplemap.go`,
types: []map[string]string{
{"KeyType": "int", "ValueType": "string"},
{"KeyType": "float64", "ValueType": "bool"},
},
expectedOut: `test/multipletypesets/many_simplemaps.go`,
},
{
filename: "generic_number.go",
in: `test/numbers/generic_number.go`,
types: []map[string]string{{"NumberType": "int"}},
expectedOut: `test/numbers/int_number.go`,
},
{
filename: "generic_digraph.go",
in: `test/bugreports/generic_digraph.go`,
types: []map[string]string{{"Node": "int"}},
expectedOut: `test/bugreports/int_digraph.go`,
},
{
filename: "generic_new_and_make_slice.go",
in: `test/bugreports/generic_new_and_make_slice.go`,
types: []map[string]string{{"NumberType": "int"}},
expectedOut: `test/bugreports/int_new_and_make_slice.go`,
},
{
filename: "cell_x.go",
in: `test/bugreports/cell_x.go`,
types: []map[string]string{{"X": "int"}},
expectedOut: `test/bugreports/cell_int.go`,
},
{
filename: "interface_generic_type.go",
in: `test/bugreports/interface_generic_type.go`,
types: []map[string]string{{"GenericType": "uint8"}},
expectedOut: `test/bugreports/interface_uint8.go`,
},
{
filename: "negation_generic.go",
in: `test/bugreports/negation_generic.go`,
types: []map[string]string{{"SomeThing": "string"}},
expectedOut: `test/bugreports/negation_string.go`,
},
}
func TestParse(t *testing.T) {
for _, test := range tests {
test.in = contents(test.in)
test.expectedOut = contents(test.expectedOut)
bytes, err := parse.Generics(test.filename, test.pkgName, strings.NewReader(test.in), test.types)
// check the error
if test.expectedErr == nil {
assert.NoError(t, err, "(%s) No error was expected but got: %s", test.filename, err)
} else {
assert.NotNil(t, err, "(%s) No error was returned by one was expected: %s", test.filename, test.expectedErr)
assert.IsType(t, test.expectedErr, err, "(%s) Generate should return object of type %v", test.filename, test.expectedErr)
}
// assert the response
if !assert.Equal(t, string(bytes), test.expectedOut, "Parse didn't generate the expected output.") {
log.Println("EXPECTED: " + test.expectedOut)
log.Println("ACTUAL: " + string(bytes))
}
}
}
func contents(s string) string {
if strings.HasSuffix(s, "go") {
file, err := ioutil.ReadFile(s)
if err != nil {
panic(err)
}
return string(file)
}
return s
}
|