Skip to content

Commit

Permalink
Merge pull request #297 from jmid/fix-qcheck-print-escaping
Browse files Browse the repository at this point in the history
Quote and escape in QCheck.Print.{char,string}, aligning it with QCheck2.Print
  • Loading branch information
jmid authored Dec 12, 2024
2 parents ce4a500 + 75f1061 commit 6af3dc4
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 67 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## NEXT RELEASE

- Quote and escape in `Print.string` and `Print.char` in the `QCheck` module,
mirroring the `QCheck2.Print` module's behaviour. Also quote and
escape `Print.bytes` in both `QCheck` and `QCheck2`.
- Clean-up `QCheck` and `QCheck2` documentation pages
- Add `exponential` generator to `QCheck`, `QCheck.Gen`, and `QCheck2.Gen`
- Add `Shrink.bool` and use it in `QCheck.bool`
Expand Down
2 changes: 1 addition & 1 deletion example/QCheck_runner_test.expected.ocaml4.32
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Test FAIL_pred_map_commute failed (48 shrink steps):

Test FAIL_fun2_pred_strings failed (2 shrink steps):

{some random string -> true; _ -> false}
{"some random string" -> true; _ -> false}

--- Failure --------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion example/QCheck_runner_test.expected.ocaml4.64
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Test FAIL_pred_map_commute failed (77 shrink steps):

Test FAIL_fun2_pred_strings failed (2 shrink steps):

{some random string -> true; _ -> false}
{"some random string" -> true; _ -> false}

--- Failure --------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion example/QCheck_runner_test.expected.ocaml5.32
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Test FAIL_pred_map_commute failed (47 shrink steps):

Test FAIL_fun2_pred_strings failed (2 shrink steps):

{some random string -> true; _ -> false}
{"some random string" -> true; _ -> false}

--- Failure --------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion example/QCheck_runner_test.expected.ocaml5.64
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Test FAIL_pred_map_commute failed (89 shrink steps):

Test FAIL_fun2_pred_strings failed (2 shrink steps):

{some random string -> true; _ -> false}
{"some random string" -> true; _ -> false}

--- Failure --------------------------------------------------------------------

Expand Down
45 changes: 22 additions & 23 deletions src/core/QCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ all rights reserved.
(** {1 Quickcheck inspired property-based testing} *)

let poly_compare=compare
open Printf

module RS = struct
(* Poor man's splitter for version < 5.0 *)
Expand Down Expand Up @@ -480,9 +479,9 @@ module Print = struct
let int = string_of_int
let bool = string_of_bool
let float = string_of_float
let bytes = Bytes.to_string
let string s = s
let char c = String.make 1 c
let string s = Printf.sprintf "%S" s
let bytes b = string (Bytes.to_string b)
let char c = Printf.sprintf "%C" c

let option f = function
| None -> "None"
Expand Down Expand Up @@ -1097,17 +1096,17 @@ let unit : unit arbitrary =
make ~small:small1 ~shrink:Shrink.nil ~print:Print.unit Gen.unit
let bool =
make ~small:small1 ~shrink:Shrink.bool ~print:Print.bool Gen.bool
let float = make_scalar ~print:string_of_float Gen.float
let pos_float = make_scalar ~print:string_of_float Gen.pfloat
let neg_float = make_scalar ~print:string_of_float Gen.nfloat
let float = make_scalar ~print:Print.float Gen.float
let pos_float = make_scalar ~print:Print.float Gen.pfloat
let neg_float = make_scalar ~print:Print.float Gen.nfloat

let float_bound_inclusive bound =
make_scalar ~print:string_of_float (Gen.float_bound_inclusive bound)
make_scalar ~print:Print.float (Gen.float_bound_inclusive bound)

let float_bound_exclusive bound =
make_scalar ~print:string_of_float (Gen.float_bound_exclusive bound)
make_scalar ~print:Print.float (Gen.float_bound_exclusive bound)

let float_range low high = make_scalar ~print:string_of_float (Gen.float_range low high)
let float_range low high = make_scalar ~print:Print.float (Gen.float_range low high)

let exponential mean = make_scalar ~print:Print.float (Gen.exponential mean)

Expand All @@ -1132,33 +1131,33 @@ let int64 =
let small_char target c = abs ((Char.code c) - (Char.code target))

let char =
make ~print:(sprintf "%C") ~small:(small_char 'a') ~shrink:Shrink.char Gen.char
make ~print:Print.char ~small:(small_char 'a') ~shrink:Shrink.char Gen.char
let printable_char =
make ~print:(sprintf "%C") ~small:(small_char 'a') ~shrink:Shrink.char_printable Gen.printable
make ~print:Print.char ~small:(small_char 'a') ~shrink:Shrink.char_printable Gen.printable
let numeral_char =
make ~print:(sprintf "%C") ~small:(small_char '0') ~shrink:Shrink.char_numeral Gen.numeral
make ~print:Print.char ~small:(small_char '0') ~shrink:Shrink.char_numeral Gen.numeral

let bytes_gen_of_size size gen =
make ~shrink:Shrink.bytes ~small:Bytes.length
~print:(Print.bytes) (Gen.bytes_size ~gen size)
~print:Print.bytes (Gen.bytes_size ~gen size)
let bytes_of gen =
make ~shrink:Shrink.bytes ~small:Bytes.length
~print:(Print.bytes) (Gen.bytes ~gen)
~print:Print.bytes (Gen.bytes ~gen)

let bytes = bytes_of Gen.char
let bytes_of_size size = bytes_gen_of_size size Gen.char
let bytes_small = bytes_gen_of_size Gen.small_nat Gen.char
let bytes_small_of gen = bytes_gen_of_size Gen.small_nat gen
let bytes_printable =
make ~shrink:(Shrink.bytes ~shrink:Shrink.char_printable) ~small:Bytes.length
~print:(Print.bytes) (Gen.bytes ~gen:Gen.printable)
~print:Print.bytes (Gen.bytes ~gen:Gen.printable)

let string_gen_of_size size gen =
make ~shrink:Shrink.string ~small:String.length
~print:(sprintf "%S") (Gen.string_size ~gen size)
~print:Print.string (Gen.string_size ~gen size)
let string_of gen =
make ~shrink:Shrink.string ~small:String.length
~print:(sprintf "%S") (Gen.string ~gen)
~print:Print.string (Gen.string ~gen)

let string = string_of Gen.char
let string_of_size size = string_gen_of_size size Gen.char
Expand All @@ -1169,23 +1168,23 @@ let string_gen = string_of

let printable_string =
make ~shrink:(Shrink.string ~shrink:Shrink.char_printable) ~small:String.length
~print:(sprintf "%S") (Gen.string ~gen:Gen.printable)
~print:Print.string (Gen.string ~gen:Gen.printable)

let printable_string_of_size size =
make ~shrink:(Shrink.string ~shrink:Shrink.char_printable) ~small:String.length
~print:(sprintf "%S") (Gen.string_size ~gen:Gen.printable size)
~print:Print.string (Gen.string_size ~gen:Gen.printable size)

let small_printable_string =
make ~shrink:(Shrink.string ~shrink:Shrink.char_printable) ~small:String.length
~print:(sprintf "%S") (Gen.string_size ~gen:Gen.printable Gen.small_nat)
~print:Print.string (Gen.string_size ~gen:Gen.printable Gen.small_nat)

let numeral_string =
make ~shrink:(Shrink.string ~shrink:Shrink.char_numeral) ~small:String.length
~print:(sprintf "%S") (Gen.string ~gen:Gen.numeral)
~print:Print.string (Gen.string ~gen:Gen.numeral)

let numeral_string_of_size size =
make ~shrink:(Shrink.string ~shrink:Shrink.char_numeral) ~small:String.length
~print:(sprintf "%S") (Gen.string_size ~gen:Gen.numeral size)
~print:Print.string (Gen.string_size ~gen:Gen.numeral size)

let string_printable = printable_string
let string_printable_of_size = printable_string_of_size
Expand Down
4 changes: 2 additions & 2 deletions src/core/QCheck2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,10 @@ module Print = struct

let float = string_of_float

let bytes = Bytes.to_string

let string s = Printf.sprintf "%S" s

let bytes b = string (Bytes.to_string b)

let char c = Printf.sprintf "%C" c

let option f = function
Expand Down
8 changes: 4 additions & 4 deletions test/core/QCheck2_expect_test.expected.ocaml4.32
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,25 @@ Test printable never produces less than '5 failed (1 shrink steps):

Test bytes are empty failed (8 shrink steps):

a
"a"

--- Failure --------------------------------------------------------------------

Test bytes never has a \000 char failed (22 shrink steps):

aaaaaaaaaaaaaaaaaaaaaa
"aaaaaa\000aaaaaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Test bytes never has a \255 char failed (59 shrink steps):

aaaaaaaaaaaaaaaaaaaaaaaaaa�aaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaaaaaaaaaaaaaaaaaaaaa\255aaaaaaaaaaaaaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Test bytes have unique chars failed (18 shrink steps):

aaaaaaaaaaaaa
"aaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Expand Down
8 changes: 4 additions & 4 deletions test/core/QCheck2_expect_test.expected.ocaml4.64
Original file line number Diff line number Diff line change
Expand Up @@ -348,25 +348,25 @@ Test printable never produces less than '5 failed (1 shrink steps):

Test bytes are empty failed (8 shrink steps):

a
"a"

--- Failure --------------------------------------------------------------------

Test bytes never has a \000 char failed (22 shrink steps):

aaaaaaaaaaaaaaaaaaaaaa
"aaaaaa\000aaaaaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Test bytes never has a \255 char failed (59 shrink steps):

aaaaaaaaaaaaaaaaaaaaaaaaaa�aaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaaaaaaaaaaaaaaaaaaaaa\255aaaaaaaaaaaaaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Test bytes have unique chars failed (18 shrink steps):

aaaaaaaaaaaaa
"aaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Expand Down
8 changes: 4 additions & 4 deletions test/core/QCheck2_expect_test.expected.ocaml5.32
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,25 @@ Test printable never produces less than '5 failed (0 shrink steps):

Test bytes are empty failed (9 shrink steps):

a
"a"

--- Failure --------------------------------------------------------------------

Test bytes never has a \000 char failed (55 shrink steps):

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaaaaaaaaaa\000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Test bytes never has a \255 char failed (75 shrink steps):

aaaaaaa�aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaa\255aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Test bytes have unique chars failed (14 shrink steps):

aaaaaaa
"aaaaaaa"

--- Failure --------------------------------------------------------------------

Expand Down
8 changes: 4 additions & 4 deletions test/core/QCheck2_expect_test.expected.ocaml5.64
Original file line number Diff line number Diff line change
Expand Up @@ -340,25 +340,25 @@ Test printable never produces less than '5 failed (0 shrink steps):

Test bytes are empty failed (9 shrink steps):

a
"a"

--- Failure --------------------------------------------------------------------

Test bytes never has a \000 char failed (55 shrink steps):

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaaaaaaaaaa\000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Test bytes never has a \255 char failed (75 shrink steps):

aaaaaaa�aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"aaaaaaa\255aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

--- Failure --------------------------------------------------------------------

Test bytes have unique chars failed (14 shrink steps):

aaaaaaa
"aaaaaaa"

--- Failure --------------------------------------------------------------------

Expand Down
10 changes: 5 additions & 5 deletions test/core/QCheck_expect_test.expected.ocaml4.32
Original file line number Diff line number Diff line change
Expand Up @@ -253,25 +253,25 @@ Test printable never produces less than '5 failed (3 shrink steps):

Test bytes are empty failed (15 shrink steps):

a
"a"

--- Failure --------------------------------------------------------------------

Test bytes never has a \000 char failed (8 shrink steps):

"\000"

--- Failure --------------------------------------------------------------------

Test bytes never has a \255 char failed (14 shrink steps):

"\255"

--- Failure --------------------------------------------------------------------

Test bytes have unique chars failed (13 shrink steps):

��
"\129\129"

--- Failure --------------------------------------------------------------------

Expand Down Expand Up @@ -511,7 +511,7 @@ Test fail_pred_map_commute failed (48 shrink steps):

Test fail_pred_strings failed (2 shrink steps):

{some random string -> true; _ -> false}
{"some random string" -> true; _ -> false}

--- Failure --------------------------------------------------------------------

Expand Down
10 changes: 5 additions & 5 deletions test/core/QCheck_expect_test.expected.ocaml4.64
Original file line number Diff line number Diff line change
Expand Up @@ -285,25 +285,25 @@ Test printable never produces less than '5 failed (3 shrink steps):

Test bytes are empty failed (15 shrink steps):

a
"a"

--- Failure --------------------------------------------------------------------

Test bytes never has a \000 char failed (8 shrink steps):

"\000"

--- Failure --------------------------------------------------------------------

Test bytes never has a \255 char failed (14 shrink steps):

"\255"

--- Failure --------------------------------------------------------------------

Test bytes have unique chars failed (13 shrink steps):

��
"\129\129"

--- Failure --------------------------------------------------------------------

Expand Down Expand Up @@ -543,7 +543,7 @@ Test fail_pred_map_commute failed (77 shrink steps):

Test fail_pred_strings failed (2 shrink steps):

{some random string -> true; _ -> false}
{"some random string" -> true; _ -> false}

--- Failure --------------------------------------------------------------------

Expand Down
Loading

0 comments on commit 6af3dc4

Please sign in to comment.