Last active
May 27, 2023 06:28
-
-
Save dacr/1818041158e7170f64da14f79fad4cc4 to your computer and use it in GitHub Desktop.
Properties based testing using scalacheck / published by https://github.com/dacr/code-examples-manager #9974ee37-a717-4abe-a3d8-54b72c90f027/b3b69552c4ac3302da25e5e09c2c2e98c28faf59
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
// summary : Properties based testing using scalacheck | |
// keywords : scala, scalacheck, properties-based-testing | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 9974ee37-a717-4abe-a3d8-54b72c90f027 | |
// created-on : 2021-12-17T16:37:49+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.3.0" | |
//> using dep "org.scalacheck::scalacheck:1.17.0" | |
// --------------------- | |
import org.scalacheck.* | |
object ToTest { | |
def pow2(p: Int): Int = 1 << p | |
def camelTokenize(that: String): Array[String] = that.split("(?=[A-Z][^A-Z])|(?:(?<=[^A-Z])(?=[A-Z]+))") | |
def snakeToCamelCase(that: String): String = that.split("_").map(_.capitalize).mkString | |
def camelToSnakeCase(that: String): String = camelTokenize(that).map(_.toLowerCase).mkString("_") | |
def basename(filename:String):String = | |
filename | |
.split("[/](?=[^/]*$)", 2) | |
.last | |
.split("[.]",2) | |
.head | |
} | |
object ScalaCheckSpecification extends Properties("ScalaCheck") { | |
import Prop.{forAll, propBoolean} | |
import ToTest.* | |
// ------------------------------------------------------------------------------ | |
property("pow2") = forAll { (n: Int, m: Int) => | |
(n < m) ==> (pow2(n) < pow2(m)) | |
} | |
// ------------------------------------------------------------------------------ | |
property("snakeToCamelCase") = forAll { (in: String) => | |
snakeToCamelCase(in).size <= in.size && | |
camelToSnakeCase(snakeToCamelCase(in)) == in | |
} | |
// ------------------------------------------------------------------------------ | |
property("basename anything") = forAll { (in: String) => | |
val b = basename(in) | |
in.contains(b) | |
} | |
// ------------------------------------------------------------------------------ | |
} | |
//ScalaCheckSpecification.main(Array.empty) | |
ScalaCheckSpecification.check(Test.Parameters.default) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment