diff --git a/kadai1/fujiokayu/converter/converter_test.go b/kadai1/fujiokayu/converter/converter_test.go deleted file mode 100644 index 72725be..0000000 --- a/kadai1/fujiokayu/converter/converter_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package converter - -import ( - "os" - "testing" -) - -const decodeFile string = "../testdata/cat.jpg" -const encodeFile string = "../testdata/cat.png" - -func Test_Convert(t *testing.T) { - err := Convert(decodeFile, "jpg", "png") - if err != nil { - t.Fatal("failed test: Convert error") - } - - // 変換後のファイルが存在するかチェック - info, err := os.Stat(encodeFile) - if err != nil { - t.Fatal("failed test: File not generated") - } - // 変換後のファイルサイズが0バイトではないかチェック - if info.Size() <= 0 { - t.Fatal("failed test: Encoded file is invalid") - } - // テストで生成したファイルを削除する。 - err = os.Remove(encodeFile) - if err != nil { - t.Fatal("failed to remove the file") - } -} diff --git a/kadai1/fujiokayu/testdata/cat.jpg b/kadai1/fujiokayu/testdata/cat.jpg deleted file mode 100644 index bccff43..0000000 Binary files a/kadai1/fujiokayu/testdata/cat.jpg and /dev/null differ diff --git a/kadai1/fujiokayu/Makefile b/kadai2/fujiokayu/Makefile similarity index 100% rename from kadai1/fujiokayu/Makefile rename to kadai2/fujiokayu/Makefile diff --git a/kadai1/fujiokayu/README.md b/kadai2/fujiokayu/README.md similarity index 100% rename from kadai1/fujiokayu/README.md rename to kadai2/fujiokayu/README.md diff --git a/kadai1/fujiokayu/args/args.go b/kadai2/fujiokayu/args/args.go similarity index 100% rename from kadai1/fujiokayu/args/args.go rename to kadai2/fujiokayu/args/args.go diff --git a/kadai1/fujiokayu/converter/converter.go b/kadai2/fujiokayu/converter/converter.go similarity index 84% rename from kadai1/fujiokayu/converter/converter.go rename to kadai2/fujiokayu/converter/converter.go index 47a4164..bee265a 100644 --- a/kadai1/fujiokayu/converter/converter.go +++ b/kadai2/fujiokayu/converter/converter.go @@ -38,6 +38,8 @@ func dec(filePath string, decodeType string) (image.Image, error) { return jpeg.Decode(reader) case "png": return png.Decode(reader) + default: + err = fmt.Errorf("undefined decodeType: %s", decodeType) } return nil, err } @@ -57,13 +59,15 @@ func enc(filePath string, encodeType string, m image.Image) error { return jpeg.Encode(writer, m, nil) case "png": return png.Encode(writer, m) + default: + err = fmt.Errorf("undefined encodeType: %s", encodeType) } - return nil + return err } //Convert : convert decodeType file to encodeType file func Convert(filePath string, decodeType string, encodeType string) error { - fmt.Println("Converting", filePath) + fmt.Println("Converting: ", filePath) m, err := dec(filePath, decodeType) if err != nil { @@ -75,6 +79,6 @@ func Convert(filePath string, decodeType string, encodeType string) error { return err } - fmt.Println("Converted", filePath) + fmt.Println("Converted : ", strings.TrimSuffix(filePath, path.Ext(filePath))+"."+encodeType) return nil } diff --git a/kadai2/fujiokayu/converter/converter_test.go b/kadai2/fujiokayu/converter/converter_test.go new file mode 100644 index 0000000..38255fe --- /dev/null +++ b/kadai2/fujiokayu/converter/converter_test.go @@ -0,0 +1,65 @@ +package converter_test + +import ( + "fmt" + "myConverter/converter" + "os" + "testing" +) + +func fileAssertionHelper(t *testing.T, testCase string, file string) { + t.Helper() + // 変換後のファイルが存在するかチェック + info, err := os.Stat(file) + if err != nil { + t.Fatal(fmt.Errorf("Case (%s) Failed test: File not generated", + testCase)) + } + // 変換後のファイルサイズが0バイトではないかチェック + if info.Size() <= 0 { + t.Fatal(fmt.Errorf("Case (%s) Failed test: Encoded file is invalid", + testCase)) + } +} + +func Test_Convert(t *testing.T) { + cases := []struct { + testCase string + isErr bool + decodeFile string + encodeFile string + decodeType string + encodeType string + }{ + {testCase: "J2P", isErr: false, decodeFile: "../testdata/cat.jpg", encodeFile: "../testdata/cat.png", decodeType: "jpg", encodeType: "png"}, + {testCase: "J2G", isErr: false, decodeFile: "../testdata/cat.jpg", encodeFile: "../testdata/cat.gif", decodeType: "jpg", encodeType: "gif"}, + {testCase: "P2J", isErr: false, decodeFile: "../testdata/cat.png", encodeFile: "../testdata/cat.jpg", decodeType: "png", encodeType: "jpg"}, + {testCase: "P2G", isErr: false, decodeFile: "../testdata/cat.png", encodeFile: "../testdata/cat.gif", decodeType: "png", encodeType: "gif"}, + {testCase: "G2J", isErr: false, decodeFile: "../testdata/cat.gif", encodeFile: "../testdata/cat.jpg", decodeType: "gif", encodeType: "jpg"}, + {testCase: "G2P", isErr: false, decodeFile: "../testdata/cat.gif", encodeFile: "../testdata/cat.png", decodeType: "gif", encodeType: "png"}, + {testCase: "decode file not exists", isErr: true, decodeFile: "../testdata/error", encodeFile: "../testdata/cat.png", decodeType: "gif", encodeType: "png"}, + {testCase: "undefined decode type", isErr: true, decodeFile: "../testdata/cat.jpg", encodeFile: "../testdata/cat.png", decodeType: "tiff", encodeType: "png"}, + {testCase: "undefined encode type", isErr: true, decodeFile: "../testdata/cat.jpg", encodeFile: "../testdata/cat.png", decodeType: "jpg", encodeType: "tiff"}, + } + + for _, c := range cases { + c := c + t.Run(c.testCase, func(t *testing.T) { + err := converter.Convert(c.decodeFile, c.decodeType, c.encodeType) + + if !c.isErr && err != nil { + t.Errorf( + "Case (%s) Failed test: Convert error.", + c.testCase) + } + + fileAssertionHelper(t, c.testCase, c.encodeFile) + + if c.isErr && err == nil { + t.Errorf( + "Case (%s) Failed test: Convert error.", + c.testCase) + } + }) + } +} diff --git a/kadai1/fujiokayu/go.mod b/kadai2/fujiokayu/go.mod similarity index 100% rename from kadai1/fujiokayu/go.mod rename to kadai2/fujiokayu/go.mod diff --git a/kadai1/fujiokayu/main.go b/kadai2/fujiokayu/main.go similarity index 100% rename from kadai1/fujiokayu/main.go rename to kadai2/fujiokayu/main.go diff --git a/kadai1/fujiokayu/testdata/a/b/test b/kadai2/fujiokayu/testdata/a/b/test similarity index 100% rename from kadai1/fujiokayu/testdata/a/b/test rename to kadai2/fujiokayu/testdata/a/b/test diff --git a/kadai1/fujiokayu/testdata/c/test b/kadai2/fujiokayu/testdata/c/test similarity index 100% rename from kadai1/fujiokayu/testdata/c/test rename to kadai2/fujiokayu/testdata/c/test diff --git a/kadai2/fujiokayu/testdata/cat.gif b/kadai2/fujiokayu/testdata/cat.gif new file mode 100644 index 0000000..76cff21 Binary files /dev/null and b/kadai2/fujiokayu/testdata/cat.gif differ diff --git a/kadai2/fujiokayu/testdata/cat.jpg b/kadai2/fujiokayu/testdata/cat.jpg new file mode 100644 index 0000000..79239e4 Binary files /dev/null and b/kadai2/fujiokayu/testdata/cat.jpg differ diff --git a/kadai2/fujiokayu/testdata/cat.png b/kadai2/fujiokayu/testdata/cat.png new file mode 100644 index 0000000..bc00977 Binary files /dev/null and b/kadai2/fujiokayu/testdata/cat.png differ diff --git a/kadai1/fujiokayu/testdata/d/e/test b/kadai2/fujiokayu/testdata/d/e/test similarity index 100% rename from kadai1/fujiokayu/testdata/d/e/test rename to kadai2/fujiokayu/testdata/d/e/test diff --git a/kadai1/fujiokayu/walker/walker.go b/kadai2/fujiokayu/walker/walker.go similarity index 100% rename from kadai1/fujiokayu/walker/walker.go rename to kadai2/fujiokayu/walker/walker.go diff --git a/kadai1/fujiokayu/walker/walker_test.go b/kadai2/fujiokayu/walker/walker_test.go similarity index 100% rename from kadai1/fujiokayu/walker/walker_test.go rename to kadai2/fujiokayu/walker/walker_test.go