-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add JSON encoding/decoding functions
- Loading branch information
Showing
7 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
(lang dune 2.7) | ||
|
||
(name decimal) | ||
(version v0.1.1) | ||
(version v0.3.0) | ||
(generate_opam_files true) | ||
(license PSF-2.0) | ||
(authors "Yawar Amin <[email protected]>") | ||
|
@@ -15,6 +15,7 @@ | |
the Python decimal module.") | ||
(documentation "https://yawaramin.github.io/ocaml-decimal/api") | ||
(depends | ||
(alcotest (and (>= 1.5.0) (< 2.0.0) :with-test)) | ||
(angstrom (and (>= 0.15.0) (< 1.0.0) :with-test)) | ||
(ocaml (>= 4.08.0)) | ||
(zarith (and (>= 1.10) (< 2.0.0))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(test | ||
(name decimal_test) | ||
(deps (glob_files data/*.decTest)) | ||
(libraries angstrom decimal)) | ||
(libraries alcotest angstrom decimal)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
open Alcotest | ||
|
||
let decimal = (module Decimal : TESTABLE with type t = Decimal.t) | ||
|
||
let test () = | ||
run "Decimal" [ | ||
"of_yojson", [ | ||
test_case "int" `Quick begin fun () -> | ||
`Int 0 | ||
|> Decimal.of_yojson | ||
|> Result.get_ok | ||
|> check decimal "0" Decimal.zero | ||
end; | ||
|
||
test_case "int" `Quick begin fun () -> | ||
`Int ~-1 | ||
|> Decimal.of_yojson | ||
|> Result.get_ok | ||
|> check decimal "-1" (Decimal.of_int ~-1) | ||
end; | ||
|
||
test_case "float" `Quick begin[@alert "-lossy"] fun () -> | ||
`Float 1. | ||
|> Decimal.of_yojson | ||
|> Result.get_ok | ||
|> check decimal "1.0" (Decimal.of_float 1.) | ||
end; | ||
|
||
test_case "float" `Quick begin[@alert "-lossy"] fun () -> | ||
let nan = "NaN" in | ||
`Float Float.nan | ||
|> Decimal.of_yojson | ||
|> Result.get_ok | ||
|> Decimal.to_string | ||
|> check string nan nan | ||
end; | ||
|
||
test_case "string" `Quick begin fun () -> | ||
let pi = "3.14159" in | ||
`String pi | ||
|> Decimal.of_yojson | ||
|> Result.get_ok | ||
|> check decimal pi (Decimal.of_string pi) | ||
end; | ||
|
||
test_case "other" `Quick begin fun () -> | ||
`Null | ||
|> Decimal.of_yojson | ||
|> Result.get_error | ||
|> check string "null" "of_yojson: invalid argument" | ||
end; | ||
]; | ||
|
||
"to_yojson", [ | ||
test_case "pi" `Quick begin fun () -> | ||
let pi = "3.14159" in | ||
match pi |> Decimal.of_string |> Decimal.to_yojson with | ||
| `String s -> check string pi pi s | ||
| _ -> fail "to_yojson must always produce a string" | ||
end; | ||
]; | ||
] |