Last active
August 29, 2015 14:06
-
-
Save ldacosta/045703aabc4aa5844c64 to your computer and use it in GitHub Desktop.
CleanString: Wrapper on String
This file contains hidden or 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
object Wrappers extends Serializable { | |
/* ********************************************************* */ | |
// Business of CleanString | |
// TODO: move this to a saner location | |
trait Wrapper[T] extends Serializable { | |
val value: T | |
} | |
trait StringWrapper extends Wrapper[String] { | |
override val value: String | |
override def toString = value | |
} | |
trait CleanString extends StringWrapper | |
object CleanString extends Serializable { | |
def apply(s:String): CleanString = { CleanStringImpl(functions.cleanString(s)) } | |
private case class CleanStringImpl(value: String) extends CleanString | |
} | |
// make it easy to go from "more general type" (String) to "more specific type" (CleanString). | |
// Hard the other way around. | |
implicit def string2CleanString(s: String): CleanString = CleanString(s) | |
// End of CleanString | |
/* ********************************************************* */ | |
} | |
import Wrappers._ | |
// UNIT TESTS:::: | |
"Creation of a CleanString" should "do nothing when string is already clean" in { | |
val aCleanString = "luisOrSomethingElse I can't think about" | |
assert(CleanString(aCleanString).value == aCleanString) | |
} | |
it should "clean 'dirty' strings" in { | |
val aDirtyString = "luis\"lala\"OrSomethingElse I can't think about" | |
val aCleanString = "luislalaOrSomethingElse I can't think about" | |
withClue(s"CleanString(${aDirtyString}) = ${CleanString(aDirtyString).value}") { | |
assert(CleanString(aDirtyString).value == aCleanString) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment