1

I want to test my function and use ScalaCheck.

The property test looks as following:

object WindowsSpec extends Properties("Windows") { val pathsGen = Gen.frequency( (3, "C:\\Windows\\DigitalLocker"), (2, "C:\\Temp"), (3, "Invalid folder"), (1, "C:\\Program Files\\Internet Explorer"), (4, "C:\\Program Files\\Windows Defender Advanced Threat Protection"), (1, "C:\\Program Files\\Windows Photo Viewer") ) val pathsGenContainer = Gen.containerOf[List,String](pathsGen) property("validPaths") = forAll(pathsGenContainer) { a: List[String] => println(a) Windows.arePathsValid(a).value.length > 0 } } 

Why do I get an empty list?

I tried in the shell to find out, if the generator works:

scala> :paste // Entering paste mode (ctrl-D to finish) val pathsGen = Gen.frequency( (3, "C:\\Windows\\DigitalLocker"), (2, "C:\\Temp"), (3, "Invalid folder"), (1, "C:\\Program Files\\Internet Explorer"), (4, "C:\\Program Files\\Windows Defender Advanced Threat Protection"), (1, "C:\\Program Files\\Windows Photo Viewer") ) val pathsGenContainer = Gen.containerOf[List,String](pathsGen) // Exiting paste mode, now interpreting. pathsGen: org.scalacheck.Gen[String] = org.scalacheck.Gen$$anon$1@6cb26245 pathsGenContainer: org.scalacheck.Gen[List[String]] = org.scalacheck.Gen$$anon$1@5335e968 scala> pathsGen pathsGen pathsGenContainer scala> pathsGen.sample res26: Option[String] = Some(C:\Program Files\Internet Explorer) scala> pathsGen val pathsGen: org.scalacheck.Gen[String] scala> pathsGenContainer.sample res27: Option[List[String]] = Some(List(C:\Temp, C:\Temp, C:\Program Files\Windows Defender Advanced Threat Protection, Invalid folder, C:\Program Files\Internet Explorer, C:\Program Files\Internet Explorer, C:\Temp, C:\Temp, C:\Program Files\Windows Photo Viewer, Invalid folder, C:\Program Files\Windows Defender Advanced Threat Protection, C:\Program Files\Windows Defender Advanced Threat Protection, C:\Windows\DigitalLocker, Invalid folder, C:\Program Files\Windows Photo Viewer, C:\Program Files\Windows Defender Advanced Threat Protection, C:\Temp, C:\Program Files\Internet Explorer, C:\Temp, C:\Program Files\Windows Defender Advanced Threat Protection, C:\Program Files\Windows Photo Viewer, Invalid folder, Invalid folder, C:\Windows\DigitalLocker, C:\Program... 

As you can see, the generator generate some inputs.
What am I doing wrong?

1
  • Your code looks correct. I've copypasted it into project with scalacheck set up and got full screen of generated paths). I've used such check: property("validPaths") = forAll(pathsGenContainer) { a: List[String] => println(a) a.forall(v => !v.isEmpty) } Commented Nov 8, 2017 at 16:00

1 Answer 1

2

TL;DR

pathsGenContainer can be simplified to Gen.listOf(pathsGen) so I will refer to that. It's perfectly reasonable for it to generate an empty list. In fact, it's desirable because the empty list is an edge case and therefore more likely to trigger bugs. If you want a non-empty list generator you can use Gen.nonEmptyListOf or Gen.nonEmptyContainerOf for other collections.

What's different between forAll and sample?

You observed that calling sample in the REPL usually generates a non-empty list, but running your forAll test always generates an empty list. Here's the difference:

  • forAll: When the test is run by the framework, it is run multiple times with different properties, one of which is size that varies within a configurable range (by default [0 - 100]). The listOf generator is size-aware and will pick up this property on every test. Since 0 is the minimal size it is always tested and you get an empty list.
  • sample: You call it manually only a few times and you don't pass any properties. So the generator simply generates a random list. If you kept calling sample, eventually you would get an empty list, but it's not deterministic. Note that 100% random data is not that useful for testing, we always want to make sure we check edge cases as well.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.