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

Kadai2 edm20627 #51

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open

Conversation

keethii27
Copy link

課題2の回答を作成しました。お手隙の際、レビューしていただけると有り難いです。

課題2 テストを書いてみよう

課題要件

  • テストのしやすさを考えてリファクタリングしてみる
  • テストのカバレッジを取ってみる
  • テーブル駆動テストを行う
  • テストヘルパーを作ってみる

使用方法

$ cmd --from jpg --to png <ディレクトリ or ファイル>

  • --from(-f) 変換対象の画像形式(デフォルト: jpg)
  • --to(-t) 変換後の画像形式(デフォルト: png)
  • --delete(-d) 変換前の画像を削除(デフォルト: 無効)

テスト

root@bd49f127869b:/go/src/github.com/edm20627/gopherdojo-studyroom/kadai2/edm20627# go test -v -cover ./imageconvert
=== RUN   TestGet
=== RUN   TestGet/get_jpg
=== RUN   TestGet/get_png
=== RUN   TestGet/get_gif
=== RUN   TestGet/occurred_ErrNotSpecified
=== RUN   TestGet/occurred_ErrMotDirectory
--- PASS: TestGet (0.10s)
    --- PASS: TestGet/get_jpg (0.01s)
    --- PASS: TestGet/get_png (0.00s)
    --- PASS: TestGet/get_gif (0.00s)
    --- PASS: TestGet/occurred_ErrNotSpecified (0.00s)
    --- PASS: TestGet/occurred_ErrMotDirectory (0.00s)
=== RUN   TestConvert
=== RUN   TestConvert/jpg_to_png
=== RUN   TestConvert/png_to_gif
=== RUN   TestConvert/gif_to_jpg
--- PASS: TestConvert (0.11s)
    --- PASS: TestConvert/jpg_to_png (0.03s)
    --- PASS: TestConvert/png_to_gif (0.04s)
    --- PASS: TestConvert/gif_to_jpg (0.04s)
=== RUN   TestValid
=== RUN   TestValid/jpg_to_png
=== RUN   TestValid/png_to_jpg
=== RUN   TestValid/jpeg_to_gif
=== RUN   TestValid/gif_to_jpeg
=== RUN   TestValid/hoge_to_fuga
--- PASS: TestValid (0.00s)
    --- PASS: TestValid/jpg_to_png (0.00s)
    --- PASS: TestValid/png_to_jpg (0.00s)
    --- PASS: TestValid/jpeg_to_gif (0.00s)
    --- PASS: TestValid/gif_to_jpeg (0.00s)
    --- PASS: TestValid/hoge_to_fuga (0.00s)
PASS
coverage: 80.4% of statements
ok  	github.com/edm20627/gopherdojo-studyroom/kadai2/edm20627/imageconvert	(cached)	coverage: 80.4% of statements


for _, c := range success_cases {
for _, path := range c.expected {
testCreateImage(t, path, c.from)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーハンドリングをしておいた方が良さそうです。

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
for _, path := range c.filepaths {
testCreateImage(t, path, c.from)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここもエラーハンドリングをしておいた方が良さそうです。

t.Error(err)
}
defer file.Close()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テスト用に毎回画像を生成していますが、画像に関していえば実際の表示が正しいかも判断材料となるため、実際のファイルをtestdataディレクトリ以下に置いておいてテストする方がより良いのではと思います。参考にGo自体も以下のように画像のテストデータを置いています。
https://github.com/golang/go/tree/master/src/image/testdata

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど。
勉強になりました!ありがとうございます!


var (
ErrNotSpecified = errors.New("Need to specify directory or file")
ErrMotDirectory = errors.New("Specify directory or file is not directory")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

おそらくs/Mot/Not/ですね

}
}
}
return false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ネストが深いので以下の方が綺麗かなと思いました

	var fromSupported, toSupported bool
	for _, v := range SupportedFormat {
		if v == ci.From {
			fromSupported = true
		}
		if v == ci.To {
			toSupported = true
		}
	}
	return fromSupported && toSupported

{name: "png to jpg", from: "png", to: "jpg", expected: true},
{name: "jpeg to gif", from: "jpeg", to: "gif", expected: true},
{name: "gif to jpeg", from: "gif", to: "jpeg", expected: true},
{name: "hoge to fuga", from: "hoge", to: "fuga", expected: false},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

両方がサポート拡張子の場合に正としたい関数ですが
正 to 正
否 to 否
のケースしかテストしていないので
正 to 否
否 to 正
のケースもテストした方が良さそうです

{name: "jpg to png", filepaths: []string{"../testdata/img_1.jpg"}, from: "jpg", to: "png"},
{name: "png to gif", filepaths: []string{"../testdata/img_1.png"}, from: "png", to: "gif"},
{name: "gif to jpg", filepaths: []string{"../testdata/img_1.gif"}, from: "gif", to: "jpg"},
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ちょっと大変ですがDeleteOptionのテストもできるとベストですね

Comment on lines 77 to 95
imageconvert.OsRemove = func(path string) error {
for _, filepath := range c.filepaths {
if path == filepath {
return nil
}
}
return errors.New("failed to delete for conversion source image")
}

ci := imageconvert.ConvertImage{Filepaths: c.filepaths, To: c.to, DeleteOption: c.deleteOption}
if actual := ci.Convert(); actual != nil {
t.Error(actual)
}
for _, filepath := range c.filepaths {
targetFile := strings.Replace(filepath, c.from, c.to, 1)
if err := os.Remove(targetFile); err != nil {
t.Fatal(err)
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeleteOptionのテストを追加しました。
再レビューいただけると有り難いです。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモ:
削除しているか確認できない。

  • テストディレクトリを丸ごとテスト時にコピー
  • 実行後に、ファイルあるなしを確認
  • テスト終了時にコピーしたテストディレクトリを削除

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants