TDD
drogą do oświecenia w

Scali
Tomasz Borek
Konrad Malawski
SFI 2013
Tomasz Borek
@gmail.com

LAFK => lafkblogs.wordpress.com
Show of Hands
Java?
Python?
Scala?
APL?
Testing?
TDD?
REAL TDD?
ADHD?
Dziś będzie o:
testach,
Scali,
poznawanie przez testy,
narzędziach.
Narzędzia:
Scala - język
ScalaTest - DSL
SBT - Simple Build Tool
Mózg
a nawet dwa!
Droga do TDD

Brak

Manualne

"PO"

"PRZED"

TDD
PRAWDZIWY cykl TDD
tak, zdarza się, a czasem
nawet jest potrzebne!

minimum kodu
by przeszło

Błąd kompilacji ma znaczenie.
Żywa, krzycząca dokumentacja

AcceptanceTDD

TDDevelopment vs TDDesign
ATDD, dla UFO

it should "..." in pending
it should "do things" in {
// ...
}
ATDD, dla UFO

Tests!
ATDD, dla Anime

Tests!
Test jednostkowy
nie "gada z bazą"
nie potrzeba mu Internetu,
szybki,
odpowiednio nazwany,
jasny, przejrzysty, klarowny,
testuje jedną rzecz,
nie dwie, nie tysiąc
Struktura testu
behaviour of "Something"
it should "have expressive name" in {
// given
val anObject : UnderTest = new UnderTest();
// when
val result = anObject.testedMethod();
// then
result should be ("expressive");
}
Struktura testu
behaviour of "Something"
it should "have expressive name" in {
// given
val anObject = new UnderTest
// when
val result = anObject.testedMethod()
// then
result should be ("expressive")
}
The stairway to Scala...
... or highway to hell?
Scala is...

Simple,
but
Hard.
Scala to język...
Funkcyjny,
Obiektowy,
Statycznie typowany,
idealny dla DSLi,
na JVMie,
by Martin Odersky
Scala Collections
Scala Basics & Collections
def greet(p: Person) =
println(s"Hello ${p.name}")
val people = List(tomek, konrad)
people foreach { person =>
greet(person)
}
Scala Basics & Collections
val people: List[Person]
def greet(p: Person)
people foreach { greet }
people foreach greet
Scala REPL
Read
Evaluate
Print
Loop
Eksplorowanie uzupełnia Testowanie
Scala Basics & Collections

Tests!
Cechy = Traits
object with
ranger traits

object with
wizard traits
object with
nerd and
wizard traits
Damage? Dice!
Cechy = Traits
class D6Dice
extends RealDice(maxRoll = 6)
object D6 extends D6Dice

object P6 extends D6Dice
with PredictableDice
Cechy = Traits
behavior of "Weapon"
it should "hit to cause damage"

behavior of "SimpleSword"
it should "cause D6 damage"
Cechy = Traits

Tests!
Cechy = Traits
behavior of "SimpleAxe"
it should "cause D6 damage"
// Axe, Sword... behave like Weapon
Cechy = Traits

Tests!
Cechy = Traits
behavior of "+10 DMG Axe"
it should "always cause > 10 damage"
Cechy = Traits

Tests!
Cechy = Traits
behavior of "Sword of Nyarlathotep"
it should "roll DMG using D20"
it should "have base damage +10 DMG"
it should "add 'panic' magic effect"
Podsumowanie - Scala
● DSLs,
● Collections,
● Pattern Matching,
● Traits
Podsumowanie - Narzędzia
behaviour of "scala"
it should "ask them politely" in pending
it should "ask them loudly" in pending
it should "go eat" in pending

● SBT,
● REPL,
● ScalaTest,
● FlatSpec,
Podsumowanie - Testy
● Droga do TDD
● Testy przed != TDD
● Cykl
● Błąd kompilacji == Pierwszy Red
● Testy jednostkowe, nie integracyjne
● Testy jako Dokumentacja
● Given When Then
Podsumowanie - Linki
● SCKRK / Code Kata / Kraków Scala
● Tomasz Kaczanowski - practicalunittesting.com
● ScalaTest docs
● Scala docs
● Martin Odersky - Programming in Scala
● Cay Horstmann - Scala for The Impatient
● "Bruce Eckel's Atomic Scala" in pending ...
THANKS!
// todo implement this
behaviour of "those who ask questions"
they should "ask them politely" in pending
they should "ask them loudly" in pending
they should "go eat" in pending

Konrad Malawski - twitter: @ktosopl
Tomasz Borek - @gmail.com
SFI 2013

TDD drogą do oświecenia w Scali