Skip to content

Commit a73929e

Browse files
author
en-ken
committed
refactor: deleted gomock and created originals.
1 parent 230e46b commit a73929e

File tree

3 files changed

+78
-75
lines changed

3 files changed

+78
-75
lines changed

kadai2/en-ken/cli/cli.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"flag"
5+
"fmt"
56
"path/filepath"
67
"strings"
78

@@ -15,39 +16,39 @@ type CLI struct {
1516
}
1617

1718
// Execute executes this app according to options
18-
// arg[0] application name
19-
// arg[1] input directory
20-
// arg[2:] options
2119
func (cli *CLI) Execute(args []string) error {
2220

21+
var inputExt, outputExt string
2322
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
24-
var (
25-
inputExt = flags.String("input-ext", ".jpg", "input extension (.jpg/.png)")
26-
outputExt = flags.String("output-ext", ".png", "output extension (.jpg/.png)")
27-
output = flags.String("output-dir", args[1], "output directory")
28-
inputDir string
29-
outputDir string
30-
)
23+
flags.StringVar(&inputExt, "in", "jpg", "input extension (jpg/png)")
24+
flags.StringVar(&outputExt, "out", "png", "output extension (jpg/png)")
3125

32-
inputDir, err := filepath.Abs(args[1])
33-
if err != nil {
26+
// Parse command args
27+
if err := flags.Parse(args[1:]); err != nil {
3428
return err
3529
}
3630

37-
if err := flags.Parse(args[2:]); err != nil {
31+
if len(flags.Args()) != 2 {
32+
return fmt.Errorf("Either input directory or output directory not specified")
33+
}
34+
// Get input dir
35+
inputDir, err := filepath.Abs(flags.Arg(0))
36+
if err != nil {
3837
return err
3938
}
40-
41-
outputDir = *output
42-
if outputDir == "" {
43-
outputDir = inputDir
39+
// Get output dir
40+
outputDir, err := filepath.Abs(flags.Arg(1))
41+
if err != nil {
42+
return err
4443
}
4544

46-
paths, err := cli.AllFilePaths(inputDir, *inputExt)
45+
// Get all file paths
46+
paths, err := cli.AllFilePaths(inputDir, inputExt)
4747
if err != nil {
4848
return err
4949
}
5050

51+
// Convert and save
5152
for _, path := range paths {
5253
img, err := cli.NewImageFIle(path)
5354
if err != nil {
@@ -56,7 +57,7 @@ func (cli *CLI) Execute(args []string) error {
5657

5758
// Copy the hierarchy of the input dir to that of the output dir.
5859
outputPath := strings.Replace(img.AbsPath(), inputDir, outputDir, -1)
59-
outputPath = strings.Replace(outputPath, *inputExt, *outputExt, 1)
60+
outputPath = strings.Replace(outputPath, inputExt, outputExt, 1)
6061

6162
err = img.SaveAs(outputPath)
6263
if err != nil {

kadai2/en-ken/cli/cli_test.go

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,27 @@ import (
55
"strings"
66
"testing"
77

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"
109
)
1110

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:\nactual: %v\nexpected: %v", path, mock.outputPath)
24+
}
25+
return nil
26+
}
1527

28+
func TestExecuteSuccess(t *testing.T) {
1629
tests := []struct {
1730
argString string
1831
inputDir string
@@ -21,75 +34,66 @@ func TestExecuteSuccess(t *testing.T) {
2134
outputExt string
2235
}{
2336
{
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",
2740
},
2841
{
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",
3245
},
3346
{
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",
3750
},
3851
{
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",
4255
},
4356
}
4457

4558
for _, test := range tests {
4659
testName := "input: " + test.argString
4760
t.Run(testName, func(t *testing.T) {
4861

49-
mockDirPath := mock_imgcnv.NewMockDirPath(ctrl)
50-
5162
inputAbsDir, _ := filepath.Abs(test.inputDir)
5263
inputPath1 := inputAbsDir + "/test1" + test.inputExt
5364
inputPath2 := inputAbsDir + "/test2" + test.inputExt
65+
outputPath1, _ := filepath.Abs(test.outputDir + "/test1" + test.outputExt)
66+
outputPath2, _ := filepath.Abs(test.outputDir + "/test2" + test.outputExt)
5467

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+
}
8275

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+
}
8993

9094
cli := &CLI{
91-
dirPath: mockDirPath,
92-
imageFactory: mockImgFactory,
95+
AllFilePaths: allFilePathsMock,
96+
NewImageFIle: newImageFileMock,
9397
}
9498
cli.Execute(strings.Split(test.argString, " "))
9599

kadai2/en-ken/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/gopherdojo/dojo6/kadai2/en-ken
22

33
go 1.12
4-
5-
require github.com/golang/mock v1.3.1

0 commit comments

Comments
 (0)