Skip to content

kadai1-yashiken#3

Open
ghost wants to merge 11 commits into
masterfrom
kadai1-yashiken
Open

kadai1-yashiken#3
ghost wants to merge 11 commits into
masterfrom
kadai1-yashiken

Conversation

@ghost

@ghost ghost commented Jul 14, 2019

Copy link
Copy Markdown

仕様

  • 引数に指定したディレクトリ配下の特定の形式の画像を指定した形式へ変換する
    • デフォルトでは、jpg形式をpng形式に変換する
    • jpg, jpeg, png,gif間の変換に対応。
  • オプションで、変換前後のファイル形式を指定する
    • -s オプションで、変換するファイル形式を指定
    • -d オプションで、変換後のファイル形式を指定する
  • 実行後、変換前のファイルは削除されず、変換前の画像が格納されているディレクトリに新たにファイルが生成される

使い方

go run main.go -s jpg -d png images

気になっている点

  • convertパッケージの実装
    • ユーザー定義型Converterを定義したが、この実装に若干不安があります。
  • convertパッケージのテストコード
    • テストに共通部分があり、冗長であるように思われる
    • TestDecode, TestEncodeのerrorがnilか否かの判定はチェックできたが、戻り値が正常であるかを判定するロジックを組めていない。
    • encode関数でwriterを定義する部分で、エラー発生するケースがわからず、テストができていない

@ghost ghost added the kadai1 label Jul 14, 2019
@ghost
ghost requested a review from tenntenn July 14, 2019 05:52
@annkara

annkara commented Jul 14, 2019

Copy link
Copy Markdown
Member

コードに関する話でなくて申し訳ないのですが、こちらの資料にある通り、不要なファイルは削除した方が良いかと思うですが、如何でしょう??

@ghost

ghost commented Jul 14, 2019

Copy link
Copy Markdown
Author

コードに関する話でなくて申し訳ないのですが、こちらの資料にある通り、不要なファイルは削除した方が良いかと思うですが、如何でしょう??

コメントありがとうございます、仰る通りです。チェック不足で申し訳ないです...
ご指摘頂いた点については、修正しました。

教えていただいた資料もとても参考になります、ありがとうございます。
コード以外の部分でも見る方への配慮大事ですね。。

@ghost
ghost removed the request for review from tenntenn July 14, 2019 15:03
@ghost ghost self-assigned this Jul 14, 2019
@ghost ghost changed the title Initial commit kadai1-yashiken Jul 15, 2019
Comment thread README.md Outdated
@@ -1,3 +1,27 @@
# yashiken追記

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ディレクトリを切ってほしいですー

Comment thread README.md Outdated

## 使い方
```
go run main.go -s jpg -d png images

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

go run で使用することを想定しない。
ちゃんとビルドすることを想定する。

Comment thread kadai1/yashiken/convert/convert.go Outdated
return err
}
// 成功メッセージの表示
fmt.Printf("%s converted to %s image\n", path, c.extCnv)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

main パッケージ以外で標準出力に出力するのはできるだけ避ける

Comment thread kadai1/yashiken/convert/convert.go Outdated
}

// Converは、受け取ったファイルパスの画像ファイルを指定した形式に変換します
func (c Converter) Convert(path string) error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

特に明確な理由がない場合はレシーバはポインタにする。

// decodeは指定したファイルパスのファイルの読み取り、デコードを行います
func (c Converter) decode(path string) (image.Image, error) {
// ファイルの読み取り
file, err := os.Open(path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

file.Close を呼び出していない

Comment thread kadai1/yashiken/convert/convert.go Outdated
}

// NewConverterはConverter構造体を生成するためのヘルパー関数です。
func NewConverter(extSrc, extCnv string) *Converter {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

構造体リテラルによる初期化以上の役割がなければ別にフィールドを隠蔽して初期化用関数を作る必要はない。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

関数名は利用側にとって分かりやすい名前にする。convert.NewConverter より converter.New 重複がなくてよい

)

// TestMainは、テストで変換した画像ファイルを消去するための関数です。
func TestMain(m *testing.M) {

@tenntenn tenntenn Jul 17, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

テストヘルパーにしてテスト関数ごとに実行する。

Comment thread kadai1/yashiken/convert/convert_test.go Outdated
}
// 呼び出し結果と期待値の比較
if *result != *expect {
t.Error("NewConverterの戻り値が正しくありません")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

テストはエラーメッセージが非常に大事なのでどう正しくないのかを出す。

Comment thread kadai1/yashiken/convert/convert_test.go Outdated

// NewImgのテストです。正しいConverter構造体を返却することを確認します。
func TestNewImg(t *testing.T) {
extSrc := "jpg"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

定数でよい。

Comment thread kadai1/yashiken/convert/convert_test.go Outdated
}

// NewImgのテストです。正しいConverter構造体を返却することを確認します。
func TestNewImg(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

このテストっていりますか?
TestNewImgって名前は正しいですか?TestNewConverter?

@ghost

ghost commented Jul 21, 2019

Copy link
Copy Markdown
Author

コメントいただきありがとうございました。Gopher道場の場でも解説していただき、とても勉強になります。
テストの部分を除き、修正を行いました。

テストのブラッシュアップは、課題2−2と併せて実施予定です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants