@@ -5,14 +5,27 @@ import (
5
5
"strings"
6
6
"testing"
7
7
8
- "github.com/golang/mock/gomock"
9
- "github.com/gopherdojo/dojo6/kadai2/en-ken/mock_imgcnv"
8
+ "github.com/gopherdojo/dojo6/kadai2/en-ken/imgcnv"
10
9
)
11
10
12
- func TestExecuteSuccess (t * testing.T ) {
13
- ctrl := gomock .NewController (t )
14
- defer ctrl .Finish ()
11
+ type ImageFileMock struct {
12
+ t * testing.T
13
+ inputPath string
14
+ outputPath string
15
+ }
16
+
17
+ func (mock * ImageFileMock ) AbsPath () string {
18
+ return mock .inputPath
19
+ }
20
+
21
+ func (mock * ImageFileMock ) SaveAs (path string ) error {
22
+ if path != mock .outputPath {
23
+ mock .t .Errorf ("SaveAs was not called correctly:\n actual: %v\n expected: %v" , path , mock .outputPath )
24
+ }
25
+ return nil
26
+ }
15
27
28
+ func TestExecuteSuccess (t * testing.T ) {
16
29
tests := []struct {
17
30
argString string
18
31
inputDir string
@@ -21,75 +34,66 @@ func TestExecuteSuccess(t *testing.T) {
21
34
outputExt string
22
35
}{
23
36
{
24
- argString : "./kadai1 ./testdata" ,
25
- inputDir : "./testdata" , inputExt : ". jpg" ,
26
- outputDir : "./testdata " , outputExt : ". png" ,
37
+ argString : "./kadai1 ./testdata ./out " ,
38
+ inputDir : "./testdata" , inputExt : "jpg" ,
39
+ outputDir : "./out " , outputExt : "png" ,
27
40
},
28
41
{
29
- argString : "./kadai1 ./testdata -input-ext .png " ,
30
- inputDir : "./testdata" , inputExt : ". png" ,
31
- outputDir : "./testdata " , outputExt : ". png" ,
42
+ argString : "./kadai1 -in png ./testdata ./out " ,
43
+ inputDir : "./testdata" , inputExt : "png" ,
44
+ outputDir : "./out " , outputExt : "png" ,
32
45
},
33
46
{
34
- argString : "./kadai1 ./testdata -input-ext .png -output-ext .jpg " ,
35
- inputDir : "./testdata" , inputExt : ".png " ,
36
- outputDir : "./testdata " , outputExt : ". jpg" ,
47
+ argString : "./kadai1 -out jpg ./testdata ./out " ,
48
+ inputDir : "./testdata" , inputExt : "jpg " ,
49
+ outputDir : "./out " , outputExt : "jpg" ,
37
50
},
38
51
{
39
- argString : "./kadai1 ./testdata -output-dir ./out" ,
40
- inputDir : "./testdata" , inputExt : ".jpg " ,
41
- outputDir : "./out" , outputExt : ".png " ,
52
+ argString : "./kadai1 -in png -out jpg ./testdata ./out" ,
53
+ inputDir : "./testdata" , inputExt : "png " ,
54
+ outputDir : "./out" , outputExt : "jpg " ,
42
55
},
43
56
}
44
57
45
58
for _ , test := range tests {
46
59
testName := "input: " + test .argString
47
60
t .Run (testName , func (t * testing.T ) {
48
61
49
- mockDirPath := mock_imgcnv .NewMockDirPath (ctrl )
50
-
51
62
inputAbsDir , _ := filepath .Abs (test .inputDir )
52
63
inputPath1 := inputAbsDir + "/test1" + test .inputExt
53
64
inputPath2 := inputAbsDir + "/test2" + test .inputExt
65
+ outputPath1 , _ := filepath .Abs (test .outputDir + "/test1" + test .outputExt )
66
+ outputPath2 , _ := filepath .Abs (test .outputDir + "/test2" + test .outputExt )
54
67
55
- mockDirPath .
56
- EXPECT ().
57
- AllFilePaths (gomock .Eq (inputAbsDir ), gomock .Eq (test .inputExt )).
58
- Return ([]string {inputPath1 , inputPath2 }, nil )
59
-
60
- mockImg := mock_imgcnv .NewMockImageFile (ctrl )
61
- gomock .InOrder (
62
- mockImg .
63
- EXPECT ().
64
- AbsPath ().
65
- Return (inputPath1 ),
66
- mockImg .
67
- EXPECT ().
68
- AbsPath ().
69
- Return (inputPath2 ),
70
- )
71
-
72
- outputPath1 := test .outputDir + "/test1" + test .outputExt
73
- outputPath2 := test .outputDir + "/test2" + test .outputExt
74
- gomock .InOrder (
75
- mockImg .
76
- EXPECT ().
77
- SaveAs (gomock .Eq (outputPath1 )),
78
- mockImg .
79
- EXPECT ().
80
- SaveAs (gomock .Eq (outputPath2 )),
81
- )
68
+ allFilePathsMock := func (path string , ext string ) ([]string , error ) {
69
+ if path == inputAbsDir && ext == test .inputExt {
70
+ return []string {inputPath1 , inputPath2 }, nil
71
+ }
72
+ t .Errorf ("AllFilePaths was not called correctly: %v %v" , path , ext )
73
+ return nil , nil
74
+ }
82
75
83
- mockImgFactory := mock_imgcnv .NewMockImageFileFactory (ctrl )
84
- mockImgFactory .
85
- EXPECT ().
86
- Create (gomock .Any ()).
87
- Return (mockImg , nil ).
88
- AnyTimes ()
76
+ newImageFileMock := func (path string ) (imgcnv.ImageFile , error ) {
77
+ switch path {
78
+ case inputPath1 :
79
+ return & ImageFileMock {
80
+ t : t ,
81
+ inputPath : path ,
82
+ outputPath : outputPath1 ,
83
+ }, nil
84
+ case inputPath2 :
85
+ return & ImageFileMock {
86
+ t : t ,
87
+ inputPath : path ,
88
+ outputPath : outputPath2 ,
89
+ }, nil
90
+ }
91
+ return nil , nil
92
+ }
89
93
90
94
cli := & CLI {
91
- dirPath : mockDirPath ,
92
- imageFactory : mockImgFactory ,
95
+ AllFilePaths : allFilePathsMock ,
96
+ NewImageFIle : newImageFileMock ,
93
97
}
94
98
cli .Execute (strings .Split (test .argString , " " ))
95
99
0 commit comments