tdd with elm

30
Elm-test… …as a learning assistant n_umiastowski Remote meetup March 23rd 2017

Upload: nicolas-umiastowski

Post on 13-Apr-2017

30 views

Category:

Software


0 download

TRANSCRIPT

Page 1: TDD with Elm

Elm-test……as a learning assistant

n_umiastowski

Remote meetup March 23rd 2017

Page 2: TDD with Elm

Learning Elm

One goal : understand how Lists work in Elm (and in general)

One rule : don’t ask on Slack

Page 3: TDD with Elm

Learning Elm

Let’s try the Test-first approach with a little exercise !

Page 4: TDD with Elm

Learning Elm

A list…

1 2 3 4 5 6 7 8 9

List Int

Page 5: TDD with Elm

Learning Elm

Getting to a List of Lists with a defined criteria

1

2

3

4

5

6

7

8

9

1st 2nd 3rd

Page 6: TDD with Elm

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)

Page 7: TDD with Elm

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))

Page 8: TDD with Elm

What would be the test?

Page 9: TDD with Elm

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 ->

Page 10: TDD with Elm

The test does not work

Ouch !!!!!

Page 11: TDD with Elm

So, what’s the process?

1/ Write a test 2/ The test fails->3/ Write type annotation

4/ Write the function

5/ Test passes !

Page 12: TDD with Elm

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!

Page 13: TDD with Elm

Never forget

Keep

It

Stupid

Simple

KISS !

Page 14: TDD with Elm

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!

Page 15: TDD with Elm

Let’s get rid of unused values

Test ->

Code ->

Page 16: TDD with Elm

Baby-steps

Test ->

Code->

Page 17: TDD with Elm

Tail-recursion! How did I get there?

Page 18: TDD with Elm

Recursion is easy

Page 19: TDD with Elm

Clean code

Refactoring welcome

Got me there

Page 20: TDD with Elm

Just one thing to add

Let’s add (0,3) at the beginning of the returned list

Page 21: TDD with Elm

Done!

Test

->

Code->

Page 22: TDD with Elm

We’re getting somewhere

And we know we can get this ->Now that we have this ->

Let’s write that function ->

Page 23: TDD with Elm

The function

Page 24: TDD with Elm

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)]

Page 25: TDD with Elm

Simplify the test

Test->Code->

Page 26: TDD with Elm

Keep only integration tests

Page 27: TDD with Elm

Test the integration, and then go to fix units of code

Page 28: TDD with Elm

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!

Page 29: TDD with Elm

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!

Page 30: TDD with Elm

Questions ?