| 1 |
open Core |
| 2 |
|
| 3 |
let () = Expect_test_helpers_base.sexp_style := To_string_hum |
| 4 |
let pr sexp = Expect_test_helpers_base.print_s ~hide_positions:true sexp |
| 5 |
|
| 6 |
let%expect_test "[%message]" = |
| 7 |
let x = 42 |
| 8 |
and y = "forty-two" in |
| 9 |
pr [%message "foo" 1 2 3 "blah"]; |
| 10 |
[%expect {| (foo 1 2 3 blah) |}]; |
| 11 |
pr [%message "foo" (x : int) (y : string) (x + String.length y : int)]; |
| 12 |
[%expect {| (foo (x 42) (y forty-two) ("x + (String.length y)" 51)) |}]; |
| 13 |
pr [%message "foo" (x : int) (y : string) ~blah:(x + String.length y : int)]; |
| 14 |
[%expect {| (foo (x 42) (y forty-two) (blah 51)) |}]; |
| 15 |
pr [%message "foo" ~_:(x : int) ~_:1 ~blah:(0 : int)]; |
| 16 |
[%expect {| (foo 42 1 (blah 0)) |}]; |
| 17 |
pr [%message "foo" [%here]]; |
| 18 |
[%expect {| (foo ppx/ppx_sexp_message/test/test.ml:LINE:COL) |}]; |
| 19 |
pr [%message "foo" ~loc:[%here]]; |
| 20 |
[%expect {| (foo (loc ppx/ppx_sexp_message/test/test.ml:LINE:COL)) |}]; |
| 21 |
pr [%message "foo" ~_:[%here]]; |
| 22 |
[%expect {| (foo ppx/ppx_sexp_message/test/test.ml:LINE:COL) |}]; |
| 23 |
pr [%message [%here] "blah"]; |
| 24 |
[%expect "(ppx/ppx_sexp_message/test/test.ml:LINE:COL blah)"]; |
| 25 |
pr [%message (sprintf "foo %d" x) (y : string)]; |
| 26 |
[%expect {| ("foo 42" (y forty-two)) |}]; |
| 27 |
pr [%message "hello"]; |
| 28 |
[%expect {| hello |}]; |
| 29 |
pr [%message y y]; |
| 30 |
[%expect {| (forty-two forty-two) |}]; |
| 31 |
pr [%message (sprintf "a") ""]; |
| 32 |
[%expect {| a |}]; |
| 33 |
pr [%message "" (sprintf "%s" "a")]; |
| 34 |
[%expect {| a |}]; |
| 35 |
pr [%message [%here]]; |
| 36 |
[%expect {| ppx/ppx_sexp_message/test/test.ml:LINE:COL |}]; |
| 37 |
pr [%message [%string "foo"]]; |
| 38 |
[%expect {| foo |}]; |
| 39 |
pr [%message (x : int)]; |
| 40 |
[%expect {| (x 42) |}]; |
| 41 |
pr [%message (x : int) (y : string)]; |
| 42 |
[%expect {| ((x 42) (y forty-two)) |}]; |
| 43 |
pr [%message "" ~_:(x : int) (y : string)]; |
| 44 |
[%expect {| (42 (y forty-two)) |}]; |
| 45 |
|
| 46 |
pr [%message "foo" ~a:""]; |
| 47 |
[%expect {| foo |}]; |
| 48 |
pr [%message]; |
| 49 |
[%expect {| () |}]; |
| 50 |
pr [%message (Some 1 : (int option[@sexp.option])) (None : (int option[@sexp.option]))]; |
| 51 |
[%expect {| ("Some 1" 1) |}]; |
| 52 |
pr [%message ([ 1 ] : (int list[@omit_nil])) ([] : (int list[@omit_nil]))]; |
| 53 |
[%expect {| ([1] (1)) |}]; |
| 54 |
pr [%message.omit_nil (Some 1 : int option) (None : int option)]; |
| 55 |
[%expect {| ("Some 1" 1) |}]; |
| 56 |
pr [%message.omit_nil ([ 1 ] : int list) ([] : int list)]; |
| 57 |
[%expect {| ([1] (1)) |}] |
| 58 |
;; |
| 59 |
|
| 60 |
let pr_lazy lazy_sexp = pr (Lazy.force lazy_sexp) |
| 61 |
|
| 62 |
let%expect_test "[%lazy_message]" = |
| 63 |
let x = 42 |
| 64 |
and y = "forty-two" in |
| 65 |
pr_lazy [%lazy_message "foo" 1 2 3 "blah"]; |
| 66 |
[%expect {| (foo 1 2 3 blah) |}]; |
| 67 |
pr_lazy [%lazy_message "foo" (x : int) (y : string) (x + String.length y : int)]; |
| 68 |
[%expect {| (foo (x 42) (y forty-two) ("x + (String.length y)" 51)) |}]; |
| 69 |
pr_lazy [%lazy_message "foo" (x : int) (y : string) ~blah:(x + String.length y : int)]; |
| 70 |
[%expect {| (foo (x 42) (y forty-two) (blah 51)) |}]; |
| 71 |
pr_lazy [%lazy_message "foo" ~_:(x : int) ~_:1 ~blah:(0 : int)]; |
| 72 |
[%expect {| (foo 42 1 (blah 0)) |}]; |
| 73 |
pr_lazy [%lazy_message "foo" [%here]]; |
| 74 |
[%expect {| (foo ppx/ppx_sexp_message/test/test.ml:LINE:COL) |}]; |
| 75 |
pr_lazy [%lazy_message "foo" ~loc:[%here]]; |
| 76 |
[%expect {| (foo (loc ppx/ppx_sexp_message/test/test.ml:LINE:COL)) |}]; |
| 77 |
pr_lazy [%lazy_message "foo" ~_:[%here]]; |
| 78 |
[%expect {| (foo ppx/ppx_sexp_message/test/test.ml:LINE:COL) |}]; |
| 79 |
pr_lazy [%lazy_message [%here] "blah"]; |
| 80 |
[%expect "(ppx/ppx_sexp_message/test/test.ml:LINE:COL blah)"]; |
| 81 |
pr_lazy [%lazy_message (sprintf "foo %d" x) (y : string)]; |
| 82 |
[%expect {| ("foo 42" (y forty-two)) |}]; |
| 83 |
pr_lazy [%lazy_message "hello"]; |
| 84 |
[%expect {| hello |}]; |
| 85 |
pr_lazy [%lazy_message y y]; |
| 86 |
[%expect {| (forty-two forty-two) |}]; |
| 87 |
pr_lazy [%lazy_message (sprintf "a") ""]; |
| 88 |
[%expect {| a |}]; |
| 89 |
pr_lazy [%lazy_message "" (sprintf "%s" "a")]; |
| 90 |
[%expect {| a |}]; |
| 91 |
pr_lazy [%lazy_message [%here]]; |
| 92 |
[%expect {| ppx/ppx_sexp_message/test/test.ml:LINE:COL |}]; |
| 93 |
pr_lazy [%lazy_message [%string "foo"]]; |
| 94 |
[%expect {| foo |}]; |
| 95 |
pr_lazy [%lazy_message (x : int)]; |
| 96 |
[%expect {| (x 42) |}]; |
| 97 |
pr_lazy [%lazy_message (x : int) (y : string)]; |
| 98 |
[%expect {| ((x 42) (y forty-two)) |}]; |
| 99 |
pr_lazy [%lazy_message "" ~_:(x : int) (y : string)]; |
| 100 |
[%expect {| (42 (y forty-two)) |}]; |
| 101 |
|
| 102 |
pr_lazy [%lazy_message "foo" ~a:""]; |
| 103 |
[%expect {| foo |}]; |
| 104 |
pr_lazy [%lazy_message]; |
| 105 |
[%expect {| () |}]; |
| 106 |
pr_lazy |
| 107 |
[%lazy_message |
| 108 |
(Some 1 : (int option[@sexp.option])) (None : (int option[@sexp.option]))]; |
| 109 |
[%expect {| ("Some 1" 1) |}]; |
| 110 |
pr_lazy [%lazy_message ([ 1 ] : (int list[@omit_nil])) ([] : (int list[@omit_nil]))]; |
| 111 |
[%expect {| ([1] (1)) |}]; |
| 112 |
pr_lazy [%lazy_message.omit_nil (Some 1 : int option) (None : int option)]; |
| 113 |
[%expect {| ("Some 1" 1) |}]; |
| 114 |
pr_lazy [%lazy_message.omit_nil ([ 1 ] : int list) ([] : int list)]; |
| 115 |
[%expect {| ([1] (1)) |}] |
| 116 |
;; |
| 117 |
|
| 118 |
let%expect_test "[%message] is not lazy" = |
| 119 |
let side_effect = ref false in |
| 120 |
let _ = [%message (side_effect := true : unit)] in |
| 121 |
Expect_test_helpers_base.require [%here] !side_effect; |
| 122 |
[%expect {| |}] |
| 123 |
;; |
| 124 |
|
| 125 |
let%expect_test "[%message_lazy] is lazy" = |
| 126 |
let side_effect = ref false in |
| 127 |
let lazy_message = [%lazy_message (side_effect := true : unit)] in |
| 128 |
Expect_test_helpers_base.require_equal [%here] (module Bool) !side_effect false; |
| 129 |
[%expect {| |}]; |
| 130 |
let _ = Lazy.force lazy_message in |
| 131 |
Expect_test_helpers_base.require [%here] !side_effect; |
| 132 |
[%expect {| |}] |
| 133 |
;; |