Skip to content

Commit ab94808

Browse files
committedMar 1, 2025
Update docs
1 parent 4468ff5 commit ab94808

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed
 

‎README.md

+18-28
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ Here is a short example to get you started. It assumes you are using [gleeunit](
2323
```gleam
2424
import qcheck
2525
26-
pub fn small_non_negative_int__test() {
26+
pub fn int_addition_commutativity__test() {
2727
use n <- qcheck.given(qcheck.small_non_negative_int())
28-
n + 1 == 1 + n
28+
should.equal(n + 1, 1 + n)
2929
}
3030
31-
pub fn small_non_negative_int__failures_shrink_to_zero__test() {
31+
pub fn int_addition_commutativity__failures_shrink_to_zero__test() {
3232
use n <- qcheck.given(qcheck.small_non_negative_int())
33-
n + 1 != 1 + n
33+
should.not_equal(n + 1, 1 + n)
3434
}
3535
```
3636

@@ -40,7 +40,11 @@ That second example will fail with an error that may look something like this if
4040
Failures:
4141
4242
1) examples/basic_example_test.small_non_negative_int__failures_shrink_to_zero__test
43-
Failure: <<"TestError[original_value: 3; shrunk_value: 0; shrink_steps: 1; error: property was False;]">>
43+
Failure: <<"TestError[original_value: 5; shrunk_value: 0; shrink_steps: 1; error: Errored(
44+
atom.create_from_string(\"assertNotEqual\")(
45+
[Module(GleeunitFfi), Line(17), Expression([65, 99, 116, 117, 97, 108]), Value(6)]
46+
)
47+
);]">>
4448
stacktrace:
4549
qcheck_ffi.fail
4650
```
@@ -49,16 +53,16 @@ That second example will fail with an error that may look something like this if
4953
- If a property holds for all generated values, then `qcheck.given` returns `Nil`.
5054
- If a property does not hold for all generated values, then `qcheck.given` will panic.
5155
- `qcheck.small_non_negative_int()` generates small integers greater than or equal to zero.
52-
- `n + 1 == 1 + n` is the property being tested in the first test.
56+
- `should.equal(n + 1, 1 + n)` is the property being tested in the first test.
5357
- It should be true for all generated values.
5458
- The return value of `qcheck.given` will be `Nil`, because the property does hold for all generated values.
55-
- `n + 1 != 1 + n` is the property being tested in the second test.
59+
- `should.not_equal(n + 1, 1 + n)` is the property being tested in the second test.
5660
- It should be false for all generated values.
5761
- `qcheck.given` will be panic, because the property does not hold for all generated values.
5862

5963
### In-depth example
6064

61-
Here is a more in-depth example. We will create a simple `Point` type write some serialization functions, and then check that the serializing round-trips.
65+
Here is a more in-depth example. We will create a simple `Point` type, write some serialization functions, and then check that the serializing round-trips.
6266

6367
First here is some code to define a `Point`.
6468

@@ -155,7 +159,7 @@ fn point_generator() {
155159
}
156160
```
157161

158-
Now that we have the point generator, we can write a property test.
162+
Now that we have the point generator, we can write a property test. (It uses the `gleeunit/should.be_true` function again.)
159163

160164
```gleam
161165
pub fn point_serialization_roundtripping__test() {
@@ -166,17 +170,11 @@ pub fn point_serialization_roundtripping__test() {
166170
|> point_to_string
167171
|> point_of_string
168172
169-
point_equal(generated_point, parsed_point)
173+
should.be_true(point_equal(generated_point, parsed_point))
170174
}
171175
```
172176

173-
A couple things to note here.
174-
175-
- `qcheck.given` will "fail" if either the property doesn't hold (e.g., returns `False`) or if there is a panic somewhere in the property function.
176-
- Either, the points are not equal, or, the deserialization returns an `Error` (because we `assert` that it is `Ok`).
177-
- Either one of these cases will trigger shrinking.
178-
179-
Let's try and run the test.
177+
Let's try and run the test. (Note that your output won't look exactly like this.)
180178

181179
```
182180
$ gleam test
@@ -212,7 +210,7 @@ You could imagine combining a property test like the one above, with a few well
212210

213211
(The full code for this example can be found in `test/examples/parsing_example_test.gleam`.)
214212

215-
### Applicative sytle
213+
### Applicative style
216214

217215
The applicative style provides a nice interface for creating generators for custom types.
218216

@@ -244,10 +242,6 @@ fn box_generator() {
244242
}
245243
```
246244

247-
### More examples
248-
249-
The [test](https://github.com/mooreryan/gleam_qcheck/tree/main/test) directory of this repository has many examples of setting up tests, using the built-in generators, and creating new generators. Until more dedicated documentation is written, the tests can provide some good info, as they exercise most of the available behavior. However, be aware that the tests will often use `use <- qcheck.rescue`. This is _not_ needed in your tests--it provides a way to test the `qcheck` internals.
250-
251245
### Integrating with testing frameworks
252246

253247
You don't have to do anything special to integrate `qcheck` with a testing framework like [gleeunit](https://github.com/lpil/gleeunit). The only thing required is that your testing framework of choice be able to handle panics/exceptions.
@@ -256,13 +250,9 @@ _Note: [startest](https://github.com/maxdeviant/startest) should be fine._
256250

257251
You may also be interested in [qcheck_gleeunit_utils](https://github.com/mooreryan/qcheck_gleeunit_utils) for running your tests in parallel and controlling test timeouts when using gleeunit and targeting Erlang.
258252

259-
## Roadmap
260-
261-
While `qcheck` has a lot of features needed to get started with property-based testing, there are still things that could be added or improved. See the `ROADMAP.md` for more information.
262-
263253
## Acknowledgements
264254

265-
Very heavily inspired by the [qcheck](https://github.com/c-cube/qcheck) and [base_quickcheck](https://github.com/janestreet/base_quickcheck) OCaml packages, and of course, the Haskell libraries from which they take inspiration.
255+
Very heavily inspired by the [qcheck](https://github.com/c-cube/qcheck) and [base_quickcheck](https://github.com/janestreet/base_quickcheck) OCaml packages.
266256

267257
## Contributing
268258

@@ -278,6 +268,6 @@ Thank you for your interest in the project!
278268
[![license MIT or Apache
279269
2.0](https://img.shields.io/badge/license-MIT%20or%20Apache%202.0-blue)](https://github.com/mooreryan/gleam_qcheck)
280270

281-
Copyright (c) 2024 Ryan M. Moore
271+
Copyright (c) 2024 - 2025 Ryan M. Moore
282272

283273
Licensed under the Apache License, Version 2.0 or the MIT license, at your option. This program may not be copied, modified, or distributed except according to those terms.
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import gleeunit/should
22
import qcheck
33

4-
pub fn small_positive_or_zero_int__test() {
4+
pub fn int_addition_commutativity__test() {
55
use n <- qcheck.given(qcheck.small_non_negative_int())
66
should.equal(n + 1, 1 + n)
77
}
88
// Uncomment this function when you need to generate the error message for the basic example in the README.
99
//
10-
// pub fn small_positive_or_zero_int__failures_shrink_to_zero__test() {
11-
// use n <- qcheck.given(qcheck.small_positive_or_zero_int())
12-
// n + 1 != 1 + n
10+
// pub fn int_addition_commutativity__failures_shrink_to_zero__test() {
11+
// use n <- qcheck.given(qcheck.small_non_negative_int())
12+
// should.not_equal(n + 1, 1 + n)
1313
// }

0 commit comments

Comments
 (0)
Please sign in to comment.