Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vary the length of Gen.listOfN #845

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

ashawley
Copy link
Contributor

Fixes #844.

property("empty listOfN") = forAll(listOfN(0, arbitrary[Int])) { l =>
l.length == 0
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test of listOfN(0, ...) is redundant to the trivial test of listOfN above that is doing_.length <= n

val g = Gen.containerOfN[List,TimeUnit](100, arbitrary[TimeUnit])
Prop.forAllNoShrink(g) { (l: List[TimeUnit]) =>
l.toSet == TimeUnit.values.toSet
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote this, but it will fail occasionally. Could be just a trivial incomplete test using List#contains, since we know the underlying Gen.oneOf works correctly.

Comment on lines +1058 to +1060
def listOfN[T](n: Int, g: Gen[T]) =
choose(0, Integer.max(n, 0))
.flatMap(n => buildableOfN[List[T], T](n, g))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that it is a good idea overall. Having listOfN which behaves differently from containerOfN and buildableOfN would be really confusing. Personally I like to have all of them behaving in a similar way.

Wouldn't it be better to create a separate function (or functions) for creating generators like this one above?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like listOfAtMostN, containerOfAtMostN and buildableOfAtMostN :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said in #844, we might be able to consider it. We can discuss there.

@joroKr21
Copy link
Member

joroKr21 commented Oct 16, 2021

I will just weigh in my opinion as a user - I think changing the behaviour in this way has the potential to break too many tests. Also the current behaviour is consistent with the name listOfN. For these reasons I think it would be better to just change the documentation of listOfN and add new methods with "at most" semantics. If it's possible, calling buildableOfN for Set and Map could then be deprecated.

@ashawley
Copy link
Contributor Author

I agree. I raised the same concern of breaking tests in #844. The chance of generating lists less than N with listOfN is possible but usually pretty remote and actually is only possible on what your combined generator is or what the test is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Behavior of Gen.listOfN behaves different to what I was assuming based on the documentation
3 participants