Created
September 6, 2011 13:01
-
-
Save derekwyatt/1197472 to your computer and use it in GitHub Desktop.
Illustrating flexible test object creation using named parameters, option monads, and list flattening
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
// This just illustrates some nice aspects of the Option type with List and 'flatten' as well as named parameters | |
// Some "complex" object here | |
val defaultSubField1: SubField = ... | |
val defaultSubField2: SubField = ... | |
val defaultSubField3: SubField = ... | |
def createTestComposite(identifier: String = "default", something: Boolean = true, | |
subField1: Option[SubField] = Some(defaultSubField1), | |
subField2: Option[SubField] = Some(defaultSubField2), | |
subField3: Option[SubField] = Some(defaultSubField3),) = { | |
CompositeClass(identifier, something, List(subField1, subField2, subField3).flatten) | |
} | |
val fullyDefaulted = createTestComposite() | |
val somethingIsFalse = createTestComposite(something = false) | |
val noSubField2AndDifferentId = createTestComposite(subField2 = None, identifier = "42") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment