12. The Three Laws of TDD 1 - You are not allowed to write any production code unless it is to make a failing unit test pass.
13. The Three Laws of TDD 1 - You are not allowed to write any production code unless it is to make a failing unit test pass. 2 - You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
14. The Three Laws of TDD 1 - You are not allowed to write any production code unless it is to make a failing unit test pass. 2 - You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. 3 - You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
32. Sapir-Whorf hypotesis “ there is a systematic relationship between the grammatical categories of the language a person speaks and how that person both understands the world and behaves in it.”
34. for ( var i = 1 ; i <= 100 ; i++){ var output = i + ": " ; if (i % 3 == 0 ) output += "fizz" ; if (i % 5 == 0 ) output += "buzz" ; console.log(output); } 48 sec
35. for ( var i = 1 ; i <= 100 ; i++){ var output = i + ": " ; if (i % 3 == 0 ) output += "fizz" ; if (i % 5 == 0 ) output += "buzz" ; console.log(output); } But it’s wrong!
43. package biz . scalzo . tdd ; public class Fizzbuzzer { public String count ( int i ) { if ( i == 3 ) return "Fizz" ; return Integer . toString ( i ); } } Fizzbuzzer.java
46. package biz . scalzo . tdd ; public class Fizzbuzzer { public String count ( int i ) { if ( i == 3 ) return "Fizz" ; if ( i == 5 ) return "Buzz" ; return Integer . toString ( i ); } } Fizzbuzzer.java
48. package biz . scalzo . tdd ; public class Fizzbuzzer { public String count ( int i ) { if ( i % 3 == 0 ) return "Fizz" ; if ( i == 5 ) return "Buzz" ; return Integer . toString ( i ); } } Fizzbuzzer.java
53. package biz . scalzo . tdd ; public class Fizzbuzzer { public String count ( int i ) { if ( i % 3 == 0 ) return "Fizz" ; if ( i % 5 == 0 ) return "Buzz" ; return Integer . toString ( i ); } } Fizzbuzzer.java
54. package biz . scalzo . tdd ; public class Fizzbuzzer { public String count ( int number ) { if ( isMultipleOf ( number , 3 )) return "Fizz" ; if ( isMultipleOf ( number , 5 )) return "Buzz" ; return Integer . toString ( number ); } private boolean isMultipleOf ( int number , int multiple ) { return number % multiple == 0 ; } } Fizzbuzzer.java
56. package biz . scalzo . tdd ; public class Fizzbuzzer { public String count ( int number ) { if ( isMultipleOf ( number , 3 ) && isMultipleOf ( number , 5 )) return "FizzBuzz" ; if ( isMultipleOf ( number , 3 )) return "Fizz" ; if ( isMultipleOf ( number , 5 )) return "Buzz" ; return Integer . toString ( number ); } private boolean isMultipleOf ( int number , int multiple ) { return number % multiple == 0 ; } } Fizzbuzzer.java
57. package biz . scalzo . tdd ; public class Fizzbuzzer { public String count ( int number ) { String fizzOrBuzz = fizz ( number ) + buzz ( number ); return fizzOrBuzz . equals ( "" ) ? Integer . toString ( number ) : fizzOrBuzz ; } private boolean isMultipleOf ( int number , int multiple ) { return number % multiple == 0 ; } private String fizz ( int number ) { return isMultipleOf ( number , 3 ) ? "Fizz" : "" ; } private String buzz ( int number ) { return isMultipleOf ( number , 5 ) ? "Buzz" : "" ; } } Fizzbuzzer.java
67. package biz . scalzo . bdd ; import java . util . ArrayList ; import java . util . List ; import org . junit . Test ; import static org . hamcrest . MatcherAssert . assertThat ; import static org . hamcrest . Matchers .*; public class AFizzbuzzer { @Test public void shouldYellOneForOne () { assertThat ( new Fizzbuzzer (). count ( 1 ), is ( equalTo ( "1" ))); } } AFizzbuzzer.java
68. package biz . scalzo . bdd ; import java . util . ArrayList ; import java . util . List ; import org . junit . Test ; import static org . hamcrest . MatcherAssert . assertThat ; import static org . hamcrest . Matchers .*; public class AFizzbuzzer { @Test public void shouldYellOneForOne () { assertThat ( new Fizzbuzzer (). count ( 1 ), is ( equalTo ( "1" ))); } } AFizzbuzzer.java
69. @Test public void shouldYellTwoForTwo () { assertThat ( new Fizzbuzzer (). count ( 2 ), is ( equalTo ( "2" ))); } AFizzbuzzer.java
70. @Test public void shouldYellFizzForMultipleOfThree () { int [] fizzers = { 3 , 6 , 9 , 12 , 18 }; for ( int fizzer : fizzers ) { assertThat ( new Fizzbuzzer (). count ( fizzer ), is ( equalTo ( "Fizz" ))); } } AFizzbuzzer.java
71. @Test public void shouldYellBuzzForMultipleOfFive () { int [] buzzers = { 5 , 10 }; for ( int buzzer : buzzers ) { assertThat ( new Fizzbuzzer (). count ( buzzer ), is ( equalTo ( "Buzz" ))); } } AFizzbuzzer.java
72. @Test public void shouldYellFizzBuzzForMultipleOfThreeAndFive () { int [] fizzBuzzers = { 15 , 30 , 45 , 60 }; for ( int fizzBuzzer : fizzBuzzers ) { assertThat ( new Fizzbuzzer (). count ( fizzBuzzer ), is ( equalTo ( "FizzBuzz" ))); } } AFizzbuzzer.java
73. @Test public void shouldYellTheNumberForNotMultipleOfThreeOrFive () { for ( int plainNumber : plainNumbers ()) { assertThat ( new Fizzbuzzer (). count ( plainNumber ), is ( equalTo ( Integer . toString ( plainNumber )))); } } private List < Integer > plainNumbers () { List < Integer > numbers = new ArrayList < Integer >(); for ( int i = 1 ; i <= 100 ; i ++) numbers . add ( i ); for ( int i = 1 ; i < 35 ; i ++) numbers . remove ( new Integer ( i * 3 )); for ( int i = 1 ; i < 25 ; i ++) numbers . remove ( new Integer ( i * 5 )); return numbers ; } AFizzbuzzer.java
77. $ :. unshift File . join ( File . dirname ( __FILE__ ), *% w [.. lib ]) require "fizzbuzzer" describe "Fizzbuzzer" do it "should yell one for one" do Fizzbuzzer . new . count ( 1 ). should == '1' end end fizzbuzzer_spec.rb
78. it "should yell two for two" do Fizzbuzzer . new . count ( 2 ). should == '2' end fizzbuzzer_spec.rb
79. it "should yell Fizz for three" do Fizzbuzzer . new . count ( 3 ). should == 'Fizz' end fizzbuzzer_spec.rb
80. it "should yell Fizz for multiple of three" do [ 3 , 6 , 9 , 12 ]. each do | number | Fizzbuzzer . new . count ( number ). should == 'Fizz' end end fizzbuzzer_spec.rb
81. it "should yell Buzz for multiple of five" do [ 5 , 10 ]. each do | number | Fizzbuzzer . new . count ( number ). should == 'Buzz' end end fizzbuzzer_spec.rb
82. it "should yell FizzBuzz for multiple of three and five" do [ 15 , 30 , 45 ]. each do | number | Fizzbuzzer . new . count ( number ). should == 'FizzBuzz' end end fizzbuzzer_spec.rb
83. it "should yell the number itself for non multiple of three or five" do ( 1 . .100 ). select { | n | n % 3 != 0 && n % 5 != 0 }. each do | number | Fizzbuzzer . new . count ( number ). should == number . to_s end end fizzbuzzer_spec.rb
85. class Fixnum def fizzbuzzed ? "FizzBuzz" if self % 3 == 0 && self % 5 == 0 end def fizzed ? "Fizz" if self % 3 == 0 end def buzzed ? "Buzz" if self % 5 == 0 end end class Fizzbuzzer def count ( number ) number . fizzbuzzed ? || number . fizzed ? || number . buzzed ? || number . to_s end end fizzbuzzer.rb
86. describe "Fizzbuzzer" do it "should yell one for one" do Fizzbuzzer . new . count ( 1 ). should == '1' end it "should yell two for two" do Fizzbuzzer . new . count ( 2 ). should == '2' end it "should yell Fizz for multiple of three" do [ 3 , 6 , 9 , 12 ]. each do | number | Fizzbuzzer . new . count ( number ). should == 'Fizz' end end it "should yell Buzz for multiple of five" do [ 5 , 10 ]. each do | number | Fizzbuzzer . new . count ( number ). should == 'Buzz' end end it "should yell FizzBuzz for multiple of three and five" do [ 15 , 30 , 45 ]. each do | number | Fizzbuzzer . new . count ( number ). should == 'FizzBuzz' end end it "should yell the number itself for non multiple of three or five" do ( 1 . .100 ). select { | n | n % 3 != 0 && n % 5 != 0 }. each do | number | Fizzbuzzer . new . count ( number ). should == number . to_s end end end fizzbuzzer_spec.rb
94. Feature : perfect fizzbuzzing As a inattentive counter I want an automatic Fizzbuzzer So that I can win Fizzbuzz Championsip fizzbuzzer.feature
95. Scenario : manage fizz numbers Given a Fizzbuzzer When I ask for a multiple of 3 Then Fizzbuzzer yells 'Fizz' fizzbuzzer.feature
96. require "fizzbuzzer" Given "a Fizzbuzzer" do @fizzbuzzer = Fizzbuzzer . new end When "I ask for a multiple of $m" do | m | @result = [ 1 , 2 ]. map { | base | @fizzbuzzer . count ( base * eval ( m ). to_i ) } end Then "Fizzbuzzer yells '$yell'" do | yell | @result . each do | result | result . should == yell end end fizzbuzzer_steps.rb
97. Scenario : manage buzz numbers Given a Fizzbuzzer When I ask for a multiple of 5 Then Fizzbuzzer yells 'Buzz' Scenario : manage fizzbuzz numbers Given a Fizzbuzzer When I ask for a multiple of 3 * 5 Then Fizzbuzzer yells 'FizzBuzz' fizzbuzzer.feature
98. Scenario : manage plain numbers Given a Fizzbuzzer When I ask for a plain number Then Fizzbuzzer yells the number itself fizzbuzzer.feature
99. When "I ask for a plain number" do @expected = ( 1 . .100 ). select { | n | n % 3 != 0 && n % 5 != 0 } @result = @expected . each do | number | @fizzbuzzer . count ( number ). to_i end end Then "Fizzbuzzer yells the number itself" do @result . should == @expected end fizzbuzzer_steps.rb
100. Scenario Outline : manage plain numbers Given a Fizzbuzzer When I ask for '<number>' Then Fizzbuzzer yells '<result>' Examples : | number | result | | 1 | 1 | | 2 | 2 | | 3 | Fizz | | 4 | 4 | | 5 | Buzz | | 6 | Fizz | fizzbuzzer.feature
101. When "I ask for '$m'" do | number | @result = @fizzbuzzer . count ( number . to_i ) end Then "Fizzbuzzer yells '$yell'" do | yell | @result . each do | result | result . should == yell end end fizzbuzzer_steps.rb
106. Feature : perfect fizzbuzzing As a inattentive counter I want an automatic Fizzbuzzer So that I can win Fizzbuzz Championsip Scenario : manage fizz numbers Given a Fizzbuzzer When I ask for a multiple of 3 Then Fizzbuzzer yells 'Fizz' Scenario : manage buzz numbers Given a Fizzbuzzer When I ask for a multiple of 5 Then Fizzbuzzer yells 'Buzz' Scenario : manage fizzbuzz numbers Given a Fizzbuzzer When I ask for a multiple of 15 Then Fizzbuzzer yells 'FizzBuzz' manage_fizzbuzzer
107. Scenario : manage plain numbers Given a Fizzbuzzer When I ask for a plain number Then Fizzbuzzer yells the number itself Scenario : manage plain numbers Given a Fizzbuzzer When I ask for '[number]' Then Fizzbuzzer yells '[yell]' Examples : | number | yell | | 1 | 1 | | 2 | 2 | | 3 | Fizz | | 4 | 4 | | 5 | Buzz | | 6 | Fizz | manage_fizzbuzzer
108. package biz . scalzo . jbehave ; import org . jbehave . scenario . Scenario ; public class ManageFizzbuzzer extends Scenario { public ManageFizzbuzzer () { super ( new ManageFizzbuzzerSteps ()); } } ManageFizzbuzzer.java
109. import biz . scalzo . bdd . Fizzbuzzer ; public class ManageFizzbuzzerSteps extends Steps { private Fizzbuzzer fizzbuzzer ; private List < String > results ; private List < String > expected ; private String result ; @BeforeScenario public void setUp () { expected = new ArrayList < String >(); results = new ArrayList < String >(); } ManageFizzbuzzerSteps.java
110. @ Given ( "a Fizzbuzzer" ) public void startFizzbuzzer () { fizzbuzzer = new Fizzbuzzer (); } @ When ( "I ask for a multiple of $divisor" ) public void askForMultipleOf ( int divisor ) { for ( int number = 1 ; number < 3 ; ++ number ) results . add ( fizzbuzzer . count ( number * divisor )); } @ Then ( "Fizzbuzzer yells '$yell'" ) public void checkYell ( String yell ) { for ( String result : results ) { assertThat ( result , is ( equalTo ( yell ))); } } ManageFizzbuzzerSteps.java
111. @ When ( "I ask for a plain number" ) public void askForPlainNumbers () { for ( int i = 1 ; i <= 100 ; i ++) if ( i % 3 != 0 && i % 5 != 0 ) expected . add ( Integer . toString ( i )); for ( String number : expected ) results . add ( fizzbuzzer . count ( Integer . parseInt ( number ))); } @ Then ( "Fizzbuzzer yells the number itself" ) public void yellsNumberItself () { for ( int i = 0 ; i < expected . size (); i ++) { assertThat ( results . get ( i ), is ( equalTo ( expected . get ( i )))); } } ManageFizzbuzzerSteps.java
112. @ When ( "I ask for '[number]'" ) public void askFor ( @ Named ( "number" ) int number ) { result = fizzbuzzer . count ( number ); } @ Then ( "Fizzbuzzer yells '[yell]'" ) public void checkResult ( @ Named ( "yell" ) String yell ) { assertThat ( result , is ( equalTo ( yell ))); } ManageFizzbuzzerSteps.java
120. Feature: Evolving a living cell In order to create a functioning rules engine As a programmer of Conway's Game of Life I can evolve a single living cell Scenario: Living cell with 0 neighbors dies Given the following setup | . | . | . | | . | x | . | | . | . | . | When I evolve the board Then the center cell should be dead Scenario: Living cell with 1 neighbor dies Given the following setup | . | x | . | | . | x | . | | . | . | . | When I evolve the board Then the center cell should be dead Scenario: Living cell with 2 neighbors lives Given the following setup | . | x | . | | . | x | x | | . | . | . | When I evolve the board Then the center cell should be alive Scenario: Living cell with 3 neighbors lives Given the following setup | x | x | x | | . | x | . | | . | . | . | When I evolve the board Then the center cell should be alive
si usa un semplice template per testare le classi. ogni classe ha un suo test case e aiuta a rimanere focalizzati
la classe ‘dovrebbe’ non ‘deve’ fare qualcosa in modo da indicare un comportamento possibile: se c’è un errore potrebbe essere un bug oppure potrebbe essere che quel comportamento non è più di quella classe
conversazione. rspec per sviluppatori. vecchio sogno che gli stakeholders possano scrivere direttamente le specifiche. si può avere un linguaggio di più alto livello