Created
August 12, 2016 23:40
-
-
Save derekmorr/dddffb4b848495f5ea27d32e30c994ab to your computer and use it in GitHub Desktop.
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
import org.scalacheck.Gen.choose | |
import org.scalatest.prop.GeneratorDrivenPropertyChecks | |
import org.scalatest.{WordSpec, MustMatchers} | |
class TruncateTest extends WordSpec with MustMatchers with GeneratorDrivenPropertyChecks { | |
def truncate(str: String, length: Int): String = { | |
if (str.length < length) | |
str | |
else if (length < 3 || str.length < 3) | |
"" | |
else | |
str.take(str.length-3) + "..." | |
} | |
"Truncate" must { | |
"output must be <= length of input" in { | |
forAll { (str: String, len: Int) => | |
truncate(str, len).length must be <= str.length | |
} | |
} | |
"output must be <= length of truncate length" in { | |
forAll { (str: String, len: Int) => | |
if (len < 3) | |
truncate(str, len).length mustEqual 0 | |
else | |
truncate(str, len).length must be <= len | |
} | |
} | |
"output must be the input if truncate length < string length" in { | |
forAll { (str: String, len: Int) => | |
whenever(str.length < len) { truncate(str, len) must equal (str) } | |
} | |
} | |
"output must start with the input" in { | |
forAll { (str: String, len: Int) => | |
whenever (len > 3) { truncate(str, len) must startWith (str) } | |
} | |
} | |
"long output must end with ..." in { | |
forAll { str: String => | |
whenever(str.length > 3) { | |
forAll(choose(3, str.length-1)) { len: Int => | |
truncate(str, len) must endWith ("...") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment