Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Kadai2-fujiokayu #21

Open
wants to merge 12 commits into
base: kadai1-fujiokayu
Choose a base branch
from
31 changes: 0 additions & 31 deletions kadai1/fujiokayu/converter/converter_test.go

This file was deleted.

Binary file removed kadai1/fujiokayu/testdata/cat.jpg
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
65 changes: 65 additions & 0 deletions kadai2/fujiokayu/converter/converter_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added kadai2/fujiokayu/testdata/cat.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added kadai2/fujiokayu/testdata/cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added kadai2/fujiokayu/testdata/cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.