Skip to content

Instantly share code, notes, and snippets.

@derekwyatt
Created September 6, 2011 13:01
Show Gist options
  • Save derekwyatt/1197472 to your computer and use it in GitHub Desktop.
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 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