我正在尝试测试一个构建HashDict的算法,但我不能让我的"预期" 实际上等于我的"实际".例如,
iex> expected = HashDict.new([ key: 12 ])
#HashDict<[foo: 12]>
iex> actual = Dict.put(HashDict.new, "key", 12)
#HashDict<[{"foo", 12}]>
Run Code Online (Sandbox Code Playgroud)
HashDict通过以更加钝的方式声明它,我可以获得文字中的第二个"风格" :
iex> HashDict.new([ { "key", 12 } ])
#HashDict<[{"foo", 12}]>
Run Code Online (Sandbox Code Playgroud)
我非常喜欢我的测试用例的简单Dict文字语法,但是我的实现以递归方式构建了Dict.我怎样才能使这些相等?
在第一种情况下,键是一个原子,但在第二种情况下,它是一个字符串.
你可以在iex上做到这一点:
expected = HashDict.new([key: 12])
actual = Dict.put(HashDict.new, :key, 12)
Dict.equals? actual, expected # returns true
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请访问:http://elixir-lang.org/docs/stable/