6
6
"go/types"
7
7
"io/ioutil"
8
8
"path"
9
+ "path/filepath"
9
10
"regexp"
10
11
"runtime"
11
12
"strings"
@@ -28,6 +29,7 @@ func TestGenerateTests(t *testing.T) {
28
29
templateDir string
29
30
template string
30
31
templateParamsPath string
32
+ templateData [][]byte
31
33
}
32
34
tests := []struct {
33
35
name string
@@ -725,6 +727,14 @@ func TestGenerateTests(t *testing.T) {
725
727
wantNoTests : true ,
726
728
wantErr : true ,
727
729
},
730
+ {
731
+ name : "With templateData" ,
732
+ args : args {
733
+ srcPath : `testdata/test004.go` ,
734
+ templateData : mustLoadExternalTemplateDir (t , "testdata/templatedata/" ),
735
+ },
736
+ want : mustReadAndFormatGoFile (t , "testdata/goldens/function_with_return_value_template_data.go" ),
737
+ },
728
738
}
729
739
tmp , err := ioutil .TempDir ("" , "gotests_test" )
730
740
if err != nil {
@@ -752,6 +762,7 @@ func TestGenerateTests(t *testing.T) {
752
762
TemplateDir : tt .args .templateDir ,
753
763
Template : tt .args .template ,
754
764
TemplateParams : params ,
765
+ TemplateData : tt .args .templateData ,
755
766
})
756
767
if (err != nil ) != tt .wantErr {
757
768
t .Errorf ("%q. GenerateTests(%v) error = %v, wantErr %v" , tt .name , tt .args .srcPath , err , tt .wantErr )
@@ -816,6 +827,31 @@ func loadExternalJsonFile(file string) (map[string]interface{}, error) {
816
827
return params , err
817
828
}
818
829
830
+ func mustLoadExternalTemplateDir (t * testing.T , dir string ) [][]byte {
831
+ files , err := ioutil .ReadDir (dir )
832
+ if err != nil {
833
+ t .Fatalf ("ioutil.ReadDir: %v" , err )
834
+ }
835
+
836
+ templateData := make ([][]byte , 0 )
837
+
838
+ for _ , f := range files {
839
+ filePath := filepath .Join (dir , f .Name ())
840
+ templateData = append (templateData , mustLoadExternalTemplateFile (t , filePath ))
841
+ }
842
+
843
+ return templateData
844
+ }
845
+
846
+ func mustLoadExternalTemplateFile (t * testing.T , file string ) []byte {
847
+ buf , err := ioutil .ReadFile (file )
848
+ if err != nil {
849
+ t .Fatalf ("loading external template file: %v" , err )
850
+ }
851
+
852
+ return buf
853
+ }
854
+
819
855
func toSnakeCase (s string ) string {
820
856
var res []rune
821
857
for _ , r := range []rune (s ) {
0 commit comments