Last active
November 21, 2017 09:07
-
-
Save tokubass/8d481785845ada8ac574b0cf4a4e3a48 to your computer and use it in GitHub Desktop.
map、structの一部の要素だけをテストしたい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## todo | |
## error messageの変数名がtest caseのほうではない | |
## leftはrightのsubsetだがrightにマージしているので値が変わっているのでわかりにくい?ここは仕様の問題でもあると思うが、、 | |
defmodule AssertionMatch do | |
import ExUnit.Assertions | |
def assert_match?(left, right) do | |
right_map = case right do | |
%_{} -> Map.from_struct(right) | |
%{} -> right | |
_ -> :error | |
end | |
left = Map.merge(right_map , left); | |
assert left == right_map | |
end | |
end | |
defmodule Struct do | |
defstruct a: 1, b: 1, z: 10 | |
end | |
defmodule Main do | |
left_main = %{a: 1, b: 2 }; | |
right_main = %Struct{} | |
import AssertionMatch | |
assert_match?(left_main,right_main) | |
# Assertion with == failed | |
# code: left == right_map | |
# left: %{a: 1, b: 2, z: 10} | |
# right: %{a: 1, b: 1, z: 10} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## error messageの変数名を呼び出し元のほうにあわせた | |
## todo | |
## leftはrightのsubsetだがrightにマージしているので値が変わっているのでわかりにくい?ここは仕様の問題でもあると思うが,, | |
## struct からmapの変換失敗時にraiseする | |
defmodule AssertionMatch do | |
defmacro assert_match?({left_name,_,_} = left, {right_name,_,_} = right) do | |
quote do | |
import ExUnit.Assertions | |
r_map = case unquote(right) do | |
%_{} -> Map.from_struct(unquote(right)) | |
%{} -> unquote(right) | |
_ -> :error | |
end | |
left = Map.merge(r_map , unquote(left)); | |
assert left == r_map, | |
expr: "assert_match?(#{unquote(left_name)}, #{unquote(right_name)})", | |
left: left, | |
right: r_map | |
end | |
end | |
end | |
defmodule Struct do | |
defstruct a: 1, b: 1, z: 10 | |
end | |
defmodule Main do | |
left_main = %{a: 1, b: 2 }; | |
right_main = %Struct{} | |
import AssertionMatch | |
assert_match?(left_main,right_main) | |
#code: assert_match?(left_main, right_main) | |
#left: %{a: 1, b: 2, z: 10} | |
#right: %{a: 1, b: 1, z: 10} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## todo | |
## struct からmapの変換失敗時にraiseする | |
## テストを書く | |
## macroの名前を変える。 assert match?のようにmatchはパターンマッチの意味になっている。assert_subset?()のほうがよさそう | |
## エラーメッセージを読みやすく。色つける | |
defmodule AssertionMatch do | |
defmacro assert_match?({got_var_name,_,_} = got, {expect_var_name,_,_} = expect ) do | |
quote do | |
import ExUnit.Assertions | |
got_map = case unquote(got) do | |
%_{} -> Map.from_struct(unquote(got)) | |
%{} -> unquote(got) | |
_ -> :error | |
end | |
got_var = case unquote(got_var_name) do | |
:%{} -> inspect(unquote(got)) | |
_ -> unquote(got_var_name) | |
end | |
expect = Map.merge(got_map, unquote(expect)); | |
expect_var = case unquote(expect_var_name) do | |
:%{} -> inspect(unquote(expect)) | |
_ -> unquote(expect_var_name) | |
end | |
assert got_map == expect, | |
expr: "assert_match?(#{got_var}, #{expect_var})", | |
left: got_map, | |
right: expect | |
end | |
end | |
end | |
defmodule Struct do | |
defstruct a: 1, b: 1, z: 10 | |
end | |
defmodule Main do | |
got_main = %Struct{} | |
expect_main = %{a: 1, b: 2 }; | |
import AssertionMatch | |
assert_match?( | |
got_main, | |
expect_main | |
) | |
assert_match?( | |
got_main, | |
%{ | |
a: 1, | |
b: 2 | |
} | |
) | |
#code: assert_match?(got_main, %{a: 1, b: 2}) | |
#left: %{a: 1, b: 1, z: 10} | |
#right: %{a: 1, b: 2, z: 10} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment