1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

作って学ぶElixir(2) テスト

Last updated at Posted at 2019-12-19

前回の記事

作って学ぶElixir(1) 引数の順序とパイプ演算子

テストを書こう

さて、前回関数の仕様が確定したので実際に実装してしまいたいところですが、先にテストケースをいくつか書いてしまいましょう。何度もiexで実行するのは手間ですからね。
なによりElixirはイミュータブルですから単体テストも効果的です。

まっさらな状態だとこんなテストモジュールになるかと思います。

test/summary_test.exs
defmodule SummaryTest do
  use ExUnit.Case
  doctest Summary

  end
end

doctestに関してはこちらの記事が参考になりますので、参考にしてみてください。
Elixir の doctest 書き方まとめ
この記事ではdoctestに関しては割愛します。

こんな感じでひたすらテストケースを作成してみます。
実装する際にサンプルで使用しそうな値を予めassertで書いてしまうと良いでしょう。
特に思い浮かばない場合は0の場合、1の場合、多数の場合と境界値を意識するとテストケースが思い浮かぶのではないでしょうか。

test/summary_test.exs
defmodule SummaryTest do
  use ExUnit.Case
  #doctest Summary

  test "1行の場合" do
    data = [%{"name" => "sato", "script" => "hello"}]
    assert data
           |> Summary.by_key("name") ==
             [%{"name" => "sato", "contents" => [%{"script" => "hello"}]}]
  end
  test "同じキーが2行の場合" do
    data = [%{"name" => "sato", "script" => "hello"}, %{"name" => "sato", "script" => "hello"}]
    assert data
           |> Summary.by_key("name") ==
             [%{"name" => "sato", "contents" => [%{"script" => "hello"}, %{"script" => "hello"}]}]
  end

  test "違うキーが2行の場合" do
    data = [%{"name" => "sato", "script" => "hello"}, %{"name" => "tanaka", "script" => "hello"}]
    assert data
           |> Summary.by_key("name") ==
             [
               %{"name" => "sato", "contents" => [%{"script" => "hello"}]},
               %{"name" => "tanaka", "contents" => [%{"script" => "hello"}]}
             ]
  end

  test "違うキーで3行の場合" do
    data = [%{"name" => "sato", "script" => "hello"}, %{"name" => "tanaka", "script" => "hello"}]
    assert data
           |> Summary.by_key("name") ==
             [
               %{"name" => "sato", "contents" => [%{"script" => "hello"}]},
               %{"name" => "tanaka", "contents" => [%{"script" => "hello"}]}
             ]
  end

  test "違うキーで4行の場合" do
    data = [
      %{"name" => "sato", "script" => "hello"},
      %{"name" => "tanaka", "script" => "hello"},
      %{"name" => "sato", "script" => "hello"},
      %{"name" => "tanaka", "script" => "hello"}
    ]
    assert data
           |> Summary.by_key("name") ==
             [
               %{"name" => "sato", "contents" => [%{"script" => "hello"}]},
               %{"name" => "tanaka", "contents" => [%{"script" => "hello"}]},
               %{"name" => "sato", "contents" => [%{"script" => "hello"}]},
               %{"name" => "tanaka", "contents" => [%{"script" => "hello"}]}
             ]
  end
  test "一部同じキーで4行の場合" do
    data = [
      %{"name" => "sato", "script" => "hello"},
      %{"name" => "tanaka", "script" => "hello"},
      %{"name" => "tanaka", "script" => "hello"},
      %{"name" => "sato", "script" => "hello"}
    ]
    assert data
           |> Summary.by_key("name") ==
             [
               %{"name" => "sato", "contents" => [%{"script" => "hello"}]},
               %{"name" => "tanaka", "contents" => [%{"script" => "hello"}, %{"script" => "hello"}]},
               %{"name" => "sato", "contents" => [%{"script" => "hello"}]}
             ],
           [
             %{
               "script" => "hello",
               "contents" => [
                 %{"name" => "sato"},
                 %{"name" => "tanaka"},
                 %{"name" => "tanaka"},
                 %{"name" => "kobayashi"}
               ]
             }
           ]
  end
end
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?