-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ml
230 lines (212 loc) · 6.24 KB
/
test.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
(**
* Unit-tests for subsetphp
*
* @author Olle Harstedt
* @since 2015-07-28
14:52:19 - ollehar: can I make ounit NOT test in parallell?
14:53:29 - companion_cube: use OUnit instead of OUnit2
14:53:55 - ely-se: run your tests on a single-core machine
14:54:28 - ely-se: or a global mutex!
14:54:52 - zozozo: ollehar: do something like ./test -runner sequential
*)
open OUnit2
open OUnitTest
open Parser_hack
open Printf
open Infer
open ListLabels
open Typedast
let test_simple_variable_inference test_ctxt =
let code = "
<?php
$a = 10;
$a = 'asd';
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
assert_raises
(Infer.Error "cannot unify types Infer.TNumber and Infer.TString")
(fun _ -> infer_program 0 parser_return.ast)
let test_variable_assignment text_ctxt =
let code = "
<?php
$a = $b;
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
assert_raises
(Failure "Can't use variable before it's defined: $b")
(fun _ -> infer_program 0 parser_return.ast)
(** TODO: Test type tree
[(Typedast.Stmt
Typedast.Expr (Typedast.TUnit,
(<opaque>,
Typedast.Binop ((Typedast.Eq None),
(<opaque>, Typedast.Lvar ((<opaque>, "$a"), Typedast.TNumber)),
(<opaque>, (Typedast.Int (<opaque>, "10"))), Typedast.TUnit))))]
*)
let test_variable_assignment2 test_ctxt =
let code = "
<?php
$a = 10;
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
let ty = infer_program 0 parser_return.ast in
let a_ty = match ty with
| [(Stmt
Expr (TUnit,
(_,
Binop ((Eq None),
(_, Lvar ((_, "$a"), ty)),
(_, (Int (_, "10"))), TUnit))))] ->
ty
| _ ->
TUnknown
in
assert_equal ~msg:"Variable has type number" a_ty TNumber
let test_variable_assignment3 test_ctxt =
let code = "
<?php
$a = 10;
$b = 'asd';
$a = $b;
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
assert_raises
(Infer.Error "cannot unify types Infer.TNumber and Infer.TString")
(fun _ -> infer_program 0 parser_return.ast)
let test_function_return_type test_ctxt =
let code = "
<?php
function foo() {
return 10;
}
foo();
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
assert_raises
(Infer.Error "cannot unify types Infer.TUnit and Infer.TNumber")
(fun _ -> infer_program 0 parser_return.ast)
let test_function_return_type2 test_ctxt =
let code = "
<?php
function foo() {
return;
}
$a = foo();
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
assert_raises
(Failure "Right-hand can't evaluate to void")
(fun _ -> infer_program 0 parser_return.ast)
let test_function_return_type3 test_ctxt =
let code = "
<?php
function foo() {
return 10;
}
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
let inferred_type = infer_program 0 parser_return.ast in
let function_type = match inferred_type with
| [(Fun {f_name = (_, "\\foo"); f_params = []; f_ret})] ->
f_ret
| _ ->
TUnknown
in
assert_equal ~msg:"Function returns number" function_type TNumber
let test_function_return_type4 test_ctxt =
let code = "
<?php
function foo() {
return 'asd';
}
$a = 10;
$a = $a + foo();
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
assert_raises
(Infer.Error "cannot unify types Infer.TString and Infer.TNumber")
(fun _ -> infer_program 0 parser_return.ast)
let test_function_return_type5 test_ctxt =
let code = "
<?php
function foo() {
return 'asd';
}
function bar() {
return 10;
}
$a = bar() + foo();
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
assert_raises
(Infer.Error "cannot unify types Infer.TString and Infer.TNumber")
(fun _ -> infer_program 0 parser_return.ast)
(** Infer argument type *)
let test_function_argument_type1 test_ctxt =
let code = "
<?php
function foo($i) {
return $i + 10;
}
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
let inferred_type = infer_program 0 parser_return.ast in
let arg_ty = match inferred_type with
| [(Fun {
f_name = (_, "\\foo");
f_params = [{ param_id = (_, "$i");
param_type}];
f_ret = TNumber })
] ->
param_type
| _ -> TUnknown
in
assert_equal ~msg:"Function argument is number" arg_ty TNumber
(** Infer argument type *)
let test_function_argument_type2 test_ctxt =
let code = "
<?php
function foo($i, $j) {
$i = 10;
$j = 'asd';
}
" in
let parser_return = Parser_hack.program (Relative_path.Root, "") code in
let inferred_type = infer_program 0 parser_return.ast in
let arg_ty = match inferred_type with
| [(Fun {
f_name = (_, "\\foo");
f_params = [{param_id = (_, "$i"); param_type = param_type_i};
{param_id = (_, "$j"); param_type = param_type_j}];
f_ret})
] ->
(param_type_i, param_type_j, f_ret)
| _ ->
(TUnknown, TUnknown, TUnknown)
in
assert_equal ~msg:"Function argument is number" arg_ty (TNumber, TString, TUnit)
let test_list = [
"simple_variable_inference", test_simple_variable_inference;
"variable_assignment", test_variable_assignment;
"variable_assignment2", test_variable_assignment2;
"variable_assignment3", test_variable_assignment3;
"function_return_type", test_function_return_type;
"function_return_type2", test_function_return_type2;
"function_return_type3", test_function_return_type3;
"function_return_type4", test_function_return_type4;
"function_return_type5", test_function_return_type5;
"function_argument_type1", test_function_argument_type1;
"function_argument_type2", test_function_argument_type2;
]
let tear_down () test_ctxt =
()
let tag_test_suite =
"tags" >::: (map test_list ~f:(fun (name, test_fn) ->
name >:: (fun test_ctxt ->
bracket ignore tear_down test_ctxt;
test_fn test_ctxt
)
))
let _ =
SharedMem.(init default_config);
run_test_tt_main tag_test_suite