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 Pragmatic Bookshelf topics Top

johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
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
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New