Skip to content

Apelsinka223/test_match

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hex.pm Downloads Build Status Coverage Status Inline docs

RecursiveMatch

Module for matching

What difference between Kernel.match?/2 and RecursiveMatch.match_r/3?

When you use Kernel.match?/2

  • can't use functions as pattern
  • can't match not strict equality (only ===, no ==)

RecursiveMatch.match_r/3 allows you:

  • use functions as patterns
  • match not strictly (with option strict: false)
  • ignore order of lists item (with option ignore_order: true)

What is for assert_match/3 and refute_match/3?

It is same as assert RecursiveMatch.match_r, but with detailed fail message.
ExUnit has no special message for match_r/3 and even no special message for match?/2 is not detailed enough, it has no diff in fail message.

assert_match/3 provides diff in test fail message

Installation

If available in Hex, the package can be installed by adding test_match to your list of dependencies in mix.exs:

Requires elixir ~> 1.5

def deps do
  [
    {:test_match, "~> 2.0"}
  ]
end

Usage

defmodule YourModule do
  import RecursiveMatch

  def function1 do
    ...
  end
  
  def function2 do
    ...
    match_r 1, 2 
    match_r a, b
    match_r :_, b
    match_r function1(), 1
    match_r [1, 2], [2, 1], ignore_order: true # true
    match_r 1, 1.0, strict: true               # false
    match_r {1, 2}, {2, 1}, ignore_order: true # false, nope :)
    ...
  end
end
defmodule YourModuleTest do
  use ExUnit.Case
  import RecursiveMatch

  test "some test" do
    ...
    assert_match 1, 2 # false
    assert_match :_, b
    assert_match a, b
    assert_match [1, 2], [2, 1], ignore_order: true
    refute_match 1, 1.0
    assert_match 1, 1.0, strict: false          
    refute_match a, c
    assert_match YourModule.function1(), 1
    ...
  end
end

Options

  • strict: when true compare using ===, when false compare using ==, default true
  • ignore_order, when true - ignore order of items in lists, default false
  • message: Custom message on fail

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/test_match.