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
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7dc7e7a
ひとまず動くものを作成
keethii27 Feb 25, 2021
5aed38c
ユーザ定義型を作成
keethii27 Feb 25, 2021
9dc9755
自作パッケージを作成
keethii27 Feb 25, 2021
7d5d81e
vlid関数をメソッドに変更
keethii27 Feb 25, 2021
7776099
微修正
keethii27 Feb 25, 2021
a504b57
画像変換処理のエラーハンドリング追加
keethii27 Feb 25, 2021
44bbfd1
削除フラグの追加
keethii27 Feb 25, 2021
87c6eb3
GoDocを生成する
keethii27 Feb 25, 2021
44e95d3
サンプル画像を追加
keethii27 Feb 25, 2021
f21ec02
README.md 追加
keethii27 Feb 26, 2021
49c5e89
.gitignore
keethii27 Feb 26, 2021
74fabfa
go mod tidy 実行'
keethii27 Feb 26, 2021
f4be072
.gitignoreの配置変更
keethii27 Apr 7, 2021
183f52f
課題1をコピー
keethii27 Apr 7, 2021
5fefe04
go moduleパス変更
keethii27 Apr 8, 2021
e1ba6c2
リファクタリング
keethii27 Apr 8, 2021
7caa18b
テスト追加
keethii27 Apr 17, 2021
c75f90f
fix エラーハンドリング追加
keethii27 May 2, 2021
6d344e9
fix タイポ
keethii27 May 2, 2021
3f72fd8
fix ネストが深いためValidメソッドをリファクタリング
keethii27 May 2, 2021
38cf933
fix TestValidに正 to 否、否 to 正ケースを追加
keethii27 May 2, 2021
539b81a
fix テスト用画像を使用しテストできるようにtastdataを作成
keethii27 May 2, 2021
1133222
fix DeleteOptionのテスト追加
keethii27 May 2, 2021
6d99bb2
fix 実際にテストでファイル削除しているか確認できるように修正
keethii27 May 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion kadai2/edm20627/imageconvert/imageconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var (
ErrNotDirectory = errors.New("Specify directory or file is not directory")
)

var OsRemove = os.Remove

type ConvertImage struct {
Filepaths []string
From, To string
Expand Down Expand Up @@ -108,7 +110,7 @@ func convert(path string, to string, deleteOption bool) error {
}

if deleteOption {
if err := os.Remove(path); err != nil {
if err := OsRemove(path); err != nil {
return err
}
}
Expand Down
38 changes: 30 additions & 8 deletions kadai2/edm20627/imageconvert/imageconvert_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package imageconvert_test

import (
"errors"
"os"
"reflect"
"strings"
"testing"

"github.com/edm20627/gopherdojo-studyroom/kadai2/edm20627/imageconvert"
Expand Down Expand Up @@ -55,22 +58,41 @@ func TestGet(t *testing.T) {

func TestConvert(t *testing.T) {
cases := []struct {
name string
filepaths []string
from string
to string
name string
filepaths []string
from string
to string
deleteOption bool
}{
{name: "jpg to png", filepaths: []string{"../testdata/img_1.jpg"}, from: "jpg", to: "png"},
{name: "png to gif", filepaths: []string{"../testdata/img_2.png"}, from: "png", to: "gif"},
{name: "gif to jpg", filepaths: []string{"../testdata/img_3.gif"}, from: "gif", to: "jpg"},
{name: "jpg to png", filepaths: []string{"../testdata/img_1.jpg"}, from: "jpg", to: "png", deleteOption: false},
{name: "png to gif", filepaths: []string{"../testdata/img_2.png"}, from: "png", to: "gif", deleteOption: false},
{name: "gif to jpg", filepaths: []string{"../testdata/img_3.gif"}, from: "gif", to: "jpg", deleteOption: false},
{name: "jpg to png", filepaths: []string{"../testdata/img_1.jpg"}, from: "jpg", to: "png", deleteOption: true},
{name: "png to gif", filepaths: []string{"../testdata/img_2.png"}, from: "png", to: "gif", deleteOption: true},
{name: "gif to jpg", filepaths: []string{"../testdata/img_3.gif"}, from: "gif", to: "jpg", deleteOption: true},
}

Choose a reason for hiding this comment

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

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


for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ci := imageconvert.ConvertImage{Filepaths: c.filepaths, To: c.to}
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.

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

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

})
}
}
Expand Down