dotdotdotpaul

dotdotdotpaul

Testing Elixir - MapSet.new vs. Enum.sort in unit test example

Title: Testing Elixir - MapSet.new vs. Enum.sort (p. 80 PDF)

The example on this page shows an assertion with the code:

code: assert Enum.sort(actual_fields_with_types) ==
       Enum.sort(@expected_fields_with_types)

But the previous code example (p.77) uses MapSet.New instead of Enum.sort:

code: assert MapSet.new(actual_fields_with_types) ==
       MapSet.new(@expected_fields_with_types)

Not sure which is better, TBH. :smiley:

Also while I’m here – p83, there’s a typo, attibribute should be attribute. :smiley:

…Paul

Marked As Solved

idlehands

idlehands

Author of Testing Elixir

Thanks @dotdotdotpaul! This is funny because I was using Enum.sort and Andrea was pushing for MapSet.new. I let him switch up the code sample (I wrote the majority of that chapter) and it looks like that led to some inconsistency. We will get fix it so that it used one or the other, but I will try to address the trade-offs here so you can decide which you prefer.

MapSet.new is fast and consistent, but, as @hauleth pointed out, it doesn’t allow duplicate entries. So… it honestly just depends on what you need. The thing that I tend to find myself doing is writing a single test assertion helper, called something like assert_unordered_lists_are_equal(lists, opts \\ []) and let the opts include :allow_dupes. Then, you have a clearly stated function and the implementation isn’t as important. In fact, depending on the options, you can do it in a different way. That only issue there, then would be slight inconsistency in the test output.

We don’t write a ton of helper functions in the samples, largely because the more complex or spread the code, the harder it is to fit on the page.

Again, thanks for catching the inconsistency. We’ll get that resolved, but ultimately, both solutions are just fine unless you need duplicates in your list.

Also Liked

dotdotdotpaul

dotdotdotpaul

Not sure if I submitted this right, LMK. :smiley:

hauleth

hauleth

Depends on what you want to test, as the behaviour of these is different in case of actual_fields_with_types = [1,2,1] and @expected_fields_with_types == [2, 1]

dotdotdotpaul

dotdotdotpaul

Yeah, I was thinking that myself, but either way, the text is inconsistent. Not sure if the intent was to switch back to Enum.sort and it didn’t get backported, or the intention was go forward with MapSet.new (which is actually described in the text as the method used) and wasn’t copied all the way through.

Popular Prag Prog topics Top

New
jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |> handle_event() |> render() but the correc...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
taguniversalmachine
It seems the second code snippet is missing the code to set the current_user: current_user: Accounts.get_user_by_session_token(session["...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Latest in PragProg

View all threads ❯