rustkas

rustkas

Property-Based Testing with PropEr, Erlang, and Elixir: the location of the function under test (page 411)

In the 5th Question of the third chapter, it was proposed to create a function, as well as a testing property to test its work. In your answer on page 411, author suggested to write in a function in a properties file. Interesting. Why did you decide to write the function under test here? May be the module solutions in the source code folder src would be better place for it? It is very interesting to know the course of your thoughts on this issue.

Source code of the module in the src folder would be like this:

-module(solutions).

-export([word_count/1]).

word_count(String) ->
% ...

Property based test source code in the test folder would be like this:

-module(prop_solutions).

-include_lib("proper/include/proper.hrl").

%%%%%%%%%%%%%%%%%%
%%% Properties %%%
%%%%%%%%%%%%%%%%%%
prop_word_count() ->
    ?FORALL(String, (non_empty(string())),
            (word_count(String) =:= alt_word_count(String))).
% ...

I also noticed that Fred placed the call to the function under test on the left in the comparison expression. Why? He already explained this to me earlier.

Full project.

Marked As Solved

ferd

ferd

Author of Property-Based Testing with PropEr, LYSE, & Erlang in Anger

In normal production code you’d expect to ship it would be correct to put all implementation code on src/ and keep tests in test/.

However, since this is a book, I put them the way they are so that I could have one test file and one solutions file, and keep them separate from the project code for chapters when I was working on them. It reduces some amount of scattering and made it easier for me to just focus on the exercises, while also simplifying the annotations internal to source files when selecting subsets that get to be included in the book.

A reader downloading the code archive would also be able to look at the project without getting its code interspersed with exercises, which they legitimately my want to skip over, which is something I wanted to avoid as a potential confusion here.

One of the things not seen by the reader (by design), for example, is that I wanted all code to be testable for the book, including failing cases. So there are chapters there are 3 or 4 copies of a module existing. In chapter 3 it’s possible I had src/thinking.erl (final version), src/thinking.erl.a (first iteration shown), src/thinking.erl.b (second iteration), and so on.

This is something I could do so that if PropEr got upgrades while writing the book, I could go back in and re-generate the snippets for result sets in a way that wouldn’t require me to re-write everything every single time, while still leaving an artifact on disk that the PragProg editing system can bring into the page.

What I’m saying here is that the choice of where to put the code in this case is one of code organization related to the book and its authoring process, and specifically here, keeping exercises and solutions separate from the chapter’s project both for my sake, but also for the sake of readers who might want to look ahead in the code bundle that ships with the book without caring for the exercises.

The code has a distinct purpose from shipping software, and its organization may reflect that.


A note on the last comment there: there is far less thought going into which order to put functions in the assertions than you are implying here. My other response mentions a possible approach I could suggest in general, but at the time of writing it the book, I didn’t ask myself that question at all, and I don’t think it really matters ultimately due to being in comparison. I think I can suggest swapping the order from what I typically write, but I would never call it out in a code review, if that helps qualify the strength of how I feel about this.

Also Liked

rustkas

rustkas

Fred, thanks a lot for the detailed answer. Your invaluable experience and the order of thought is very interesting, as it helps to better understand your plan and priorities.

The tool (rebar3), thanks to which it is easy and pleasant to learn Erlang, helps a lot.

Popular Pragmatic Bookshelf topics Top

jdufour
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
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 found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
hazardco
On page 78 the following code appears: <%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1020 16974 374
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New

Latest in PragProg Customers

View all threads ❯