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

Simplify API #331

Closed
gunar opened this issue Mar 8, 2019 · 3 comments
Closed

Simplify API #331

gunar opened this issue Mar 8, 2019 · 3 comments

Comments

@gunar
Copy link
Contributor

gunar commented Mar 8, 2019

The current API is expressive enough, but I believe it could be made more appealing to people new to PBT.

I suggest

  1. Merging fc.assert and fc.property into fc.test.
  2. Allowing primitive constructors as identifiers to built-in arbitraries.
  3. Presenting pre() earlier on in the documentation.

@dubzzz what do you think?


-const fc = require('fast-check');
+const { test } = require('fast-check');
 
 // Code under test
 const contains = (text, pattern) => text.indexOf(pattern) >= 0;
@@ -7,10 +7,10 @@
 describe('properties', () => {
   // string text always contains itself
   it('should always contain itself', () => {
-    fc.assert(fc.property(fc.string(), text => contains(text, text)));
+    test(String, text => contains(text, text));
   });
   // string a + b + c always contains b, whatever the values of a, b and c
   it('should always contain its substrings', () => {
-    fc.assert(fc.property(fc.string(), fc.string(), fc.string(), (a,b,c) => contains(a+b+c, b)));
+    test(String, String, String, (a,b,c) => contains(a+b+c, b));
   });
 });
@dubzzz
Copy link
Owner

dubzzz commented Mar 15, 2019

Hi @gunar,

First of all thanks for the suggestions. Here are my feedbacks concerning those ones:

Merging fc.assert and fc.property into fc.test

I believe the split between assert and property is the common way to go with property based testing.

One of the advantages is that you can easily change fc.assert into:

  • fc.check -> not to throw
  • fc.sample -> to see the generated values
  • fc.statistics -> to analyze the look and feel of the generated values

I think the best thing would be to provide wrapper specific for each framework as I already did for https://github.com/dubzzz/ava-fast-check/ or am trying to do with jestjs/jest#8035

Here are some examples taken from different projects in different programming languages:

  • scalacheck in Scala
  @Test
  def propertyDoNotHoldOnInfiniteStreams() {
    val inputGen = /* ... */
    check(Prop.forAll(inputGen) {gens: (List[Int], Int) => gens match {
      case (l, num) => LineRound.ofInt.merge_tiles(Stream.continually(l.toStream).flatten).take(num).size == num
    }})
}

check would be the equivalent of fc.assert and Prop.forAll the equivalent of fc.property.

  • jsverify in JavaScript
it(name, function () {
  jsc.assert(jsc.forall(...));
}
  • testcheck in JavaScript
check(
  property(
    gen.int, gen.int,
    (a, b) => a + b >= a && a + b >= b
  )
)

Allowing primitive constructors as identifiers to built-in arbitraries

I am not really fond of using primitive constructors as shorthands for arbitraries:

  • String is not shorter than string
  • String: is it supposed to be a fc.string(), fc.unicodeString()...?
  • Number: is it supposed to be an integer, a float, infinity?

Moreover it will highly complexify all the typings and require lots of changes to have it everywhere. Additionally there is no certainty that the users of fast-check building additional wrappers (wrappers similar to array, tuple...) will handle them as arbitraries.

Presenting pre() earlier on in the documentation

Feel free to suggest the associated change in the documentation with a PR. So that we could discuus your suggestion together. The easier the documentation the better.

@dubzzz dubzzz added the question label Apr 4, 2019
@dubzzz
Copy link
Owner

dubzzz commented May 7, 2019

@gunar Any updates? Any feedbacks?

@gunar
Copy link
Contributor Author

gunar commented May 13, 2019

Not really, your answer pretty much nailed it. Thanks @dubzzz.

@gunar gunar closed this as completed May 13, 2019
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

No branches or pull requests

2 participants