Created
October 26, 2015 13:46
-
-
Save jeroenr/be1ab400e4ddda652026 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import org.specs2.mutable.Specification | |
/** | |
* Created by jero on 26-10-15. | |
*/ | |
class FooTest extends Specification{ | |
trait Spec | |
trait Data | |
case class SpecA(id: String) extends Spec | |
case class SpecB(id: String) extends Spec | |
case class DataA(id: String) extends Data | |
case class DataB(id: String) extends Data | |
trait ConvertableTo[+D <: Data] { | |
def convert: PartialFunction[Spec, D] | |
} | |
class ConvertableToA extends ConvertableTo[DataA] { | |
override def convert = { case SpecA(id) => DataA(id) } | |
} | |
class ConvertableToB extends ConvertableTo[DataB] { | |
override def convert = { case SpecB(id) => DataB(id) } | |
} | |
implicit class ConvertableSpecList(specs: List[Spec]) { | |
val converters = List(new ConvertableToA, new ConvertableToB) // TODO: dynamically load all available converters | |
private val convert = converters.map(_.convert).reduceLeft(_ orElse _) | |
def toDataList = specs.map(convert) | |
} | |
"Foo" should { | |
"work" in { | |
val specs = List(SpecA("a"), SpecB("b")) | |
val result = specs.toDataList | |
result should beEqualTo(List(DataA("a"), DataB("b"))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment