package bonsai
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
A library for building dynamic webapps, using Js_of_ocaml
Install
dune-project
Dependency
Authors
Maintainers
Sources
v0.17.0.tar.gz
sha256=c78c4476ee6b856846e2d0941e5965009d5e1b853e564b2b1bee61202f0b1ebb
doc/src/bonsai.test_of_bonsai_itself/test_legacy_bonsai.ml.html
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 590open! 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 clock = Bonsai.Time_source.create ~start:(Time_ns.now ()) in let driver component = Bonsai_test.Arrow.Driver.create component ~initial_input ~clock in f (driver component) ;; module Helpers = struct include Bonsai_test.Arrow.Helpers let make ~driver = Bonsai_test.Arrow.Helpers.make ~driver let make_string ~driver = Bonsai_test.Arrow.Helpers.make_string ~driver let make_string_with_inject ~driver = Bonsai_test.Arrow.Helpers.make_string_with_inject ~driver ;; let make_with_inject ~driver = Bonsai_test.Arrow.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 _ () 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) ~sexp_of_model:[%sexp_of: Counter_component.Model.t] ~equal:[%equal: Int.t] ~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) ~sexp_of_model:[%sexp_of: Counter_component.Model.t] ~equal:[%equal: Counter_component.Model.t] ~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 {| ("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) ~sexp_of_model:[%sexp_of: Counter_component.Model.t] ~default_model:0 ~equal:[%equal: Counter_component.Model.t]) ~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 ~sexp_of_model:[%sexp_of: Counter_component.Model.t] ~sexp_of_action:[%sexp_of: Counter_component.Action.t] ~equal:[%equal: Counter_component.Model.t] dummy_source_code_position ~default_model:0 ~apply_action:(fun (_ : _ Bonsai.Apply_action_context.t) () 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) ~sexp_of_model:[%sexp_of: Counter_component.Model.t] ~default_model:0 ~equal:[%equal: Counter_component.Model.t] 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 } end in let counter_component = Bonsai.Arrow_deprecated.of_module (module Counter_component) ~sexp_of_model:[%sexp_of: Counter_component.Model.t] ~default_model:0 ~equal:[%equal: Counter_component.Model.t] 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 context () () Action.Trigger = Bonsai.Apply_action_context.schedule_event context (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) ~sexp_of_model:[%sexp_of: Raises_something_from_without.Model.t] ~equal:[%equal: Raises_something_from_without.Model.t] ~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 context () () Action.Trigger = Bonsai.Apply_action_context.schedule_event context (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) ~sexp_of_model:[%sexp_of: Raises_something_from_without.Model.t] ~equal:[%equal: Raises_something_from_without.Model.t] ~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 (_ : _ Bonsai.Apply_action_context.t) _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) ~sexp_of_model:[%sexp_of: Words_counter_component.Model.t] ~default_model:0 ~equal:[%equal: Words_counter_component.Model.t] 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"; Bonsai_test.Arrow.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 ~equal = Bonsai.Arrow_deprecated.state_machine ~sexp_of_model:[%sexp_of: M.t] ~equal ~sexp_of_action:[%sexp_of: M.t] [%here] ~default_model:default ~apply_action:(fun (_ : _ Bonsai.Apply_action_context.t) () _model -> Fn.id) >>| Tuple2.map_fst ~f:M.sexp_of_t ;; let%expect_test "normal operation" = let driver = Bonsai_test.Arrow.Driver.create ~initial_input:() ~clock:(Bonsai.Time_source.create ~start:(Time_ns.now ())) (dummy (module Int) ~equal:[%equal: Int.t] ~default:5) in let (module H) = Helpers.make_with_inject ~driver ~sexp_of_result:Fn.id in H.show (); [%expect {| 5 |}] ;; end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>