Skip to content

Instantly share code, notes, and snippets.

@jobjo
Created June 12, 2021 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jobjo/32f1b7297a330dc16d7a7c9fb15008fe to your computer and use it in GitHub Desktop.
Save jobjo/32f1b7297a330dc16d7a7c9fb15008fe to your computer and use it in GitHub Desktop.
open Popper
open Sample.Syntax
type exp =
| Lit of bool
| And of exp * exp
| Or of exp * exp
| Not of exp
[@@deriving show, ord, popper]
(* A buggy evaluator function *)
let rec eval = function
| Lit b -> b
| And (e1, e2) -> eval e1 || eval e2
| Or (e1, e2) -> eval e1 || eval e2
| Not b -> not @@ eval b
(* A simple unit test *)
let test_hello_world =
test @@ fun () ->
equal Comparator.string "hello world" (String.lowercase_ascii "Hello World")
(* Another unit test *)
let test_lit_true = test @@ fun () -> is_true (eval (Lit true) = true)
(* A property-based test *)
let test_false_ident_or =
test @@ fun () ->
let* e = exp_sample in
is_true (eval e = eval (Or (Lit false, e)))
(* Another property-based test *)
let test_true_ident_and =
test @@ fun () ->
let* e = Sample.with_log "e" pp_exp exp_sample in
is_true ~loc:__LOC__ (eval e = eval (And (Lit true, e)))
(* Bundle some tests together *)
let exp_suite =
suite
[ ("Lit true", test_lit_true)
; ("False ident or", test_false_ident_or)
; ("True ident and", test_true_ident_and)
]
(* Top-level test-suite *)
let suite =
suite [ ("Hello World", test_hello_world); ("Expression", exp_suite) ]
let () = run suite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment