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
|
(* Js_of_ocaml tests
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2019 Ty Overby
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)
open Util
let%expect_test _ =
let program =
compile_and_parse
~use_js_string:true
{|
type js_string
external js_string : string -> js_string = "caml_jsstring_of_string"
type r = {x: int; y: string; z : string}
let ex = {x = 5; y = "hello"; z = "the • and › characters"} ;;
let sx = js_string "hello2", js_string "the • and › characters (2)"
let ax = [|1;2;3;4|] ;;
let bx = [|1.0;2.0;3.0;4.0|] ;;
let cx = [|0./.0.;-0./.0.;1./.0.;-1./.0.;0.;-0.|] ;;
let (>>=) a b = a * b
let (>>|) a b = a + b
let (>>?=) a b = a / b
let symbol_op = (>>=), (>>|), (>>?=);;
|}
in
print_var_decl program "ex";
print_var_decl program "sx";
print_var_decl program "ax";
print_var_decl program "bx";
print_var_decl program "cx";
print_var_decl program "symbol_op";
[%expect
{|
var ex = [0, 5, "hello", "the \xe2\x80\xa2 and \xe2\x80\xba characters"];
//end
var sx = [0, "hello2", "the • and › characters (2)"];
//end
var ax = [0, 1, 2, 3, 4];
//end
var bx = [254, 1., 2., 3., 4.];
//end
var cx = [254, NaN, NaN, Infinity, - Infinity, 0., - 0.];
//end
var symbol_op = [0, symbol_bind, symbol_map, symbol];
//end |}]
let%expect_test _ =
let program =
compile_and_parse
~use_js_string:false
{|
type js_string
external js_string : string -> js_string = "caml_jsstring_of_string"
type r = {x: int; y: string; z : string}
let ex = {x = 5; y = "hello"; z = "the • and › characters"} ;;
let sx = "hello2", js_string "the • and › characters (2)"
let ax = [|1;2;3;4|] ;;
let bx = [|1.0;2.0;3.0;4.0|] ;;
let cx = [|0./.0.;-0./.0.;1./.0.;-1./.0.;0.;-0.|] ;;
let (>>=) a b = a * b
let (>>|) a b = a + b
let (>>?=) a b = a / b
let symbol_op = (>>=), (>>|), (>>?=);;
|}
in
print_var_decl program "ex";
print_var_decl program "sx";
print_var_decl program "ax";
print_var_decl program "bx";
print_var_decl program "cx";
print_var_decl program "symbol_op";
[%expect
{|
var ex = [0,
5,
caml_string_of_jsbytes("hello"),
caml_string_of_jsbytes("the \xe2\x80\xa2 and \xe2\x80\xba characters")];
//end
var sx = [0, caml_string_of_jsbytes("hello2"), "the • and › characters (2)"];
//end
var ax = [0, 1, 2, 3, 4];
//end
var bx = [254, 1., 2., 3., 4.];
//end
var cx = [254, NaN, NaN, Infinity, - Infinity, 0., - 0.];
//end
var symbol_op = [0, symbol_bind, symbol_map, symbol];
//end |}]
let%expect_test _ =
let compile ~enable s =
let enable_disable = if enable then "--enable" else "--disable" in
let flags = [ enable_disable; "vardecl" ] in
compile_and_parse ~flags s
in
let program ~enable =
compile
~enable
{|
let match_expr = function
| [] | [None] | _ :: None :: _ -> 1
| [ Some None ] -> 2
| [ Some (Some 2) ] -> 3
| _ -> 4
|}
in
with_temp_dir ~f:(fun () -> print_fun_decl (program ~enable:true) (Some "match_expr"));
[%expect
{|
function match_expr(param){
var _c_, _b_, _a_;
a:
if(param){
_a_ = param[1];
if(_a_){
_b_ = _a_[1];
if(_b_){
if(2 === _b_[1] && ! param[2]) return 3;
}
else if(! param[2]) return 2;
}
else if(! param[2]) break a;
_c_ = param[2];
if(_c_ && ! _c_[1]) break a;
return 4;
}
return 1;
}
//end |}];
with_temp_dir ~f:(fun () -> print_fun_decl (program ~enable:false) (Some "match_expr"));
[%expect
{|
function match_expr(param){
a:
if(param){
var _a_ = param[1];
if(_a_){
var _b_ = _a_[1];
if(_b_){
if(2 === _b_[1] && ! param[2]) return 3;
}
else if(! param[2]) return 2;
}
else if(! param[2]) break a;
var _c_ = param[2];
if(_c_ && ! _c_[1]) break a;
return 4;
}
return 1;
}
//end |}]
|