Giter Site home page Giter Site logo

sztheory / power_assert_ex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ma2gedev/power_assert_ex

0.0 1.0 0.0 225 KB

Power Assert in Elixir. Shows evaluation results each expression.

Home Page: https://github.com/ma2gedev/power_assert_ex

License: Other

Elixir 100.00%

power_assert_ex's Introduction

Power Assert

hex.pm version hex.pm daily downloads hex.pm weekly downloads hex.pm downloads Build Status License

Power Assert makes test results easier to understand, without changing your ExUnit test code.

Demo

Example test is here:

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

Here is the difference between ExUnit and Power Assert results:

Difference between ExUnit and Power Assert

Enjoy ๐Ÿ’ช !

Installation

Add Power Assert to your mix.exs dependencies:

defp deps do
  [{:power_assert, "~> 0.2.0", only: :test}]
end

and fetch $ mix deps.get.

Usage

Replace use ExUnit.Case into use PowerAssert in your test code:

## before(ExUnit)
defmodule YourAwesomeTest do
  use ExUnit.Case  # <-- **HERE**
end

## after(PowerAssert)
defmodule YourAwesomeTest do
  use PowerAssert  # <-- **REPLACED**
end

Done! You can run $ mix test.

Use with ExUnit.CaseTemplate

Insert use PowerAssert with ExUnit.CaseTemplate.using/2 macro:

## before(ExUnit.CaseTemplate)
defmodule YourAwesomeTest do
  use ExUnit.CaseTemplate
end

## after(PowerAssert)
defmodule YourAwesomeTest do
  use ExUnit.CaseTemplate

  # add the following
  using do
    quote do
      use PowerAssert
    end
  end
end

protip: useful command to replace use ExUnit.Case

$ git grep -l 'use ExUnit\.Case' | xargs sed -i.bak -e 's/use ExUnit\.Case/use PowerAssert/g'

How to use with other framework depending on ExUnit such as ExSpec or ShouldI

ExSpec

Append use PowerAssert after use ExSpec:

defmodule ExSpecBasedTest do
  use ExSpec
  use PowerAssert   # <-- append

  describe "describe" do
    it "it" do
      assert something == "hoge"
    end
  end
end

See also: test/ex_spec/ex_spec_test.exs

ShouldI

Append use PowerAssert after use ShouldI:

defmodule ShouldTest do
  use ShouldI
  use PowerAssert   # <-- append

  should "inside should" do
    assert something == "hoge"
  end
end

See also: test/should/should_test.exs

API

Only provide assert macro:

assert(expression, message \\ nil)

Dependencies

  • ExUnit

Limitation

  • NOT SUPPORTED
    • match expression ex: assert List.first(x = [false])
    • fn expression ex: assert fn(x) -> x == 1 end.(2)
    • :: expression ex: << x :: bitstring >>
      • this means string interpolations also unsupported ex: "#{x} hoge"
    • sigil expression ex: ~w(hoge fuga)
    • quote arguments ex: assert quote(@opts, do: :hoge)
    • case expression
    • get_and_update_in/2, put_in/2, update_in/2, for/1
    • <<>> expression includes attributes <<@x, "y">>; <<x :: binary, "y">>
    • __MODULE__.Foo
    • many macros maybe caught error...

Resources

Author

Takayuki Matsubara (@ma2ge on twitter)

License

Distributed under the Apache 2 License.

Check LICENSE files for more information.

power_assert_ex's People

Contributors

lowks avatar ma2gedev avatar melpon avatar niku avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.