-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathres_test.ml
156 lines (135 loc) · 4.64 KB
/
res_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
module IO = Res_io
let dataDir = "res_syntax/tests"
(* test printing of .res file*)
let () =
let filename = Filename.concat dataDir "api/resSyntax.res" in
let prettySource = Res_multi_printer.print `res ~input:filename in
assert (
prettySource
= {|// test file
if true {
Js.log("true")
} else {
Js.log("false")
}
|})
(* test printing of .resi file*)
let () =
let filename = Filename.concat dataDir "api/resiSyntax.resi" in
let prettySource = Res_multi_printer.print `res ~input:filename in
assert (prettySource = {|// test interface file
let x: int
|})
(* test printing of ocaml .ml file *)
let () =
let filename = Filename.concat dataDir "api/mlSyntax.ml" in
let prettySource = Res_multi_printer.print `ml ~input:filename in
assert (
prettySource
= {|/* test ml file */
let () = print_endline("hello world")
let unicode = "🙈 😅 🙌"
let d = `Sehr Schön`
|})
(* test printing of ocaml .mli file *)
let () =
let filename = Filename.concat dataDir "api/mliSyntax.mli" in
let prettySource = Res_multi_printer.print `ml ~input:filename in
assert (
prettySource
= {|/* test mli file */
let x: int
/* comment */
let y: float
|})
let () = print_endline "✅ multi printer api tests"
module OutcomePrinterTests = struct
let signatureToOutcome structure =
Lazy.force Res_outcome_printer.setup;
Res_compmisc.init_path ();
Clflags.nopervasives := true;
let env = Res_compmisc.initial_env () in
try
let _typedStructure, signature, _newenv =
Typemod.type_toplevel_phrase env structure
in
signature |> Printtyp.tree_of_signature
|> !Oprint.out_signature Format.str_formatter;
Format.flush_str_formatter ()
with
| Typetexp.Error (_, _, err) ->
Typetexp.report_error env Format.str_formatter err;
prerr_string (Format.flush_str_formatter ());
exit 1
| Typemod.Error (_, _, err) ->
Typemod.report_error env Format.str_formatter err;
prerr_string (Format.flush_str_formatter ());
exit 1
| Typedecl.Error (_, err) ->
Typedecl.report_error Format.str_formatter err;
prerr_string (Format.flush_str_formatter ());
exit 1
| e ->
prerr_string
("Unknown error while trying to print outcome tree.\n"
^ "We don't display all the outcome type errors; try adding the new \
case to the `try` pattern match.\n");
raise e
(* `tests/oprint/oprint.res` will be read into memory and typechecked.
* The inferred signature (i.e. the type of the module `oprint.res`) will
* then be converted to the outcome tree.
* The outcome tree is printed to a string
* and stored in a snapshot `tests/oprint/expected/oprint.resi.txt` *)
let run () =
let filename = Filename.concat dataDir "oprint/oprint.res" in
let result =
Res_driver.parsingEngine.parseImplementation ~forPrinter:false ~filename
in
let signature =
if result.Res_driver.invalid then (
Res_driver.parsingEngine.stringOfDiagnostics ~source:result.source
~filename:result.filename result.diagnostics;
exit 1)
else result.Res_driver.parsetree
in
IO.writeFile
~filename:(Filename.concat dataDir "oprint/expected/oprint.resi.txt")
~contents:(signatureToOutcome signature)
end
module ParserApiTest = struct
let makeDefault () =
let src = " let x = 1\nlet y = 2\nlet z = 3" in
let parser = Res_parser.make src "test.res" in
assert (parser.scanner.lnum == 1);
assert (parser.scanner.lineOffset == 0);
assert (parser.scanner.offset == 6);
assert (parser.token = Res_token.Let);
print_endline "✅ Parser make: initializes parser and checking offsets"
let unixLf () =
let src = "let x = 1\nlet y = 2\nlet z = 3" in
let parser = Res_parser.make src "test.res" in
(match Res_core.parseImplementation parser with
| [x; y; z] ->
assert (x.pstr_loc.loc_start.pos_lnum = 1);
assert (y.pstr_loc.loc_start.pos_lnum = 2);
assert (z.pstr_loc.loc_start.pos_lnum = 3)
| _ -> assert false);
print_endline "✅ Parser handles LF correct"
let windowsCrlf () =
let src = "let x = 1\r\nlet y = 2\r\nlet z = 3" in
let parser = Res_parser.make src "test.res" in
(match Res_core.parseImplementation parser with
| [x; y; z] ->
assert (x.pstr_loc.loc_start.pos_lnum = 1);
assert (y.pstr_loc.loc_start.pos_lnum = 2);
assert (z.pstr_loc.loc_start.pos_lnum = 3)
| _ -> assert false);
print_endline "✅ Parser handles CRLF correct"
let run () =
makeDefault ();
unixLf ();
windowsCrlf ()
end
let () = OutcomePrinterTests.run ()
let () = ParserApiTest.run ()
let () = Res_utf8_test.run ()