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.

Where Next?

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=> (create-migration "guestbook") Execution error (Ill...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
hazardco
On page 78 the following code appears: <%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New

Other popular topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
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
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
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

Sub Categories: