Skip to content

Commit

Permalink
Merge pull request #17 from vch9/dev
Browse files Browse the repository at this point in the history
Version 0.3
  • Loading branch information
vch9 authored Oct 13, 2021
2 parents 52aee67 + 783ab2f commit 6d66dd4
Show file tree
Hide file tree
Showing 36 changed files with 1,252 additions and 699 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: opam pin -n .

- name: Deps
run: opam install -d . --deps-only
run: opam install -d . --deps-only --with-test

- name: Build
run: opam exec -- dune build @doc
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ setup.log

# Local OPAM switch
_opam/

# Coverage
_coverage/
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@
- Allow optional printers in a spec
- Improve runner's style with colors
- Main runner function with cli args

## 0.3.0

- Use of data_encoding to store snapshots
- Allow optional path
- Provide result default types for specifications
- Use `git diff` instead of Patdiff
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
all: build

build:
@dune build

test:
@dune runtest

coverage:
@dune runtest --instrument-with bisect_ppx --force
bisect-ppx-report html
firefox _coverage/index.html

clean:
@dune clean
rm -rf _coverage
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,18 @@ In the above, we first provide a naive implementation of the exponentiation.
Then, we create a test with the according specification using `Osnap` combinator.

```
exponentiation 14 4 38416
exponentiation 13 11 1792160394037
exponentiation 11 1 11
exponentiation 12 2 144
exponentiation 2 14 16384
{
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`.
Expand All @@ -75,10 +82,7 @@ let _ = Osnap.Runner.(run_tests ~mode:Interactive [ test ])

Finally, we get the following result:
```
Recap:
1 passed
0 promoted
0 ignored
0 fails
-------------------------------------------------------------------------------
success: ran 1 test (1 passed)
```
`Osnap` has not found any difference between the old and new snapshot :heavy_check_mark:.
Binary file removed examples/.osnap/error_add.osnap
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/.osnap/exponentiation
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ "name": "exponentiation",
"scenarios":
[ [ 0, [ 5, 0 ] ], [ 0, [ 5, 0 ] ], [ 1, [ 0, 1 ] ], [ 4, [ 4, 256 ] ],
[ 0, [ 1, 0 ] ] ] }
Binary file removed examples/.osnap/interactive_add.osnap
Binary file not shown.
Binary file removed examples/.osnap/promote_add.osnap
Binary file not shown.
8 changes: 6 additions & 2 deletions examples/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(executables
(names interactive promote error)
(executable
(name exponentiation)
(libraries osnap))

(rule
(alias runtest)
(action (run %{exe:exponentiation.exe})))
13 changes: 0 additions & 13 deletions examples/error.ml

This file was deleted.

25 changes: 25 additions & 0 deletions examples/exponentiation.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(* let rec exponentiation x n =
* if n = 0 then 1 else if n = 1 then x else exponentiation x (n - 1) * x *)

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 small_int =
Spec.
{
gen = QCheck.Gen.(0 -- 5);
printer = Some string_of_int;
encoding = Some Data_encoding.int31;
}
in
let spec = Spec.(small_int ^> small_int ^>> Result.int) in

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

let _ = Osnap.Runner.(run_tests ~encoding:Data_encoding ~mode:Error [ test ])
13 changes: 0 additions & 13 deletions examples/interactive.ml

This file was deleted.

13 changes: 0 additions & 13 deletions examples/promote.ml

This file was deleted.

9 changes: 5 additions & 4 deletions osnap.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
opam-version: "2.0"
name: "osnap"
version: "0.2.0"
version: "0.3.0"
license: "MIT"
synopsis: "OCaml random snapshot testing"

Expand All @@ -18,11 +18,12 @@ authors: [
depends: [
"dune" {>= "2.8.0" }
"ocaml" {>= "4.12.0" }
"qcheck" {>= "0.17" }
"patdiff" { >= "0.14.0" }
"ppx_deriving" { >= "5.2.1" }
"alcotest" { with-test & >= "1.4.0" }
"data-encoding" { >= "0.4" }
"qcheck-core" { >= "0.17"}
"qcheck-alcotest" { with-test & = "0.17" }
"odoc" { with-doc }
"bisect_ppx" { dev & >= "2.5.0"}
]

build: ["dune" "build" "-p" name "-j" jobs]
Expand Down
75 changes: 37 additions & 38 deletions test/test_diff.ml → src/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,40 @@
(* *)
(*****************************************************************************)

open Osnap__Diff

let eq = Alcotest.of_pp pp

let test_diff_new () =
let prev = None in
let next = "foo" in

let expected = New "foo" in
let actual = diff prev next in

Alcotest.check eq "diff None foo = New foo" expected actual

let test_diff_same () =
let prev = Some "foo" in
let next = "foo" in

let expected = Same in
let actual = diff prev next in

Alcotest.check eq "diff (Some foo) foo = Sucess" expected actual

let test_diff () =
let prev = Some "oof" in
let next = "foo" in

let expected = Diff "foo" in
let actual = diff prev next in

Alcotest.check eq "diff (Some foo) foo = Sucess" expected actual

let tests =
( "Diff",
Alcotest.
[
test_case "test diff new" `Quick test_diff_new;
test_case "test diff same" `Quick test_diff_same;
] )
let dir = ".osnap"

let sep = Filename.dir_sep

let fname name = dir ^ sep ^ name

let opt_path name =
let cwd = Sys.getcwd () in
let lcwd = Str.split (Str.regexp "/_build/default/") cwd in

let name = fname name in
match lcwd with
| [ pwd; path ] -> pwd ^ sep ^ path ^ sep ^ name
| _ -> cwd ^ sep ^ name

let full_path path =
if Filename.is_relative path then Sys.getcwd () ^ sep ^ path else path

let read_file path =
let lines = ref [] in
let ic = open_in path in
try
while true do
lines := (input_line ic ^ "\n") :: !lines
done ;
assert false
with End_of_file ->
close_in ic ;
List.rev !lines |> String.concat ""

let write path s =
let dir = Filename.dirname path in
if (not (Sys.file_exists path)) && not (Sys.file_exists dir) then
Unix.mkdir dir 0o755 ;
let oc = open_out path in
let () = output_string oc s in
close_out oc
25 changes: 14 additions & 11 deletions src/diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ let pp fmt = function
| Diff s -> Format.pp_print_string fmt @@ "Diff " ^ s
| New s -> Format.pp_print_string fmt @@ "New " ^ s

(** [diff prev next] computes diff between [prev] and [next] using Patdiff
If prev is absent, returns [New [next]] *)
let diff prev next =
let config = Patdiff.Configuration.default in
let diff ~path ~prev ~next =
match prev with
| None -> New next
| Some prev -> (
let open Patdiff_kernel.Diff_input in
let prev = { name = "prev"; text = prev } in
let next = { name = "next"; text = next } in

match Patdiff__Compare_core.diff_strings ~prev ~next config with
| `Same -> Same
| `Different s -> Diff s)
let cmd =
Printf.sprintf
"git diff --exit-code --no-index %s %s --output=%s"
prev
next
path
in
match Unix.system cmd with
| WEXITED 0 -> Same
| WEXITED _ ->
let diff = Common.read_file path in
Diff diff
| WSIGNALED _ | WSTOPPED _ -> failwith (cmd ^ " was killed or stopped"))
9 changes: 6 additions & 3 deletions src/diff.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type t =

val pp : Format.formatter -> t -> unit

(** [diff prev next] computes diff between [prev] and [next] using Patdiff
If prev is absent, returns [New [next]] *)
val diff : string option -> string -> t
(** [diff prev next] computes diff between [prev] and [next] using diff
@param where to store the diff's result
@param prev optional path of previous snapshot
@param next path of the next snapshot *)
val diff : path:string -> prev:string option -> next:string -> t
5 changes: 2 additions & 3 deletions src/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(library
(name osnap)
(public_name osnap)
(preprocess
(pps ppx_deriving.show))
(libraries qcheck patdiff ppx_deriving))
(libraries str qcheck-core data-encoding)
(instrumentation (backend bisect_ppx)))
76 changes: 0 additions & 76 deletions src/interpreter.ml

This file was deleted.

Loading

0 comments on commit 6d66dd4

Please sign in to comment.