package bonsai

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file test_legacy_bonsai.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
open! Core
open! Import

(* We need to fake the source-code position because this test is run in
   two files with different names

   In particular, we can't just use [print_s ~hide_positions:true] because that
   only hides line and column numbers, but includes the file name. *)
let dummy_source_code_position =
  Source_code_position.
    { pos_fname = "file_name.ml"; pos_lnum = 0; pos_bol = 0; pos_cnum = 0 }
;;

let run_test ~(component : _ Bonsai.Arrow_deprecated.t) ~initial_input ~f =
  let driver component = Driver.create component ~initial_input ~clock:Incr.clock in
  f (driver component)
;;

module Helpers = struct
  include Helpers

  let make ~driver = Helpers.make ~driver
  let make_string ~driver = Helpers.make_string ~driver
  let make_string_with_inject ~driver = Helpers.make_string_with_inject ~driver
  let make_with_inject ~driver = Helpers.make_with_inject ~driver
end

module Counter_component = struct
  module Input = Unit
  module Model = Int

  module Action = struct
    type t =
      | Increment
      | Decrement
    [@@deriving sexp_of]
  end

  module Result = struct
    type t = string * (Action.t -> unit Effect.t)
  end

  let apply_action ~inject:_ ~schedule_event:_ () model : Action.t -> Model.t = function
    | Increment -> model + 1
    | Decrement -> model - 1
  ;;

  let compute ~inject () m = Int.to_string m, inject
  let name = "counter-component"
end

let%expect_test "enum" =
  let open Bonsai.Arrow_deprecated.Infix in
  let component =
    Bonsai.Arrow_deprecated.enum
      (module Bool)
      ~which:Tuple2.get1
      ~handle:(function
        | true -> Tuple2.get2 @>> Bonsai.Arrow_deprecated.pure ~f:(sprintf "true %d")
        | false -> Tuple2.get2 @>> Bonsai.Arrow_deprecated.pure ~f:(sprintf "false %d"))
  in
  run_test ~component ~initial_input:(true, 5) ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make_string ~driver in
    H.show ();
    [%expect {| true 5 |}];
    H.set_input (true, 10);
    [%expect {| true 10 |}];
    H.set_input (false, 10);
    [%expect {| false 10 |}];
    H.set_input (false, 5);
    [%expect {| false 5 |}])
;;

let%expect_test "enum with action handling `Warn" =
  let open Bonsai.Arrow_deprecated.Infix in
  let module Action = struct
    type t =
      | Outer of Counter_component.Action.t
      | Inner of Counter_component.Action.t
  end
  in
  let component =
    let%map.Bonsai.Arrow_deprecated (result, inject_inner), inject_outer =
      Bonsai.Arrow_deprecated.of_module (module Counter_component) ~default_model:1
      >>> Bonsai.Arrow_deprecated.first
            (Bonsai.Arrow_deprecated.enum
               (module Bool)
               ~which:(fun digit -> Int.of_string digit mod 3 = 0)
               ~handle:(function
                 | false ->
                   Fn.ignore
                   @>> Bonsai.Arrow_deprecated.of_module
                         (module Counter_component)
                         ~default_model:0
                   >>| Tuple2.map_fst ~f:(sprintf "counter %s")
                 | true ->
                   Bonsai.Arrow_deprecated.pure ~f:(fun s ->
                     let view = sprintf "pure %s" s in
                     let inj _ = failwith "can't raise actions out of this one" in
                     view, inj)))
    in
    ( result
    , function
      | Action.Outer a -> inject_outer a
      | Inner a -> inject_inner a )
  in
  run_test ~component ~initial_input:() ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make_string_with_inject ~driver in
    H.show ();
    [%expect "counter 0"];
    H.do_actions [ Inner Increment ];
    [%expect "counter 1"];
    H.do_actions [ Inner Increment ];
    [%expect "counter 2"];
    H.do_actions [ Outer Increment ];
    [%expect "counter 2"];
    H.do_actions
      [ Outer Increment
      (* The inner action is ignored.  You can see this because it prints "counter 2"
         when it gets focus again. *)
      ; Inner Increment
      ];
    [%expect
      {|
               (lib/bonsai/src/proc.ml:84:14
                "An action sent to an [of_module1] has been dropped because its input was not present. This happens when the [of_module1] is inactive when it receives a message."
                (action Increment))
               pure 3|}];
    H.do_actions [ Outer Increment ];
    [%expect "counter 2"])
;;

let%expect_test "constant component" =
  run_test
    ~component:(Bonsai.Arrow_deprecated.const "some constant value")
    ~initial_input:()
    ~f:(fun driver ->
      [%expect {| |}];
      let (module H) = Helpers.make_string ~driver in
      H.show ();
      [%expect {| some constant value |}])
;;

let%expect_test "module component" =
  run_test
    ~component:
      (Bonsai.Arrow_deprecated.of_module (module Counter_component) ~default_model:0)
    ~initial_input:()
    ~f:(fun driver ->
      [%expect {| |}];
      let (module H) = Helpers.make_string_with_inject ~driver in
      H.show ();
      [%expect "0"];
      H.do_actions [ Increment ];
      [%expect "1"];
      H.do_actions [ Decrement ];
      [%expect "0"];
      (* Increment and decrement in the same cycle should cancel out *)
      H.do_actions [ Increment; Decrement ];
      [%expect "0"])
;;

let%expect_test "state-machine counter-component" =
  let component =
    let%map.Bonsai.Arrow_deprecated model, inject =
      Bonsai.Arrow_deprecated.state_machine
        (module Counter_component.Model)
        (module Counter_component.Action)
        dummy_source_code_position
        ~default_model:0
        ~apply_action:(fun ~inject:_ ~schedule_event:_ () model -> function
          | Increment -> model + 1
          | Decrement -> model - 1)
    in
    Int.to_string model, inject
  in
  run_test ~component ~initial_input:() ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make_string_with_inject ~driver in
    H.show ();
    [%expect "0"];
    H.do_actions [ Increment ];
    [%expect "1"];
    H.do_actions [ Decrement ];
    [%expect "0"];
    (* Increment and decrement in the same cycle should cancel out *)
    H.do_actions [ Increment; Decrement ];
    [%expect "0"])
;;

let%expect_test "basic Same_model let syntax" =
  let open Bonsai.Arrow_deprecated.Let_syntax in
  let counter_component =
    Bonsai.Arrow_deprecated.of_module (module Counter_component) ~default_model:0
  in
  let component =
    let%map a_side = Bonsai.Arrow_deprecated.const 5
    and b_side, inject_b = counter_component in
    sprintf "%d | %s" a_side b_side, inject_b
  in
  run_test ~component ~initial_input:() ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make_string_with_inject ~driver in
    H.show ();
    [%expect {| 5 | 0 |}];
    H.do_actions [ Increment ];
    [%expect {| 5 | 1 |}];
    H.do_actions [ Decrement ];
    [%expect {| 5 | 0 |}])
;;

let%expect_test "module project field" =
  let open Bonsai.Arrow_deprecated.Let_syntax in
  let module _ = struct
    type t =
      { a : int
      ; b : int
      }
    [@@deriving fields]
  end
  in
  let counter_component =
    Bonsai.Arrow_deprecated.of_module (module Counter_component) ~default_model:0
  in
  let component =
    let%map a_side, inject_a = counter_component
    and b_side, inject_b = counter_component in
    ( sprintf "%s | %s" a_side b_side
    , function
      | First a -> inject_a a
      | Second b -> inject_b b )
  in
  run_test ~component ~initial_input:() ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make_string_with_inject ~driver in
    H.show ();
    [%expect {| 0 | 0 |}];
    H.do_actions [ First Increment ];
    [%expect {| 1 | 0 |}];
    H.do_actions [ Second Decrement ];
    [%expect {| 1 | -1 |}])
;;

let%expect_test "incremental fn constructor" =
  let component =
    Bonsai.Arrow_deprecated.With_incr.pure
      ~f:
        (Incr_map.mapi ~f:(fun ~key:_ ~data ->
           print_endline "doing math";
           data + 1))
  in
  let initial_input = [ 0, 0; 1, 1; 2, 2 ] |> Int.Map.of_alist_exn in
  run_test ~component ~initial_input ~f:(fun driver ->
    [%expect {|
      doing math
      doing math
      doing math |}];
    let (module H) = Helpers.make ~driver ~sexp_of_result:[%sexp_of: int Int.Map.t] in
    H.show ();
    [%expect {|
      ((0 1)
       (1 2)
       (2 3)) |}];
    H.set_input (Map.add_exn (initial_input : _ Int.Map.t) ~key:3 ~data:3);
    [%expect {|
      doing math
      ((0 1)
       (1 2)
       (2 3)
       (3 4)) |}])
;;

let%expect_test "schedule event from outside of the component" =
  let module Raises_something_from_without = struct
    module Input = Unit
    module Model = Unit

    module Action = struct
      type t = Trigger [@@deriving sexp_of]
    end

    module Result = struct
      type t = unit * (Action.t -> unit Effect.t)
    end

    let apply_action ~inject:_ ~schedule_event () () Action.Trigger =
      schedule_event (Effect.external_ "hello world")
    ;;

    let compute ~inject () () = (), inject
    let name = "raises-something-from-without"
  end
  in
  let component =
    Bonsai.Arrow_deprecated.of_module
      (module Raises_something_from_without)
      ~default_model:()
  in
  run_test ~component ~initial_input:() ~f:(fun driver ->
    [%expect {| |}];
    [%expect {||}];
    let (module H) = Helpers.make_with_inject ~driver ~sexp_of_result:[%sexp_of: unit] in
    H.do_actions [ Trigger ];
    [%expect {|
      External event: hello world
      () |}])
;;

let%expect_test "schedule many events from outside of the component" =
  let module Raises_something_from_without = struct
    module Input = Unit

    module Action = struct
      type t = Trigger [@@deriving sexp_of]
    end

    module Model = Unit

    module Result = struct
      type t = unit * (Action.t -> unit Effect.t)
    end

    let apply_action ~inject:_ ~schedule_event () () Action.Trigger =
      schedule_event
        (Effect.sequence
           [ Effect.external_ "hello world"
           ; Effect.no_op
           ; Effect.external_ "goodbye world"
           ])
    ;;

    let compute ~inject () () = (), inject
    let name = "raises-something-from-without"
  end
  in
  let component =
    Bonsai.Arrow_deprecated.of_module
      (module Raises_something_from_without)
      ~default_model:()
  in
  run_test ~component ~initial_input:() ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make_with_inject ~driver ~sexp_of_result:[%sexp_of: unit] in
    H.do_actions [ Trigger ];
    [%expect
      {|
      External event: hello world
      External event: goodbye world
      () |}])
;;

let%expect_test "value cutoff" =
  let open Bonsai.Arrow_deprecated.Infix in
  let cutoff =
    Incr.Cutoff.create (fun ~old_value ~new_value -> old_value % 2 = new_value % 2)
  in
  let component =
    Bonsai.Arrow_deprecated.With_incr.value_cutoff ~cutoff
    >>> Bonsai.Arrow_deprecated.pure ~f:Int.to_string
  in
  run_test ~component ~initial_input:1 ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make_string ~driver in
    H.show ();
    [%expect "1"];
    H.set_input 5;
    [%expect "1"];
    H.set_input 6;
    [%expect "6"];
    H.set_input 2;
    [%expect "6"])
;;

let%expect_test "input" =
  let module Words_counter_component = struct
    module Input = struct
      type t = string list
    end

    module Model = Int

    module Action = struct
      type t = Increment [@@deriving sexp_of]
    end

    module Result = struct
      type t = (int * string list) * (Action.t -> unit Effect.t)
    end

    let apply_action ~inject:_ ~schedule_event:_ _words model : Action.t -> Model.t
      = function
        | Increment -> model + 1
    ;;

    let compute ~inject words m =
      let res = m, List.filter words ~f:(fun s -> String.length s = m) in
      res, inject
    ;;

    let name = "words-counter-component"
  end
  in
  let component =
    Bonsai.Arrow_deprecated.of_module (module Words_counter_component) ~default_model:0
  in
  let initial_input = [] in
  run_test ~component ~initial_input ~f:(fun driver ->
    [%expect {| |}];
    let (module H) =
      Helpers.make_with_inject ~driver ~sexp_of_result:[%sexp_of: int * string list]
    in
    H.show ();
    [%expect {| (0 ()) |}];
    H.set_input [ "a"; "b"; "c"; "aa"; "bbb"; "cccc" ];
    [%expect {| (0 ()) |}];
    H.do_actions [ Words_counter_component.Action.Increment ];
    [%expect {| (1 (a b c)) |}];
    H.do_actions [ Words_counter_component.Action.Increment ];
    [%expect {| (2 (aa)) |}];
    H.do_actions [ Words_counter_component.Action.Increment ];
    [%expect {| (3 (bbb)) |}];
    H.do_actions [ Words_counter_component.Action.Increment ];
    [%expect {| (4 (cccc)) |}];
    H.set_input [ "aaaa"; "bbbb" ];
    [%expect {| (4 (aaaa bbbb)) |}])
;;

let%expect_test "compose, pure" =
  let open Bonsai.Arrow_deprecated.Infix in
  let component_a = Bonsai.Arrow_deprecated.pure ~f:(fun model -> model mod 5) in
  let component_b = Bonsai.Arrow_deprecated.pure ~f:(fun input -> input + 2) in
  let component = component_a >>> component_b in
  run_test ~component ~initial_input:0 ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make ~driver ~sexp_of_result:[%sexp_of: int] in
    H.show ();
    [%expect "2"];
    H.set_input 11;
    [%expect "3"])
;;

let%expect_test "pure_incr" =
  let open Bonsai.Arrow_deprecated.Infix in
  let component_a = Bonsai.Arrow_deprecated.pure ~f:(fun model -> model mod 5) in
  let component_b =
    Bonsai.Arrow_deprecated.With_incr.pure ~f:(fun input ->
      Incr.map input ~f:(fun i -> i + 2))
  in
  let component = component_a >>> component_b in
  run_test ~component ~initial_input:0 ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make ~driver ~sexp_of_result:[%sexp_of: int] in
    H.show ();
    [%expect "2"];
    H.set_input 11;
    [%expect "3"])
;;

let%expect_test "input projection" =
  let open Bonsai.Arrow_deprecated.Infix in
  let component =
    String.length @>> Bonsai.Arrow_deprecated.pure ~f:(fun input -> input + 1)
  in
  run_test ~component ~initial_input:"hi" ~f:(fun driver ->
    [%expect {| |}];
    let (module H) = Helpers.make ~driver ~sexp_of_result:[%sexp_of: int] in
    H.show ();
    [%expect "3"];
    H.set_input "hello";
    [%expect "6"])
;;

let%expect_test "assoc on input" =
  let component =
    Bonsai.Arrow_deprecated.pure ~f:(fun x -> x + 1)
    |> Bonsai.Arrow_deprecated.Map.assoc_input (module String)
  in
  run_test
    ~component
    ~initial_input:(String.Map.of_alist_exn [ "a", 0; "b", 2 ])
    ~f:(fun driver ->
      let (module H) =
        Helpers.make ~driver ~sexp_of_result:[%sexp_of: int String.Map.t]
      in
      H.show ();
      [%expect {|
        ((a 1)
         (b 3)) |}];
      H.set_input (String.Map.of_alist_exn [ "a", 1; "b", 2 ]);
      [%expect {|
        ((a 2)
         (b 3)) |}])
;;

let%expect_test "Incremental.of_incr" =
  let var = Incr.Var.create "hello" in
  let incr = Incr.Var.watch var in
  let component = Bonsai.Arrow_deprecated.With_incr.of_incr incr in
  run_test
    ~component
    ~initial_input:(String.Map.of_alist_exn [ "a", 0; "b", 2 ])
    ~f:(fun driver ->
      [%expect {| |}];
      let (module H) = Helpers.make_string ~driver in
      H.show ();
      [%expect {| hello |}];
      Incr.Var.set var "world";
      Driver.flush driver;
      H.show ();
      [%expect {| world |}];
      (* reset for next test *)
      Incr.Var.set var "hello")
;;

module _ = struct
  open Bonsai.Arrow_deprecated.Let_syntax

  let dummy (type t) (module M : Bonsai.Arrow_deprecated.Model with type t = t) ~default =
    Bonsai.Arrow_deprecated.state_machine
      (module M)
      (module M)
      [%here]
      ~default_model:default
      ~apply_action:(fun ~inject:_ ~schedule_event:_ () _model -> Fn.id)
    >>| Tuple2.map_fst ~f:M.sexp_of_t
  ;;

  let%expect_test "normal operation" =
    let driver =
      Driver.create ~initial_input:() ~clock:Incr.clock (dummy (module Int) ~default:5)
    in
    let (module H) = Helpers.make_with_inject ~driver ~sexp_of_result:Fn.id in
    H.show ();
    [%expect {| 5 |}]
  ;;

  let%expect_test "with of_sexp" =
    let driver =
      Driver.create
        ~initial_model_sexp:[%sexp 2]
        ~initial_input:()
        ~clock:Incr.clock
        (dummy (module Int) ~default:5)
    in
    let (module H) = Helpers.make_with_inject ~driver ~sexp_of_result:Fn.id in
    H.show ();
    [%expect {| 2 |}]
  ;;

  let%expect_test "multiple components" =
    let component =
      let%map (a, _), (b, _) =
        Bonsai.Arrow_deprecated.both
          (dummy (module Int) ~default:5)
          (dummy (module Int) ~default:5)
      in
      Sexp.List [ a; b ], Nothing.unreachable_code
    in
    let driver =
      Driver.create
        ~initial_model_sexp:[%sexp [ "2"; "3" ]]
        ~initial_input:()
        ~clock:Incr.clock
        component
    in
    let (module H) = Helpers.make_with_inject ~driver ~sexp_of_result:Fn.id in
    H.show ();
    [%expect {| (2 3) |}]
  ;;

  let%expect_test "enum" =
    let module Action = struct
      type t =
        | Outer of bool
        | Inner of string
    end
    in
    let component =
      let%map.Bonsai.Arrow_deprecated (inner, change_inner), change_outer =
        dummy (module Bool) ~default:true
        >>> Bonsai.Arrow_deprecated.first
              (Bonsai.Arrow_deprecated.if_
                 [%of_sexp: bool]
                 ~then_:
                   (Fn.ignore @>> dummy (module Int) ~default:0
                    >>| Tuple2.map_snd ~f:(fun inject s -> s |> int_of_string |> inject))
                 ~else_:(Fn.ignore @>> dummy (module String) ~default:"world"))
      in
      let inject = function
        | Action.Outer b -> change_outer b
        | Inner s -> change_inner s
      in
      inner, inject
    in
    let driver = Driver.create ~initial_input:() ~clock:Incr.clock component in
    let (module H) = Helpers.make_with_inject ~driver ~sexp_of_result:Fn.id in
    H.show ();
    [%expect {| 0 |}];
    H.do_actions [ Action.Inner "23" ];
    [%expect {| 23 |}];
    H.do_actions [ Action.Outer false ];
    [%expect {| world |}];
    H.do_actions [ Action.Inner "bonsai!" ];
    [%expect {| bonsai! |}];
    let initial_model_sexp = Driver.sexp_of_model driver in
    print_s initial_model_sexp;
    [%expect {|
      (false (
        (0 23)
        (1 bonsai!))) |}];
    let driver =
      Driver.create ~initial_model_sexp ~initial_input:() ~clock:Incr.clock component
    in
    let (module H) = Helpers.make_with_inject ~driver ~sexp_of_result:Fn.id in
    H.show ();
    [%expect {| bonsai! |}];
    H.do_actions [ Action.Outer true ];
    [%expect {| 23 |}]
  ;;
end
OCaml

Innovation. Community. Security.