package osnap

All checked builds pass
  1. Overview
  2. Docs
OCaml random snapshot testing

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.3.0.tar.gz
md5=2682db989c9b3143230d58ef027e5bf1
sha512=ffea89af2280470d1a6581e75ea7be7520d4a2726bd07ea00532b82709048df389776d2b3be1f96e3cd11412f81d9ec8d50c6babc5a86355f0cd2a2bae44b642

Description

Added to opam-repository:

README

OSnap

Random snapshot testing library for OCaml.

The Documentation can be found here. This library uses QCheck generators to generate random values for snapshots.

Build

Build from source:

$ git clone git@github.com:vch9/osnap.git
$ dune build

You can also install with opam:

$ opam install osnap

License

The code is now released under the MIT license.

An introduction to the library

The library intends to test differences on results between two versions.

Binary Exponentiation

Let's say we write a function computing the exponentiation.

let rec exponentiation x n =
  if n = 0 then 1
  else if n = 1 then x
  else exponentiation x (n - 1) * x
  
let test =
  let open Osnap in
  let spec = Spec.(small_int ^> small_int ^>> string_of_int) in
  let path = ".osnap/exponentiation" in
  
  Test.make ~spec ~path ~count:5 ~name:"exponentiation" exponentiation
  
let _ =
 Osnap.Runner.(run_tests ~mode:Interactive [ test ])

In the above, we first provide a naive implementation of the exponentiation. Then, we create a test with the according specification using Osnap combinator.

{
  name = exponentiation;
  scenarios = [
	35	66	=	4292184014870020553
	0	3	=	0
	9	3	=	729
	3	7	=	2187
	67	9	=	27206534396294947
  ]
}

Do you want to promote these diff? [Y\n]

As we agree with our function result, we promote the change by typing Y. We try to improve our function efficiency with binary exponentiation.

let rec binary_expo x n =
  if n = 0 then 1
  else if n mod 2 = 0 then
    let tmp = binary_expo x (n / 2) in
    tmp * tmp
  else x * binary_expo x (n - 1)
  
let test =
  let open Osnap in
  let spec = Spec.(small_int ^> small_int ^>> string_of_int) in
  let path = ".osnap/exponentiation" in

  Test.make ~spec ~path ~count:5 ~name:"exponentiation" binary_expo

let _ = Osnap.Runner.(run_tests ~mode:Interactive [ test ])

Finally, we get the following result:

-------------------------------------------------------------------------------
success: ran 1 test (1 passed)

Osnap has not found any difference between the old and new snapshot :heavy_check_mark:.

Dependencies (5)

  1. qcheck-core >= "0.17"
  2. data-encoding >= "0.4"
  3. ppx_deriving >= "5.2.1"
  4. ocaml >= "4.12.0"
  5. dune >= "2.8.0"

Dev Dependencies (3)

  1. bisect_ppx dev & >= "2.5.0"
  2. odoc with-doc
  3. qcheck-alcotest with-test & = "0.17"

Used by

None

Conflicts

None

Build status

Results of building osnap with each compiler on check.ci.ocaml.org, run of against opam-repository 7cdc698. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.3.0 (this version) n/a good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure