|
| 1 | +let test_expand_styles () = |
| 2 | + let loc = Ppxlib.Location.none in |
| 3 | + let expr = [%expr "some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()] in |
| 4 | + let attributes = [ (Ppxlib.Labelled "styles", expr) ] in |
| 5 | + let expanded_attributes = Expand_styles_attribute.make ~loc:Ppxlib.Location.none attributes in |
| 6 | + |
| 7 | + List.iter |
| 8 | + (fun attribute -> |
| 9 | + match attribute with |
| 10 | + | ( Ppxlib.Labelled "className", |
| 11 | + [%expr fst ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ())] ) -> |
| 12 | + Alcotest.(check pass) "className uses fst" () () |
| 13 | + | Ppxlib.Labelled "style", [%expr snd ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ())] -> |
| 14 | + Alcotest.(check pass) "style uses snd" () () |
| 15 | + | _ -> Alcotest.fail "Expanded attributes should be className and style") |
| 16 | + expanded_attributes |
| 17 | + |
| 18 | +let test_expand_styles_with_previous_className () = |
| 19 | + let loc = Ppxlib.Location.none in |
| 20 | + let expr = [%expr "some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()] in |
| 21 | + let attributes = [ (Ppxlib.Labelled "className", [%expr "previous-class-name"]); (Ppxlib.Labelled "styles", expr) ] in |
| 22 | + let expanded_attributes = Expand_styles_attribute.make ~loc:Ppxlib.Location.none attributes in |
| 23 | + List.iter |
| 24 | + (fun attribute -> |
| 25 | + match attribute with |
| 26 | + | ( Ppxlib.Labelled "className", |
| 27 | + [%expr |
| 28 | + fst ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()) ^ " " ^ "previous-class-name"] |
| 29 | + ) -> |
| 30 | + Alcotest.(check pass) "className uses previous class name" () () |
| 31 | + | Ppxlib.Labelled "style", [%expr snd ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ())] -> |
| 32 | + Alcotest.(check pass) "style uses combine" () () |
| 33 | + | _ -> Alcotest.fail "Expanded attributes should be className and style") |
| 34 | + expanded_attributes |
| 35 | + |
| 36 | +let test_expand_styles_with_previous_styles () = |
| 37 | + let loc = Ppxlib.Location.none in |
| 38 | + let expr = [%expr "some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()] in |
| 39 | + let attributes = [ (Ppxlib.Labelled "style", [%expr "previous-styles"]); (Ppxlib.Labelled "styles", expr) ] in |
| 40 | + let expanded_attributes = Expand_styles_attribute.make ~loc:Ppxlib.Location.none attributes in |
| 41 | + List.iter |
| 42 | + (fun attribute -> |
| 43 | + match attribute with |
| 44 | + | ( Ppxlib.Labelled "className", |
| 45 | + [%expr fst ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ())] ) -> |
| 46 | + Alcotest.(check pass) "className uses fst" () () |
| 47 | + | ( Ppxlib.Labelled "style", |
| 48 | + [%expr |
| 49 | + ReactDOM.Style.combine "previous-styles" |
| 50 | + (snd ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()))] ) -> |
| 51 | + Alcotest.(check pass) "style uses combine" () () |
| 52 | + | _ -> Alcotest.fail "Expanded attributes should be className and style") |
| 53 | + expanded_attributes |
| 54 | + |
| 55 | +let test_expand_styles_optional () = |
| 56 | + let loc = Ppxlib.Location.none in |
| 57 | + let expr = [%expr Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ())] in |
| 58 | + let attributes = [ (Ppxlib.Optional "styles", expr) ] in |
| 59 | + let expanded_attributes = Expand_styles_attribute.make ~loc:Ppxlib.Location.none attributes in |
| 60 | + List.iter |
| 61 | + (fun attribute -> |
| 62 | + match attribute with |
| 63 | + | ( Ppxlib.Optional "className", |
| 64 | + [%expr |
| 65 | + match Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()) with |
| 66 | + | None -> None |
| 67 | + | Some x -> Some (fst x)] ) -> |
| 68 | + Alcotest.(check pass) "className uses fst" () () |
| 69 | + | ( Ppxlib.Optional "style", |
| 70 | + [%expr |
| 71 | + match Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()) with |
| 72 | + | None -> None |
| 73 | + | Some x -> Some (snd x)] ) -> |
| 74 | + Alcotest.(check pass) "style uses snd" () () |
| 75 | + | _ -> Alcotest.fail "Expanded attributes should be className and style") |
| 76 | + expanded_attributes |
| 77 | + |
| 78 | +let test_expand_styles_optional_with_previous_className () = |
| 79 | + let loc = Ppxlib.Location.none in |
| 80 | + let expr = [%expr Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ())] in |
| 81 | + let attributes = [ (Ppxlib.Labelled "className", [%expr "previous-class-name"]); (Ppxlib.Optional "styles", expr) ] in |
| 82 | + let expanded_attributes = Expand_styles_attribute.make ~loc:Ppxlib.Location.none attributes in |
| 83 | + List.iter |
| 84 | + (fun attribute -> |
| 85 | + match attribute with |
| 86 | + | ( Ppxlib.Labelled "className", |
| 87 | + [%expr |
| 88 | + match |
| 89 | + match Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()) with |
| 90 | + | None -> None |
| 91 | + | Some x -> Some (fst x) |
| 92 | + with |
| 93 | + | None -> "previous-class-name" |
| 94 | + | Some x -> x ^ " " ^ "previous-class-name"] ) -> |
| 95 | + Alcotest.(check pass) "className uses fst" () () |
| 96 | + | ( Ppxlib.Optional "style", |
| 97 | + [%expr |
| 98 | + match Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()) with |
| 99 | + | None -> None |
| 100 | + | Some x -> Some (snd x)] ) -> |
| 101 | + Alcotest.(check pass) "style uses snd" () () |
| 102 | + | _ -> Alcotest.fail "Expanded attributes should be className and style") |
| 103 | + expanded_attributes |
| 104 | + |
| 105 | +let test_expand_styles_optional_with_previous_styles () = |
| 106 | + let loc = Ppxlib.Location.none in |
| 107 | + let expr = [%expr Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ())] in |
| 108 | + let attributes = [ (Ppxlib.Labelled "style", [%expr "previous-styles"]); (Ppxlib.Optional "styles", expr) ] in |
| 109 | + let expanded_attributes = Expand_styles_attribute.make ~loc:Ppxlib.Location.none attributes in |
| 110 | + List.iter |
| 111 | + (fun attribute -> |
| 112 | + match attribute with |
| 113 | + | ( Ppxlib.Optional "className", |
| 114 | + [%expr |
| 115 | + match Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()) with |
| 116 | + | None -> None |
| 117 | + | Some x -> Some (fst x)] ) -> |
| 118 | + Alcotest.(check pass) "className uses fst" () () |
| 119 | + | ( Ppxlib.Labelled "style", |
| 120 | + [%expr |
| 121 | + match |
| 122 | + match Some ("some-class-name", ReactDOM.Style.make ~backgroundColor:"gainsboro" ()) with |
| 123 | + | None -> None |
| 124 | + | Some x -> Some (snd x) |
| 125 | + with |
| 126 | + | None -> "previous-styles" |
| 127 | + | Some x -> ReactDOM.Style.combine "previous-styles" x] ) -> |
| 128 | + Alcotest.(check pass) "style uses snd" () () |
| 129 | + | _ -> Alcotest.fail "Expanded attributes should be className and style") |
| 130 | + expanded_attributes |
| 131 | + |
| 132 | +let test title fn = (title, [ Alcotest.test_case "" `Quick fn ]) |
| 133 | + |
| 134 | +let () = |
| 135 | + Alcotest.run "expand_styles_attribute" |
| 136 | + [ |
| 137 | + test "expand_styles_prop_on_attributes" test_expand_styles; |
| 138 | + test "expand_styles_with_previous_className" test_expand_styles_with_previous_className; |
| 139 | + test "expand_styles_with_previous_styles" test_expand_styles_with_previous_styles; |
| 140 | + test "expand_styles_optional" test_expand_styles_optional; |
| 141 | + test "expand_styles_optional_with_previous_className" test_expand_styles_optional_with_previous_className; |
| 142 | + test "expand_styles_optional_with_previous_styles" test_expand_styles_optional_with_previous_styles; |
| 143 | + ] |
0 commit comments