週刊Elixirライブラリ2015-16

Elixirのライブラリの解説を週ごとにお届けする記事です。
解説が間違っていた場合には、コメントか@hayabusa333にご連絡くださると嬉しいです。

今回はPowerAssertExについて

# PowerAssertEx is 何?
PowerAssertExは、ma2geさんによって開発されたElixir用のPowerAssertです。
PowerAssert自体はt_wasaさんより作成された単純なアサーションのみで、Assertにて出力された情報をわかりやすく表示するUnitTest用のフレームワークです。

# 実行環境
OS:OS X Yosemite
Erlang:Eshell V6.5, OTP-Version 18
Elixir:v1.0.4

# PowerAssertExの実行を行うための新規プロジェクトの作成

$ mix new my_power_assert_ex
$ cd my_power_assert_ex

# HexにてPowerAssertExをインストールするために設定ファイルの記載を行う

$ vim mix.exs

mix.exsの内容は下記となります。

defmodule MyPowerAssertEx.Mixfile do
  use Mix.Project

  def project do
    [app: :my_power_assert_ex,
     version: "0.0.1",
     elixir: "~> 1.0",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type `mix help compile.app` for more information
  def application do
    [applications: [:logger]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type `mix help deps` for more examples and options
  defp deps do
    [{:power_assert, "~> 0.0.1"}]
  end
end

# 依存関係の解決を行います。

$ mix deps.get

# PowerAssert を確認するために Unit Testを記載

$ vim test/my_power_assert_ex_test.exs

defmodule MyPowerAssertExTest do
  use PowerAssert

  setup do
    {:ok, hello: "world"}
  end

  # PowerAssertEx を確認するための失敗するテストを確認
  test "Enum.at should return the element at the given index" do
    array = [1, 2, 3, 4, 5, 6]
    index = 2
    two = 2
    assert array |> Enum.at(index) == two
  end

  setup do
    {:ok, hello: "world"}
  end

  # 文字列比較のテストを失敗させた場合
  test "setup. assert with a match", %{hello: hello} do
   assert hello == "world1"
  end

  # PowerAssert なので assert_raise は意味がありませんがテストは実施可能です。
  test "defp one. assert with a match" do
    assert_raise FunctionError, fn ->
      MyExUnit.onep == 1
    end
  end
end

# PowerAssertExを使ってみる

$ mix test

  1) test setup. assert with a match (MyPowerAssertExTest)
     test/my_power_assert_ex_test.exs:21
     hello == "world1"
     |
     "world"
     stacktrace:
       test/my_power_assert_ex_test.exs:22

  2) test Enum.at should return the element at the given index (MyPowerAssertExTest)
     test/my_power_assert_ex_test.exs:9
     array |> Enum.at(index) == two
     |             |  |         |
     |             3  2         2
     [1, 2, 3, 4, 5, 6]
     stacktrace:
       test/my_power_assert_ex_test.exs:13

  3) test defp one. assert with a match (MyPowerAssertExTest)
     test/my_power_assert_ex_test.exs:26
     Expected exception FunctionError but got UndefinedFunctionError (undefined function: MyExUnit.onep/0 (module MyExUnit is not available))
     stacktrace:
       test/my_power_assert_ex_test.exs:27


Finished in 0.07 seconds (0.06s on load, 0.01s on tests)
3 tests, 3 failures

Randomized with seed 387497

# まとめ
今回はma2geさんの新しく作成されたライブラリを紹介させていただきました。
Githubでも、ToDoリストはまだ残っていらっしゃるのでアップデートがとても楽しみなライブラリだと思います。
ExUnitも同時に使用することもできますので、すぐにでも組み込み可能で、使用していて楽しいツールでもありますので、使ってみてはいかがでしょうか?

それでは皆さま、良いElixirライフを