Skip to content

Commit

Permalink
Merge pull request #3 from mbarbin/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
mbarbin authored Sep 4, 2024
2 parents 25a3faf + 23e58ee commit 2b54c52
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 23 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
[![CI Status](https://github.com/mbarbin/loc/workflows/ci/badge.svg)](https://github.com/mbarbin/loc/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/mbarbin/loc/badge.svg?branch=main)](https://coveralls.io/github/mbarbin/loc?branch=main)

`Loc.it` representing a range of lexing positions from a parsed file. It may be
used to decorate AST nodes built by parsers so as to allow located error
messages during file processing (compilers, interpreters, etc.)
Loc is an OCaml library to manipulate code locations, which are ranges of lexing positions from a parsed file. It may be used to decorate AST nodes built by parsers so as to allow located error messages during file processing (compilers, interpreters, linters, refactor tools, etc.)

It is inspired by dune's `Loc.t`, and uses it under the hood. The type equality
with `dune` is exposed for a better compatibility of packages that uses `loc`.
It is inspired by dune's `Loc.t`, and uses it under the hood. The type equality with `Stdune.Loc.t` is currently not exposed and `Stdune` not mentioned in the interface of `Loc`, with the aim of keeping the signature of `Loc` stable across potential internal changes in `Stdune`.

## Code documentation

The code documentation of the latest release is built with `odoc` and published
to `GitHub` pages [here](https://mbarbin.github.io/loc).
The code documentation of the latest release is built with `odoc` and published to `GitHub` pages [here](https://mbarbin.github.io/loc).
13 changes: 0 additions & 13 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,8 @@
(depends
(ocaml
(>= 5.2))
(ocamlformat
(and
:with-dev-setup
(= 0.26.2)))
(bisect_ppx
(and
:with-dev-setup
(>= 2.8.3)))
(fpath
(>= 0.7.3))
(ppx_js_style
(and
:with-dev-setup
(>= v0.17)
(< v0.18)))
(sexplib0
(and
(>= v0.17)
Expand Down
3 changes: 0 additions & 3 deletions loc.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ bug-reports: "https://github.com/mbarbin/loc/issues"
depends: [
"dune" {>= "3.16"}
"ocaml" {>= "5.2"}
"ocamlformat" {with-dev-setup & = "0.26.2"}
"bisect_ppx" {with-dev-setup & >= "2.8.3"}
"fpath" {>= "0.7.3"}
"ppx_js_style" {with-dev-setup & >= "v0.17" & < "v0.18"}
"sexplib0" {>= "v0.17" & < "v0.18"}
"stdune" {>= "3.16"}
"odoc" {with-doc}
Expand Down
7 changes: 7 additions & 0 deletions test/test__file_cache.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
let%expect_test "getters" =
let file_cache = Loc.File_cache.create ~path:(Fpath.v "foo.txt") ~file_contents:"" in
print_endline (Loc.File_cache.path file_cache |> Fpath.to_string);
[%expect {| foo.txt |}];
()
;;

let%expect_test "negative" =
let file_cache = Loc.File_cache.create ~path:(Fpath.v "foo.txt") ~file_contents:"" in
require_does_raise [%here] (fun () -> Loc.in_file_line ~file_cache ~line:0);
Expand Down
36 changes: 36 additions & 0 deletions test/test__in_file.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
let loc = Loc.in_file ~path:(Fpath.v "file")

let%expect_test "to_string" =
print_endline (Loc.to_string loc);
[%expect {| File "file", line 1, characters 0-0: |}];
print_endline (Loc.to_file_colon_line loc);
[%expect {| file:1 |}];
()
;;

let%expect_test "path" =
print_endline (Loc.path loc |> Fpath.to_string);
[%expect {| file |}];
()
;;

let%expect_test "start_line" =
print_s [%sexp (Loc.start_line loc : int)];
[%expect {| 1 |}];
()
;;

let%expect_test "is_none" =
print_s [%sexp (Loc.is_none loc : bool)];
[%expect {| false |}];
()
;;

let%expect_test "range" =
print_s [%sexp (Loc.range loc : Loc.Range.t)];
[%expect {|
((start 0)
(stop 0))
|}];
()
;;
Empty file added test/test__in_file.mli
Empty file.
16 changes: 16 additions & 0 deletions test/test__position.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let p1 = [%here]
let p2 = [%here]
let equal_position (a : Lexing.position) (b : Lexing.position) = Stdlib.compare a b = 0

let%expect_test "equal" =
let r1 = Loc.create (p1, p2) in
require [%here] (equal_position (Loc.start r1) p1);
[%expect {||}];
require [%here] (equal_position (Loc.stop r1) p2);
[%expect {||}];
require [%here] (not (equal_position (Loc.start r1) p2));
[%expect {||}];
require [%here] (not (equal_position (Loc.stop r1) p1));
[%expect {||}];
()
;;
Empty file added test/test__position.mli
Empty file.
21 changes: 21 additions & 0 deletions test/test__to_string.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let%expect_test "1 line" =
let loc =
Loc.create
( { Lexing.pos_fname = "file"; pos_lnum = 1; pos_cnum = 2; pos_bol = 0 }
, { Lexing.pos_fname = "file"; pos_lnum = 1; pos_cnum = 10; pos_bol = 0 } )
in
print_endline (Loc.to_string loc);
[%expect {| File "file", line 1, characters 2-10: |}];
()
;;

let%expect_test "multiple lines" =
let loc =
Loc.create
( { Lexing.pos_fname = "file"; pos_lnum = 1; pos_cnum = 2; pos_bol = 0 }
, { Lexing.pos_fname = "file"; pos_lnum = 3; pos_cnum = 35; pos_bol = 30 } )
in
print_endline (Loc.to_string loc);
[%expect {| File "file", lines 1-3, characters 2-35: |}];
()
;;
Empty file added test/test__to_string.mli
Empty file.

0 comments on commit 2b54c52

Please sign in to comment.