Advanced Scala

Download Advanced Scala

Post on 10-Nov-2014

988 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

This presentation was part of a seminar in a masters course. It covers 3 chapters of Martin Odersky's Book "Programming in Scala" (Packages and Imports, Assertions and Unit testing, Case Classes and Pattern Matching).

TRANSCRIPT

<ul><li> 1. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Masterseminar Scala Fabian Becker FH Giessen-Friedberg 15. November 2010 </li> <li> 2. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Content 1 Packages &amp; Imports 2 Assertions and Unit Testing 3 Case Classes and Pattern Matching </li> <li> 3. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Packages Important to minimize coupling Scala code resides in Java global hierarchy of packages Per default code is in the unnamed package There are two ways to place code inside packages Listing 1.1 package database.mysql class Query </li> <li> 4. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Listing 1.2 package database { package mysql { class Query } } Listing 1.3 package database.mysql { class Query } </li> <li> 5. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Packages Packages in Scala truly nest In Java packages are in a hierarchy they dont nest Naming a package in Java you always start at the root </li> <li> 6. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Listing 1.4: Accessing packages package game { package ui { class Player } package engine { class Main { // Java: new game.ui.Player() val player = new ui.Player } } } </li> <li> 7. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Listing 1.5: More complex package ui { class Player } // In ui.scala package game { // In game.scala package ui { class Player } package engine { package ui { class Player } class Main { val player = new ui.Player val player2 = new game.ui.Player val player3 = new _root_.ui.Player } } } </li> <li> 8. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Imports Importing packages Importing packages allows to directly access members in that package Scala imports are similar to the imports from Java Imports can appear anywhere </li> <li> 9. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Imports Listing 1.6.1: Dening Enemies package game abstract class Enemy( val name: String, var lvl: Int ) object Enemies { object Dwarf extends Enemy("Gimli", 10) object Orc extends Enemy("Bwarg", 5) object Necromancer extends Enemy("Destructor", 99) val col = List(Dwarf, Orc, Necromancer) } </li> <li> 10. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Imports Listing 1.6.2: Importing Enemies // Simple import for Enemy import game.Enemy // Import all members of game import game._ // Import all members of Enemies import game.Enemies._ </li> <li> 11. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Imports Listing 1.7: Import in a function def showEnemy(enemy: Enemy) { import enemy._ println("Enemy - Name:"+name+" Level:"+lvl) } Imports Imports can appear in functions They import all members of a object </li> <li> 12. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Selective Imports Listing 1.8: Selective Imports // Only import Dwarf and Orc import game.Enemies.{Dwarf, Orc} // Renaming an import import game.Enemies.{Necromancer =&gt; Necro} // Rename one, import the rest import game.Enemies.{Orc =&gt; O, _} // Exclude an import import game.Enemies.{Orc =&gt; _, _} </li> <li> 13. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Selective Imports Selective Imports Import: import game.Enemies.{Orc} game.Enemies.Orc Rename: originalname new name Hide Packages: original name Catch All: importgame.Enemies.{ } game.Enemies. </li> <li> 14. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Implicit imports Listing 1.9: Implicit imports import java.lang._ // everything in the java.lang package import scala._ // everything in the scala package import Predef._ // everything in the Predef object Implicit imports Packages imported implicitly by Scala scala. overshadows java.lang. scala.StringBuilder java.lang.StringBuilder </li> <li> 15. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Access modiers Listing 1.10: Private modiers class Outer { class Inner { private def f() { println("f") } class MostInner { f(); // OK } } (new Inner).f() // fails } Private Scala: private members are only accessible inside the class or object that contain their denition Java allows outer classes to access private members of their inner classes </li> <li> 16. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Access modiers Protected Java: protected members are accessible within the same package Scala: Only from the object they are dened in and their subclasses Public There is no explicit modier for public All members not labeled private or protected are public Accessable from anywhere </li> <li> 17. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Access modiers Scope of protection Scala allows to control access to a level X private[X] and protected[X] allow access up to X Listing 1.11: Example package game { package ui { private[game] class Player { private[this] var hp = 42 protected[ui] def getHP() { hp } } } } </li> <li> 18. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Assertions and Unit Testing Testing Code Assertions for run-time checking Unit testing for development testing of small units </li> <li> 19. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Assertions Listing 2.1: Assert def add1(x: Int) : Int = { val z = x + 1 assert(z &gt; x) return z } println(add1(41)) Assertion methods Method assert predened in Predef assert(condition) assert(condition, explanations) If the condition doesnt hold, an AssertionError is thrown </li> <li> 20. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Assertions Listing 1.13: Ensuring def lvlUp(e: Enemy): Enemy = { if(e.lvl &lt; 100) { e.lvl = e.lvl + 1 } ensuring(e.lvl &lt; 100) e } Ensuring Method assert predened in Predef assert(condition) assert(condition, explanations) If the condition doesnt hold, an AssertionError is thrown </li> <li> 21. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing ScalaTest org.scalatest.Suite (http://www.scalatest.org/) Can be executed on the Scala console: (new HitchhikerSuite).execute() Listing 2.3: Simple testcase import org.scalatest.Suite class HitchhikerSuite extends Suite { def testAnswerToLife() { val answer = 42 assert(answer == 42) } } </li> <li> 22. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing ScalaTest with Fun ScalaTest allows dierent styles of testing FunSuite is a trait that overrides execute and allows to dene tests as function values Naming the tests is easier (you can use Strings) Listing 2.4: FunSuite import org.scalatest.FunSuite class HitchhikerSuite extends FunSuie { test("The answer to life should be 42") { val answer = 42 assert(answer == 42) } } </li> <li> 23. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing ScalaTest - Informative testing A normal assertion produces long error messages but do not show why the match failed ScalaTest denes the === operator for assert(), shows the values Alternative: expect(value) { block } Listing 2.5: Informative testing assert(answer === 42) // or expect(42) { answer } </li> <li> 24. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing ScalaTest - Catching Exceptions Testing for Exceptions can be done with intercept() Listing 2.6: Intercept s = "hi" intercept[IndexOutOfBoundsException] { s.charAt(-1) } </li> <li> 25. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing JUnit Most popular test framework for Java Can easily be used from Scala Does not natively support Scalas assertion syntax Listing 2.7: JUnit Testcase import junit.framework.TestCase import junit.framework.Assert.assertEquals import junit.framework.Assert.fail class MyTestCase extends TestCase { def testAnswer() { val answer = 42 assertEquals(42, answer) } } </li> <li> 26. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing JUnit + Scala ScalaTest denes JUnit4Suite (requires JUnit 4 of course) JUnit4Suite extends TestCase Listing 2.8: Using Scala syntax in JUnit import org.scalatest.junit.JUnit4Suite class AnswerSuite extends JUnit4Suite { def testAnswerToLife() { var answer = 42 assert(answer === 42) } } </li> <li> 27. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing TestNG TestNG framework inspired by JUnit/nUnit Uses annotations Listing 2.9: TestNG import org.testng.annotations.Test import org.testng.Assert.assertEquals class AnswerTests { @Test def verifyAnswerToLife() { val answer = 42 assertEquals(answer, 42) } } </li> <li> 28. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing TestNG + Scala ScalaTest denes trait TestNGSuite TestNGWrapperSuite enables TestNG to test Scala while the tests are written in Java Listing 2.10: TestNG + Scala import org.scalatest.testng.TestNGSuite import org.testng.annotations.Test class AnswerSuite extends TestNGSuite { @Test def verifyAnswerToLife() { val answer = 42 expect(42) { answer } assert(answer === 42) } } </li> <li> 29. Packages &amp; Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing Listing 2.11: TestNG + Scala import org.scalatest.Spec class AnswerSpec extends Spec { "The Answer" -- { "should be an integer" - { val answer = 42 assert(answer.isInstanceOf[Int] == true) } "should be 42" - { val...</li></ul>