Elixir:配置混合项目以始终将字符列表打印为列表吗? [英] Elixir: Configure a mix project to always print charlists as lists?

查看:38
本文介绍了Elixir:配置混合项目以始终将字符列表打印为列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试Phoenix应用程序时,我经常遇到比较预期记录ID和实际记录ID的情况.错误非常难以解释,因为Elixir始终将整数列表打印为字符列表,因此我的测试输出如下:

In testing my Phoenix app, I keep running into situations where I'm comparing lists of expected versus actual record IDs. Errors are tedious to interpret because Elixir keeps printing the integer lists as charlists, so my test output looks like:

     Assertion with == failed
     code:  assert H.sort(Enum.map(list1, &(&1.id()))) == H.sort(Enum.map(list2, &(&1.id())))
     left:  'stu'
     right: 'st'

这是在勉强我重写测试,以避免比较整数列表,这是可以容忍的,但是只是耸耸肩寻找这种语言的变通方法是可耻的.因此,我想知道是否有一种方法可以告诉Elixir/Mix总是 将整数列表打印为列表,而不是字符列表/字符串.我写的是Ruby风格的Elixir,但我几乎从不使用字符列表,对我来说,它们基本上是一个可以解决的陷阱.

This is nudging me to rewrite my tests to avoid comparing lists of integers, which is tolerable, but it's a shame to just shrug and look for a workaround in a language like this. So I'm wondering if there's a way to tell Elixir/Mix to always print integer lists as lists, rather than as charlists/charstrings. I write Ruby-styled Elixir and I almost never make use of charlists, to me they're mostly a gotcha to work around.

感谢此答案,我知道有一种方法可以将IEx配置为始终将整数列表打印为列表.在Mix中或在Elixir本身中是否有办法做到这一点,所以 mix test 将采用这种行为?

Thanks to this answer I know there's a way to configure IEx to always print integer lists as lists. Is there a way to do so in Mix, or globally in Elixir itself, so mix test will adopt that behavior?

推荐答案

人性化"角色列表也许是Elixir中最令人困惑的陷阱之一.正如 @Aleksei 指出的那样,这不能在 ExUnit 中进行配置(尽管您可以通过在 .iex.exs 文件中添加 IEx.configure(检查:[charlists::as_lists])来配置 iex .

The "humanizing" of charlists is perhaps one of the most confusing gotchas in Elixir. As @Aleksei points out, this isn't configurable in ExUnit (although you can configure iex by adding IEx.configure(inspect: [charlists: :as_lists]) in your .iex.exs file).

但是,您不需要重写测试.请记住,打印值的方式只是对数据的视图.它不会影响存储的数据方式.

However, you should not need to rewrite your tests. Keep in mind that the way that values are printed is only a view on the data. It does not affect how the data are stored.

您的示例断言失败,因为'stu'不等于'st',就像 [115,116,117] 不是等于 [115,116] .

Your example assertion failed because 'stu' is not equal to 'st' just as [115, 116, 117] is not equal to [115, 116].

看下面的代码:

iex> [115, 116, 117] === 'stu'
true

左侧和右侧100%完全相等.单引号仅使人类能够更轻松地输入数据.

The left and right sides are 100% exactly equivalent. The single-quotes merely allow humans to enter data more easily.

在特定情况下,您可能需要查看 MapSet ,特别是如果您的ID列表是唯一的.然后,您可以避免比较之前的尴尬/脆性排序.使用 MapSet ,您可以比较两个集合是否相等.例如:

In your particular case, you might want to take a look at MapSet, especially if your list of IDs are unique. Then you can avoid the awkward/brittle sorting prior to comparison. With MapSet, you can just compare the two sets for equality. For example:

expected_ids = MapSet.new([115, 116, 117])
actual_ids = MapSet.new(some_function_output)

assert MapSet.equal?(actual_ids, expected_ids)

这可能有助于避免单调乏味地查找由字符列表引起的错误.您的另一种选择可能是在映射过程中将整数转换为字符串,例如 Enum.map(list1,fn x-> to_string(x.id)结尾)

This might help avoid the tedious tracking down of errors caused by the charlists. Another option in your case might be to convert the integers to strings during the mapping, e.g. Enum.map(list1, fn x -> to_string(x.id) end)

这篇关于Elixir:配置混合项目以始终将字符列表打印为列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆