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

Add Component.identity #12

Merged
merged 2 commits into from
May 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Unreleased
--------------

- archi: add `Component.identity`which creates a component whose value is its
argument ([#11](https://github.com/anmonteiro/archi/pull/11))

0.1.0 2020-05-07
--------------

Expand Down
6 changes: 6 additions & 0 deletions lib/archi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ module Make (Io : IO) = struct
fun ~start ~stop ->
Component { start; stop; hkey = Hmap.Key.create (); dependencies = [] }

let identity : type ctx ty. ty -> (ctx, ty) t =
fun c ->
let start _ctx = Io.Result.return c in
let stop _c = Io.return () in
make ~start ~stop

let make_m
: type ctx a.
(module SIMPLE_COMPONENT with type t = a and type ctx = ctx)
Expand Down
2 changes: 2 additions & 0 deletions lib/archi_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ module type S = sig

(** Creating components *)

val identity : 'ty -> ('ctx, 'ty) t

val make
: start:('ctx -> ('a, string) result Io.t)
-> stop:('a -> unit Io.t)
Expand Down
2 changes: 1 addition & 1 deletion nix/sources.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
let
overlays =
builtins.fetchTarball
https://github.com/anmonteiro/nix-overlays/archive/3ce96cd.tar.gz;
https://github.com/anmonteiro/nix-overlays/archive/0e67e7b.tar.gz;

in

Expand Down
34 changes: 34 additions & 0 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,39 @@ let test_get () =
| Error error ->
Alcotest.fail error

let test_identity () =
let identity_component = Component.identity "I'm the component" in
let system =
System.make_reusable
~lift:(fun id -> id)
[ "identity component", identity_component ]
in
let started = System.start () system in
(match started with
| Ok system ->
let id = System.get system in
Alcotest.(check string "expected value" "I'm the component" id)
| Error error ->
Alcotest.fail error);
let v = ref "" in
let server =
Component.using
~dependencies:[ identity_component ]
~start:(fun () id ->
v := id;
Ok ())
~stop:(fun _ -> ())
in
let system =
System.make [ "identity component", identity_component; "server", server ]
in
let started = System.start () system in
match started with
| Ok _system ->
Alcotest.(check string "expected value" "I'm the component" !v)
| Error error ->
Alcotest.fail error

let suite =
[ "start / stop order", `Quick, test_start_stop_order system
; ( "start / stop order, module system"
Expand All @@ -198,6 +231,7 @@ let suite =
; "duplicates", `Quick, test_duplicates_start_once
; "system depending on system", `Quick, test_start_stop_order_system_deps
; "get resulting value", `Quick, test_get
; "test identity component", `Quick, test_identity
]

let () = Alcotest.run "archi unit tests" [ "archi", suite ]