Last active
May 25, 2024 10:19
-
-
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/b909fa2245834bee4a2e2006969e7c84ef84cbcf
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
// 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.4.2" | |
//> 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