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

hokita / 課題2 #17

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

hokita / 課題2 #17

wants to merge 9 commits into from

Conversation

hokita
Copy link

@hokita hokita commented Jul 20, 2020

課題2

io.Readerとio.Writer

io.Readerとio.Writerについて調べてみよう

  • io.Readerとio.Writerについて調べてみよう
    • 標準パッケージでどのように使われているか
    • io.Readerとio.Writerがあることでどういう利点があるのか具体例を挙げて考えてみる

解答

標準パッケージでどのように使われているか

  • strings
    • 文字列操作
  • bufio
    • バッファリングしながら読み書きする
  • bytes
    • バイトスライスを操作する

io.Readerとio.Writerがあることでどういう利点があるのか具体例を挙げて考えてみる

IOが統一されているため、標準入出力、ファイル、ネットワーク通信どのような場合でも、入出力処理をする側、それを使う側がお互いの処理内容を知らなくてすみ、付け替えも可能。

例えばファイルを読み込んで処理Aをするアプリがあり、今回新しくHTTPレスポンスからA処理をする機能を追加したい場合、io.Readerを返すHTTPレスポンス読込処理を用意すれば、処理A自体を変更する必要はない。また処理Aのテストは出力を意識する必要もない。

テストを書いてみよう

1回目の課題のテストを作ってみて下さい

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

対応

テストのしやすさを考えてリファクタリングしてみる

  • io.Readerとio.Writerの課題を通して(c *Converter) Execute(in io.Reader, out io.Writer)を作成した。

テストのカバレッジを取ってみる

$ go test -coverprofile=profile github.com/gopherdojo/dojo8/kadai2/hokita/imgconv/
ok      github.com/gopherdojo/dojo8/kadai2/hokita/imgconv       0.438s  coverage: 79.5% o
f statements
$ go test -coverprofile=profile github.com/gopherdojo/dojo8/kadai2/hokita/imgconv/imgconv
ok      github.com/gopherdojo/dojo8/kadai2/hokita/imgconv/imgconv       0.476s  coverage:
 95.7% of statements

テーブル駆動テストを行う

  • map[string]structで作ってみた。

テストヘルパーを作ってみる

  • convertで作成されたファイル確認、削除処理をするcheckAndDeleteFileを作成

わからなかったこと、むずかしかったこと

  • モックをしたいがためにinterfaceを無理やり作ることはあるのだろうか。
  • だからと言って抽象化してモックしないとunitテストではなくE2Eテストになってしまいそう。

hokita and others added 7 commits July 7, 2020 00:49
- 引数を同じ型でまとめる
- Close()のエラー処理を追加
- factoryメソッド名をnewを付与
- 拡張子をmapのパッケージ変数として宣言
- Encodeのエラー処理を追加
- 終了コードを明示的に数値を代入する
- flag処理をmainパッケージへ移動
- Converter.Executeに渡されるファイルパスの拡張子が対象外の場合ErrUnmatchExtを返すようにする
  - 受け取る側でErrUnmatchExtかを判断し、ErrUnmatchExtなら無視する
@hokita
Copy link
Author

hokita commented Jul 20, 2020

こちらレビューお願いします。

@hokita hokita added the kadai2 課題2 label Jul 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kadai2 課題2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant