Skip to content

Commit 0c9f8cb

Browse files
committed
テストヘルパーを使ったテストの実装
1 parent 56dccc5 commit 0c9f8cb

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed
Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,72 @@
11
package imageconverter_test
22

33
import (
4-
"fmt"
54
"os"
65
"testing"
76

87
"github.com/gopherdojo/dojo2/kadai2/kadai2-2/imageconverter"
98
)
109

1110
func TestConverter_Run(t *testing.T) {
11+
t.Run("jpg to png", func(t *testing.T) {
12+
testConverter_Run(t,
13+
imageconverter.FilePath("../sample_dir1/Octocat.jpeg"),
14+
imageconverter.Format("jpg"),
15+
imageconverter.Format("png"),
16+
imageconverter.FilePath("../sample_dir1/Octocat.png"),
17+
true,
18+
)
19+
testConverter_Run(t,
20+
imageconverter.FilePath("../sample_dir1/dummy_fuga.md"),
21+
imageconverter.Format("jpg"),
22+
imageconverter.Format("png"),
23+
imageconverter.FilePath("../sample_dir1/dummy_fuga.png"),
24+
false,
25+
)
26+
testConverter_Run(t,
27+
imageconverter.FilePath("../sample_dir1/sample_dir2/Octocat.jpg"),
28+
imageconverter.Format("jpg"),
29+
imageconverter.Format("png"),
30+
imageconverter.FilePath("../sample_dir1/sample_dir2/Octocat.png"),
31+
true,
32+
)
33+
})
34+
t.Run("png to jpg", func(t *testing.T) {
35+
testConverter_Run(t,
36+
imageconverter.FilePath("../sample_dir1/sample_dir2/sample_dir3/Octocat.png"),
37+
imageconverter.Format("png"),
38+
imageconverter.Format("jpg"),
39+
imageconverter.FilePath("../sample_dir1/sample_dir2/sample_dir3/Octocat.jpg"),
40+
true,
41+
)
42+
testConverter_Run(t,
43+
imageconverter.FilePath("../sample_dir1/dummy_fuga.md"),
44+
imageconverter.Format("png"),
45+
imageconverter.Format("jpg"),
46+
imageconverter.FilePath("../sample_dir1/dummy_fuga.jpg"),
47+
false,
48+
)
49+
})
50+
}
51+
52+
func testConverter_Run(t *testing.T,
53+
inFP imageconverter.FilePath,
54+
inF imageconverter.Format,
55+
outF imageconverter.Format,
56+
expectedOutFP imageconverter.FilePath,
57+
expectedGenerated bool,
58+
) {
59+
t.Helper()
1260
var c imageconverter.Converter
13-
fi := imageconverter.FileInfo{Path: imageconverter.FilePath("../sample_dir1/Octocat.jpeg")}
14-
c.Run(fi, imageconverter.Format("jpg"), imageconverter.Format("png"))
15-
generatedFilePath := imageconverter.FilePath("../sample_dir1/Octocat.png")
16-
result := fileExists(generatedFilePath)
17-
expected := true
18-
if result != expected {
61+
fi := imageconverter.FileInfo{Path: inFP}
62+
c.Run(fi, inF, outF)
63+
result := fileExists(expectedOutFP)
64+
if result != expectedGenerated {
1965
t.Errorf("Converter.Run failed.")
2066
}
21-
fileClear(generatedFilePath)
67+
if result {
68+
fileClear(expectedOutFP)
69+
}
2270
}
2371

2472
func fileExists(path imageconverter.FilePath) bool {
@@ -27,7 +75,5 @@ func fileExists(path imageconverter.FilePath) bool {
2775
}
2876

2977
func fileClear(path imageconverter.FilePath) {
30-
if err := os.Remove(string(path)); err != nil {
31-
fmt.Println(err)
32-
}
78+
os.Remove(string(path))
3379
}

0 commit comments

Comments
 (0)