tdd with elm
TRANSCRIPT
Elm-test……as a learning assistant
n_umiastowski
Remote meetup March 23rd 2017
Learning Elm
One goal : understand how Lists work in Elm (and in general)
One rule : don’t ask on Slack
Learning Elm
Let’s try the Test-first approach with a little exercise !
Learning Elm
A list…
1 2 3 4 5 6 7 8 9
List Int
…
Learning Elm
Getting to a List of Lists with a defined criteria
1
2
3
4
5
6
7
8
9
1st 2nd 3rd
…
…
…
…
Learning Elm
Defining ancestors and descendants :
1
2
3
4
5
6
7
8
9
1st 2nd 3rd
(8,9)
(9,0)
(1,2)
(2,3)
(3,0)
(4,5)
(5,6)
(6,7)
(7,0)
Learning Elm
[(1,2), (2,3),(3,0),(3,4),(4,5),(5,6),(6,0),(7,8),(8,9),(9,0),….,…]
[ [(1,2), (2,3)],[(3,4),(4,5),(5,6)],[(7,8),(8,9)] ,…,…]
->
From List (Int, Int) to List (List (Int, Int))
What would be the test?
How do I start?
[(1,2), (2,3),(3,0),(3,4),(4,5),(5,6),(6,0),(7,8),(8,9),(9,0)]
->
1/ Splitting criteria
2/ Let’s isolate it ->
The test does not work
Ouch !!!!!
So, what’s the process?
1/ Write a test 2/ The test fails->3/ Write type annotation
4/ Write the function
5/ Test passes !
Let’s design a solution
Now, can I split with that? Let’s think of a test…
->Split on Tuple.first < 3 ? OKBut then…Tuple.first < 6?Tuple.first < 9?Will be a nightmare!
Never forget
Keep
It
Stupid
Simple
KISS !
I’ve got an idea
Let’s think of another test….
->Split on Tuple.first between 0 and 3? OKTuple.first between 3 and 6? OKTuple.first between 6 and 9? OK
Let’s go for that!
Let’s get rid of unused values
Test ->
Code ->
Baby-steps
Test ->
Code->
Tail-recursion! How did I get there?
Recursion is easy
Clean code
Refactoring welcome
Got me there
Just one thing to add
Let’s add (0,3) at the beginning of the returned list
Done!
Test
->
Code->
We’re getting somewhere
And we know we can get this ->Now that we have this ->
Let’s write that function ->
The function
Let’s test the whole thing
[(0,3),(3,6),(6,9)]
[(1,2), (2,3),(3,0),(3,4),(4,5),(5,6),(6,0),(7,8),(8,9),(9,0)]
Simplify the test
Test->Code->
Keep only integration tests
Test the integration, and then go to fix units of code
Experiment 1
[(1,2), (2,3),(3,0) ,(3,4),(4,5),(5,6),(6,0) ,(7,8),(8,9),(9,0),(10,11),(11,0)]Experimentation:
< 3, < 6, < 9, < 11< 3 -> Test OK
< 6 -> Test KO
Drop this experiment, try another
Experiment 2[(1,2), (2,3),(3,0) ,(3,4),(4,5),(5,6),(6,0) ,(7,8),(8,9),(9,0),(10,11),(11,0) ]Experimentation:
< 3, then exclude this, and take < 6
Recursion nightmare
Drop this experiment, try another
2 failed attempts first!
As a conclusion
My exercise is documented with testsI can now test edge cases, wrong inputs, and write fuzz tests !If I come back in 2 months, the tests will remind me of the road to get to the result!
And most important… I succeded, and learnt, I stayed calm and focus, and it was FUN!
Questions ?