本文整理汇总了Scala中org.scalatest.prop.PropertyChecks类的典型用法代码示例。如果您正苦于以下问题:Scala PropertyChecks类的具体用法?Scala PropertyChecks怎么用?Scala PropertyChecks使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PropertyChecks类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: Demo2Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FunSpec, Matchers}
// DEMO 2 - use assume with an ensuring that compares with math.sqrt
class Demo2Spec extends FunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRootB function") {
it("should compute the square root") {
forAll { (x: Double) =>
whenever (x >= 0.0 && !x.isPosInfinity) {
noException should be thrownBy { squareRootB(x) }
}
}
}
it("should should throw IAE on negative input") {
an [AssertionError] should be thrownBy {
squareRootB(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [AssertionError] should be thrownBy {
squareRootB(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:33,代码来源:Demo2Spec.scala
示例2: Demo11Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, Matchers}
// (Demo 10 - PosInt in the REPL)
// DEMO 11 - Use PosZInt to weaken the require?
// Only input is changed to PosZDouble so far.
class Demo11Spec extends LogicFunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
forAll { (x: PosZDouble) =>
whenever (!x.isPosInfinity) {
squareRoot2(x) should === (math.sqrt(x))
}
}
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:37,代码来源:Demo11Spec.scala
示例3: ODateTimeSpec
//设置package包名称以及导入依赖的类
package net.fosdal.oslo.odatetime
import org.joda.time.DateTime
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, WordSpec}
import scala.concurrent.duration._
class ODateTimeSpec extends WordSpec with Matchers with PropertyChecks {
"ordering order collections correctly" in new Fixture {
Seq(now, tomorrow, yesterday).sorted(ordering) shouldBe Seq(yesterday, now, tomorrow)
}
"comparisons" in new Fixture {
yesterday < now shouldBe true
yesterday > now shouldBe false
sameAsNow <= now shouldBe true
sameAsNow >= now shouldBe true
}
"Adding FiniteDuration to DateTime" in new Fixture {
forAll { millis: Int =>
now + millis.milliseconds shouldBe now.plusMillis(millis)
}
}
"Subtracting FiniteDuration from DateTime" in new Fixture {
forAll { millis: Int =>
now - millis.milliseconds shouldBe now.minusMillis(millis)
}
}
"Subtracting DateTime from DateTime" in new Fixture {
tomorrow - yesterday shouldBe 2.days
yesterday - tomorrow shouldBe -2.days
}
trait Fixture {
val now = DateTime.now
val sameAsNow = DateTime.now
val tomorrow = now.plusDays(1)
val yesterday = now.minusDays(1)
}
}
开发者ID:sfosdal,项目名称:oslo,代码行数:47,代码来源:ODateTimeSpec.scala
示例4: BarcoderTest
//设置package包名称以及导入依赖的类
package kokellab.utils.misc
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import com.google.zxing.BarcodeFormat
import org.scalacheck.Gen
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}
class BarcoderTest extends PropSpec with GeneratorDrivenPropertyChecks with Matchers {
def fakeEncodeDecode(text: String, barcodeFormat: BarcodeFormat, dimensions: (Int, Int), imageFormat: String): String =
if (text.isEmpty) text else encodeDecode(text.toUpperCase, barcodeFormat, dimensions, imageFormat)
def genBoundedList[T](maxSize: Int, gen: Gen[T]): Gen[List[T]] =
Gen.choose(0, maxSize) flatMap (size => Gen.listOfN(size, gen))
def genBoundedString(maxSize: Int, gen: Gen[Char]): Gen[String] =
Gen.choose(0, maxSize) flatMap (size => Gen.listOfN(size, gen) map (_.mkString))
def encodeDecode(text: String, codeFormat: BarcodeFormat, dimensions: (Int, Int), imageFormat: String): String = {
val barcoder = new Barcoder(codeFormat, imageFormat, dimensions._1, dimensions._2)
val os = new ByteArrayOutputStream()
barcoder.encode(text, os)
val is = new ByteArrayInputStream(os.toByteArray)
barcoder.decode(is)
}
val imageFormatGen = Gen.oneOf("png", "jpg", "gif")
def test(barcodeFormat: BarcodeFormat, dimensionsGen: Gen[(Int, Int)], stringGen: Gen[String]) = {
property(s"Decoding an encoded string should yield the original string for ${barcodeFormat.name} codes") {
forAll(imageFormatGen, stringGen, dimensionsGen) { (imageFormat: String, text: String, dimensions: (Int, Int)) =>
fakeEncodeDecode(text, barcodeFormat, dimensions, imageFormat) should equal (text.toUpperCase)
}
}
}
val rectangularGen: Gen[(Int, Int)] = for {
width <- Gen.choose(20, 100)
height <- Gen.choose(20, 100)
} yield (width, height)
val squareGen: Gen[(Int, Int)] = for {
size <- Gen.choose(20, 100)
} yield (size, size)
val code39And93Gen: Gen[String] = genBoundedString(48, Gen.frequency((36, Gen.alphaNumChar), (7, Gen.oneOf('-', '.', '$', '/', '+', '%', ' '))))
test(BarcodeFormat.CODE_39, rectangularGen, code39And93Gen)
test(BarcodeFormat.CODE_93, rectangularGen, code39And93Gen)
// TODO this fails due to https://github.com/zxing/zxing/issues/716
// there's nothing I can do now
// test(BarcodeFormat.CODE_128, rectangularGen, genBoundedString(48, Gen.choose[Char](0x20, 127)))
// TODO QR codes break; also not my fault
// test(BarcodeFormat.QR_CODE, squareGen, genBoundedString(4296, Gen.frequency((36, Gen.alphaNumChar), (8, Gen.oneOf('-', '.', '$', '/', '+', '%', ' ', ':')))))
}
开发者ID:kokellab,项目名称:kl-common-scala,代码行数:59,代码来源:BarcoderTest.scala
示例5: FileHasherTest
//设置package包名称以及导入依赖的类
package kokellab.utils.misc
import java.io.ByteArrayInputStream
import java.nio.file.Paths
import org.scalacheck.Arbitrary
import org.scalacheck.Gen
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}
import scala.io.Source
class FileHasherTest extends PropSpec with GeneratorDrivenPropertyChecks with Matchers {
val hasher = new FileHasher("SHA-256")
println(hasher.hash("aef46c1e5d854fcfd20ab427b599c86102b2fc6252fcdfe9be84dafdb58d6ffb".getBytes()))
val anyByteArray: Gen[List[Byte]] = Gen.listOf(Arbitrary.arbByte.arbitrary)
property("Correct hashes should validate") {
forAll(anyByteArray) { (array: List[Byte]) =>
val hex = hasher.hash(array)
hasher.validate(array, hex)
}
}
property("Incorrect hashes should not validate") {
// this is a bit weird, but if we generated a byte array and a string hash hex, we couldn't guarantee that the hashes don't match
forAll(anyByteArray, anyByteArray) { (arrayA: List[Byte], arrayB: List[Byte]) =>
if (arrayA != arrayB) {
a [ValidationFailedException] should be thrownBy {
val hexB = hasher.hash(arrayB)
hasher.validate(arrayA, hexB)
}
}
}
}
property("Can hash from stream") {
forAll(anyByteArray) { (array: List[Byte]) =>
val hex = hasher.hash(new ByteArrayInputStream(array.toArray))
hasher.validate(new ByteArrayInputStream(array.toArray), hex)
}
}
property("Can hash from file") {
val archive = Paths.get(this.getClass.getResource("test.7z").toURI)
val file = Paths.get(this.getClass.getResource("expected_file_to_hash.txt").toURI)
hasher.hash(archive) should equal ("afa411f16a1e7943cb07a57516c593384c097e8521f840b2112d2680877a2b04")
hasher.hash(file) should equal ("aef46c1e5d854fcfd20ab427b599c86102b2fc6252fcdfe9be84dafdb58d6ffb")
}
}
开发者ID:kokellab,项目名称:kl-common-scala,代码行数:53,代码来源:FileHasherTest.scala
示例6: AlgosTests
//设置package包名称以及导入依赖的类
package org.ergoplatform.settings
import org.ergoplatform.ErgoGenerators
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}
class AlgosTests extends PropSpec
with PropertyChecks
with GeneratorDrivenPropertyChecks
with Matchers
with ErgoGenerators with scorex.testkit.SerializationTests {
property("blockIdDifficulty should be > 0") {
forAll(genBytesList(32)) { id: Array[Byte] =>
assert(Algos.blockIdDifficulty(Algos.hash(id)) > 0)
}
}
}
开发者ID:ergoplatform,项目名称:ergo,代码行数:21,代码来源:AlgosTests.scala
示例7: ErgoMemPoolTest
//设置package包名称以及导入依赖的类
package org.ergoplatform.nodeView.mempool
import org.ergoplatform.ErgoGenerators
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}
import scala.concurrent.Await
import scala.concurrent.duration._
class ErgoMemPoolTest extends PropSpec
with PropertyChecks
with GeneratorDrivenPropertyChecks
with Matchers
with ErgoGenerators {
property("wait for the appearance of transactions") {
forAll(blockTransactionsGen) { blockTransactions =>
val memPool = ErgoMemPool.empty
val ids = blockTransactions.txs.map(_.id)
val transactionsFuture = memPool.waitForAll(ids)
memPool.put(blockTransactions.txs)
val transactionsFromMempool = Await.result(transactionsFuture, 5.seconds)
transactionsFromMempool should contain theSameElementsAs blockTransactions.txs
memPool.waitedForAssembly shouldBe 'empty
}
}
}
开发者ID:ergoplatform,项目名称:ergo,代码行数:29,代码来源:ErgoMemPoolTest.scala
示例8: RulesSpec
//设置package包名称以及导入依赖的类
package hanabi
import state._
import org.scalatest.mockito.MockitoSugar
import org.scalatest.BeforeAndAfter
import org.junit.runner.RunWith
import org.scalatest.OneInstancePerTest
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.junit.JUnitRunner
import hanabi._
import hanabi.ai._
import org.scalatest.prop.PropertyChecks
@RunWith(classOf[JUnitRunner])
class RulesSpec extends FlatSpec with Matchers with MockitoSugar with OneInstancePerTest with BeforeAndAfter
with PropertyChecks with HanabiDomain {
import SimpleRules._
import Cards._
"simple rules" should "have proper number of cards" in {
forAll { c: Card =>
whenever(c.level == 1) {
count(c) shouldBe 3
}
}
forAll { c: Card =>
whenever(c.level == 5) {
count(c) shouldBe 1
}
}
forAll { c: Card =>
whenever(c.level > 1 && c.level < 5) {
count(c) shouldBe 2
}
}
}
implicit override val generatorDrivenConfig = PropertyCheckConfiguration(
minSuccessful = 100,
maxDiscardedFactor = 15)
}
开发者ID:wl-seclin-hashcode,项目名称:hanabi,代码行数:43,代码来源:RulesSpec.scala
示例9: ComplexProperties
//设置package包名称以及导入依赖的类
// src/main/scala/progscala2/toolslibs/toolslibs/ComplexProperties.scala
package progscala2.toolslibs
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks
class ComplexProperties extends FunSuite with PropertyChecks {
def additionTest(a: Complex, b: Complex) = {
assert( (a + b).real === (a.real + b.real) )
assert( (a + b).imaginary === (a.imaginary + b.imaginary) )
}
def subtractionTest(a: Complex, b: Complex) = {
assert( (a - b).real === (a.real - b.real) )
assert( (a - b).imaginary === (a.imaginary - b.imaginary) )
}
val zero = Complex(0.0, 0.0)
test ("Complex addition with the identity element (zero)") {
forAll { (real: Double, imag: Double) =>
val c = Complex(real, imag)
additionTest(zero, c)
additionTest(c, zero)
}
}
test ("Complex subtraction with the identity element (zero)") {
forAll { (real: Double, imag: Double) =>
val c = Complex(real, imag)
subtractionTest(zero, c)
subtractionTest(c, zero)
}
}
test ("Complex addition with two values") {
forAll { (real1: Double, imag1: Double, real2: Double, imag2: Double) =>
val c1 = Complex(real1, imag1)
val c2 = Complex(real2, imag2)
additionTest(c1, c2)
}
}
test ("Complex subtraction with two values") {
forAll { (real1: Double, imag1: Double, real2: Double, imag2: Double) =>
val c1 = Complex(real1, imag1)
val c2 = Complex(real2, imag2)
subtractionTest(c1, c2)
}
}
}
开发者ID:vr1090,项目名称:scala-ing,代码行数:52,代码来源:ComplexProperties.scala
示例10: Demo17Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, WillMatchers}
// Demo 17 - use implies with q and q
class Demo17Spec extends LogicFunSpec with WillMatchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
forAll { (x: PosZDouble) =>
val p = x will !== (PosZDouble.PositiveInfinity)
val q = squareRoot3(x).value will === (math.sqrt(x))
p implies q
}
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] will be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] will be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:34,代码来源:Demo17Spec.scala
示例11: Demo13Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, Matchers}
// (Demo 12 PosInt.ensuringValid in the REPL)
// DEMO 13 - Use PosZDouble as result type
class Demo13Spec extends LogicFunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
forAll { (x: PosZDouble) =>
whenever (!x.isPosInfinity) {
squareRoot3(x).value should === (math.sqrt(x))
}
}
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:36,代码来源:Demo13Spec.scala
示例12: Demo16Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, WillMatchers}
// DEMO 16 use implies
class Demo16Spec extends LogicFunSpec with WillMatchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
forAll { (x: PosZDouble) =>
(x will !== (PosZDouble.PositiveInfinity)) implies {
squareRoot3(x).value will === (math.sqrt(x))
}
}
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] will be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] will be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:34,代码来源:Demo16Spec.scala
示例13: Demo15Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, Matchers, WillMatchers}
// (Demo 14 was REPL will and expect)
// DEMO 15 use Expectations with whenever
class Demo15Spec extends LogicFunSpec with WillMatchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
forAll { (x: PosZDouble) =>
whenever (!x.isPosInfinity) {
squareRoot3(x).value will === (math.sqrt(x))
}
}
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] will be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] will be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:36,代码来源:Demo15Spec.scala
示例14: Demo18Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Expectation, Fact, LogicFunSpec, WillMatchers}
// Demo 18 - use implies with Booleans
class Demo18Spec extends LogicFunSpec with WillMatchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
forAll { (x: PosZDouble) =>
x != PosZDouble.PositiveInfinity implies squareRoot3(x).value == math.sqrt(x)
}
}
it("should compute the square root (version 2)") {
forAll { (x: PosZDouble) =>
val p: Fact = x != PosZDouble.PositiveInfinity
val q: Fact = squareRoot3(x).value == math.sqrt(x)
p implies q
}
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] will be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] will be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:39,代码来源:Demo18Spec.scala
示例15: Demo9Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{LogicFunSpec, Matchers}
// DEMO 9 - Our forAll in a LogicSpec
class Demo9Spec extends LogicFunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
forAll { (x: Double) =>
whenever (x >= 0.0 && !x.isPosInfinity) {
squareRoot1(x) should === (math.sqrt(x))
}
}
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:33,代码来源:Demo9Spec.scala
示例16: Demo7Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import java.io.{File, FileWriter}
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FunSpec, Matchers}
// (Demo 6 was REPL demo of Assertion and Succeeded)
// DEMO 7 File output
class Demo7Spec extends FunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
val two = squareRoot1(4.0)
val outputFile = new FileWriter("squareRoot.out")
try outputFile.write(two.toString + "\n")
finally outputFile.close()
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:36,代码来源:Demo7Spec.scala
示例17: Demo4Spec
//设置package包名称以及导入依赖的类
package org.scalatest.examples
// Type-constructor polymorphism
import org.scalactic.TypeCheckedTripleEquals
import org.scalactic.anyvals.PosZDouble
import org.scalatest.{FunSpec, LogicFunSpec, Matchers, WillMatchers}
import org.scalatest.examples.Demo._
import org.scalatest.prop.PropertyChecks
// DEMO 4 - an example based test
class Demo4Spec extends FunSpec with Matchers with PropertyChecks with TypeCheckedTripleEquals {
describe("The squareRoot1 function") {
it("should compute the square root") {
val one = squareRoot1(1.0)
one shouldEqual 1.0
val two = squareRoot1(4.0)
two shouldEqual 2.0
val three = squareRoot1(9.0)
three shouldEqual 3.0
}
it("should should throw IAE on negative input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(-1.0)
}
}
it("should should throw IAE on positive infinity input") {
an [IllegalArgumentException] should be thrownBy {
squareRoot1(Double.PositiveInfinity)
}
}
}
}
开发者ID:bvenners,项目名称:scalaX2016Demos,代码行数:35,代码来源:Demo4Spec.scala
示例18: MonadProperties
//设置package包名称以及导入依赖的类
// src/test/scala/progscala2/fp/categories/MonadProperties.scala
package progscala2.fp.categories
import org.scalatest.FunSpec
import org.scalatest.prop.PropertyChecks
class MonadProperties extends FunSpec with PropertyChecks {
// Arbitrary function:
val f1: Int => Seq[Int] = i => 0 until 10 by ((math.abs(i) % 10) + 1)
describe ("Monad law for unit") {
it ("works for Sequence Monads") {
import SeqM._
val unitInt: Int => Seq[Int] = (i:Int) => unit(i)
forAll { (i: Int) =>
val seq: Seq[Int] = Seq(i)
assert( flatMap(unit(i))(f1) === f1(i) )
assert( flatMap(seq)(unitInt) === seq )
}
}
}
describe ("Monad law for function composition") {
it ("works for Sequence Monads") {
val f2: Int => Seq[Int] = i => Seq(i+1)
import SeqM._
forAll { (i: Int) =>
val seq = Seq(i)
assert( flatMap(flatMap(seq)(f1))(f2) ===
flatMap(seq)(x => flatMap(f1(x))(f2)) )
}
}
}
}
开发者ID:oops,项目名称:scala_test,代码行数:35,代码来源:MonadProperties.scala
示例19: InstitutionViewProtobufConverterSpec
//设置package包名称以及导入依赖的类
package hmda.query.serialization
import hmda.model.institution.InstitutionGenerators._
import hmda.persistence.model.serialization.InstitutionViewEvents.InstitutionViewStateMessage
import hmda.query.view.institutions.InstitutionView.InstitutionViewState
import hmda.query.serialization.InstitutionViewProtobufConverter._
import org.scalacheck.Gen
import org.scalatest.{ MustMatchers, PropSpec }
import org.scalatest.prop.PropertyChecks
class InstitutionViewProtobufConverterSpec extends PropSpec with PropertyChecks with MustMatchers {
val institutionList = Gen.listOf(institutionGen)
property("InstitutionViewState must serialize to protobuf and back") {
forAll(institutionList, Gen.choose(0: Long, 100000: Long)) { (i, seq) =>
val state = InstitutionViewState(i.toSet, seq)
val protobuf = institutionViewStateToProtobuf(state).toByteArray
institutionViewStateFromProtobuf(InstitutionViewStateMessage.parseFrom(protobuf)) mustBe state
}
}
}
开发者ID:cfpb,项目名称:hmda-platform,代码行数:23,代码来源:InstitutionViewProtobufConverterSpec.scala
示例20: TsCsvParserSpec
//设置package包名称以及导入依赖的类
package hmda.parser.fi.ts
import hmda.model.fi.ts.TsGenerators
import org.scalatest.prop.PropertyChecks
import org.scalatest.{ MustMatchers, PropSpec }
class TsCsvParserSpec extends PropSpec with PropertyChecks with MustMatchers with TsGenerators {
property("Transmittal Sheet must be parsed from CSV") {
forAll(tsGen) { (ts) =>
TsCsvParser(ts.toCSV) mustBe Right(ts)
}
}
property("Must return length error on too long csv") {
forAll(tsGen) { (ts) =>
TsCsvParser(ts.toCSV + "|0|1") mustBe Left(List("An incorrect number of data fields were reported: 23 data fields were found, when 21 data fields were expected."))
}
}
property("Must recognize blank fields at the end of ts") {
forAll(tsGen) { (ts) =>
TsCsvParser(ts.toCSV + "|") mustBe Left(List("An incorrect number of data fields were reported: 22 data fields were found, when 21 data fields were expected."))
}
}
val validTs = "1|0123456789|9|201301171330|2013|99-9999999|900|MIKES SMALL BANK XXXXXXXXXXX|1234 Main St XXXXXXXXXXXXXXXXXXXXX|Sacramento XXXXXX|CA|99999-9999|MIKES SMALL INC XXXXXXXXXXX|1234 Kearney St XXXXXXXXXXXXXXXXXXXXX|San Francisco XXXXXX|CA|99999-1234|Mrs. Krabappel XXXXXXXXXXX|916-999-9999|999-753-9999|[email protected]XX"
val unparsableTsOneField = "INVALID|0123456789|9|201301171330|2013|99-9999999|900|MIKES SMALL BANK XXXXXXXXXXX|1234 Main St XXXXXXXXXXXXXXXXXXXXX|Sacramento XXXXXX|CA|99999-9999|MIKES SMALL INC XXXXXXXXXXX|1234 Kearney St XXXXXXXXXXXXXXXXXXXXX|San Francisco XXXXXX|CA|99999-1234|Mrs. Krabappel XXXXXXXXXXX|916-999-9999|999-753-9999|[email protected]XX"
val unparsableTsTwoFields = "INVALID|0123456789|INVALID|201301171330|2013|99-9999999|900|MIKES SMALL BANK XXXXXXXXXXX|1234 Main St XXXXXXXXXXXXXXXXXXXXX|Sacramento XXXXXX|CA|99999-9999|MIKES SMALL INC XXXXXXXXXXX|1234 Kearney St XXXXXXXXXXXXXXXXXXXXX|San Francisco XXXXXX|CA|99999-1234|Mrs. Krabappel XXXXXXXXXXX|916-999-9999|999-753-9999|[email protected]XX"
val unparsableTsTwoFieldsWrongLength = "INVALID|0123456789|9|INVALID|2013|99-9999999|900|MIKES SMALL BANK XXXXXXXXXXX|1234 Main St XXXXXXXXXXXXXXXXXXXXX|Sacramento XXXXXX|CA|99999-9999|MIKES SMALL INC XXXXXXXXXXX|1234 Kearney St XXXXXXXXXXXXXXXXXXXXX|San Francisco XXXXXX|CA|99999-1234|Mrs. Krabappel XXXXXXXXXXX|916-999-9999|999-753-9999|[email protected]XX|0"
property("Must return name of non-integer field") {
TsCsvParser(unparsableTsOneField) mustBe Left(List("Record Identifier is not an integer"))
}
property("Must return names of non-integer fields") {
TsCsvParser(unparsableTsTwoFields) mustBe Left(List("Record Identifier is not an integer", "Agency Code is not an integer"))
}
property("Must return only length error on too long csv") {
TsCsvParser(unparsableTsTwoFieldsWrongLength) mustBe Left(List("An incorrect number of data fields were reported: 22 data fields were found, when 21 data fields were expected."))
}
}
开发者ID:cfpb,项目名称:hmda-platform,代码行数:45,代码来源:TsCsvParserSpec.scala
注:本文中的org.scalatest.prop.PropertyChecks类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论