Elixir 编写自定义断言宏
有时,我必须在Elixir测试中比较多行字符串,当所有行都以内联方式打印时(如Elixir 编写自定义断言宏,elixir,Elixir,有时,我必须在Elixir测试中比较多行字符串,当所有行都以内联方式打印时(如line1\nline2\nline3)很难读取 我想编写一个自定义测试断言compare,在两列中显示LHS和RHS 作为起点,我创建了一个名为compare的宏,它与assert相同 defmodule CompareAssertion do defmacro __using__(_) do quote do require CustomCompare import Custom
line1\nline2\nline3
)很难读取
我想编写一个自定义测试断言compare
,在两列中显示LHS和RHS
作为起点,我创建了一个名为compare
的宏,它与assert
相同
defmodule CompareAssertion do
defmacro __using__(_) do
quote do
require CustomCompare
import CustomCompare
end
end
defmacro compare(assertion) do
case translate_assertion(assertion) do
nil ->
quote do
value = unquote(assertion)
unless value do
raise ExUnit.AssertionError,
expr: unquote(Macro.escape(assertion)),
message: "Expected truthy, got #{inspect value}"
end
value
end
value ->
value
end
end
# ...
end
我有一个函数render\u columns(col\u a,col\u b)
,它将两个字符串并排呈现为列。如何将这两者集成在一起,使LHS和RH显示在两列中
谢谢 您的代码与assert不同。您应该实现一个新的格式化程序。谢谢@josemrb我要一个look@CoderDennis你说得对,我只讨论了处理==比较的部分。我对匹配(=)断言不感兴趣。我链接到的行显示了一个提取
左
和右
部分的示例,我认为这可能会对您有所帮助。我真的不明白你还想问这个问题。你的代码和断言不一样。您应该实现一个新的格式化程序。谢谢@josemrb我要一个look@CoderDennis你说得对,我只讨论了处理==比较的部分。我对匹配(=)断言不感兴趣。我链接到的行显示了一个提取左
和右
部分的示例,我认为这可能会对您有所帮助。我真的不明白你还想问这个问题。