Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #385: Increase test coverage for Lwt_result #440

Merged
merged 5 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/core/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Test.run "core" [
Test_lwt_list.suite;
Test_lwt_switch.suite;
Test_lwt_mutex.suite;
Test_lwt_result.suite;
]
147 changes: 147 additions & 0 deletions tests/core/test_lwt_result.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
(* OCaml promise library
* http://www.ocsigen.org/lwt
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, with linking exceptions;
* either version 2.1 of the License, or (at your option) any later
* version. See COPYING file for details.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*)

open Test
open Lwt

exception Dummy_error

let suite =
suite "lwt_result" [
test "maps"
(fun () ->
let x = Lwt_result.return 0 in
let correct = Lwt_result.return 1 in
return (Lwt_result.map ((+) 1) x = correct)
);

test ">|= is a variant of map"
(fun () ->
let x = Lwt_result.return 0 in
let correct = Lwt_result.return 1 in
return (Lwt_result.(>|=) x ((+) 1) = correct)
);

test "map, error case"
(fun () ->
let x = Lwt_result.fail 0 in
return (Lwt_result.map ((+) 1) x = x)
);

test "map_err"
(fun () ->
let x = Lwt_result.return 0 in
return (Lwt_result.map_err ((+) 1) x = x)
);

test "map_err, error case"
(fun () ->
let x = Lwt_result.fail 0 in
let correct = Lwt_result.fail 1 in
return (Lwt_result.map_err ((+) 1) x = correct)
);

test "bind"
(fun () ->
let x = Lwt_result.return 0 in
let correct = Lwt_result.return 1 in
let actual = Lwt_result.bind x (fun y -> Lwt_result.return (y + 1)) in
return (actual = correct)
);

test "bind, error case"
(fun () ->
let x = Lwt_result.fail 0 in
let actual = Lwt_result.bind x (fun y -> Lwt_result.return (y + 1)) in
return (actual = x)
);

test "ok"
(fun () ->
let x = Lwt.return 0 in
return (Lwt_result.ok x = Lwt_result.return 0)
);

test "catch"
(fun () ->
let x = Lwt.return 0 in
return (Lwt_result.catch x = Lwt_result.return 0)
);

test "catch, error case"
(fun () ->
let x = Lwt.fail Dummy_error in
return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
);

test "get_exn"
(fun () ->
let x = Lwt_result.return 0 in
return (Lwt_result.get_exn x = Lwt.return 0)
);

test "get_exn, error case"
(fun () ->
let x = Lwt_result.fail Dummy_error in
return (Lwt_result.get_exn x = Lwt.fail Dummy_error)
);

test "bind_lwt"
(fun () ->
let x = Lwt_result.return 0 in
let f y = Lwt.return (y + 1) in
return (Lwt_result.bind_lwt x f = Lwt_result.return 1)
);

test "bind_lwt, error case"
(fun () ->
let x = Lwt_result.fail 0 in
let f y = Lwt.return (y + 1) in
return (Lwt_result.bind_lwt x f = Lwt_result.fail 0)
);

test "bind_lwt_err"
(fun () ->
let x = Lwt_result.return 0 in
let f y = Lwt.return (y + 1) in
return (Lwt_result.bind_lwt_err x f = Lwt_result.return 0)
);

test "bind_lwt_err, error case"
(fun () ->
let x = Lwt_result.fail 0 in
let f y = Lwt.return (y + 1) in
return (Lwt_result.bind_lwt_err x f = Lwt_result.fail 1)
);

test "bind_result"
(fun () ->
let x = Lwt_result.return 0 in
let f y = Result.Ok (y + 1) in
return (Lwt_result.bind_result x f = Lwt_result.return 1)
);

test "bind_result, error case"
(fun () ->
let x = Lwt_result.fail 0 in
let f y = Result.Ok (y + 1) in
return (Lwt_result.bind_result x f = Lwt_result.fail 0)
);
]