This repository was archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathres_test.ml
207 lines (173 loc) · 5.66 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
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
module IO = Res_io
module Snapshot = struct
(* If set to true, will always create a new snapshot file.
* Previous snapshots will be overwritten *)
let forceUpdate = ref false
let take ~filename ~contents =
(* snapshot filename, just append .snapshot *)
let snapFilename = filename ^ ".snapshot" in
let diff =
(* create the file when snapshot doesn't exist or we're force updating *)
if !forceUpdate || not (Sys.file_exists snapFilename) then (
IO.writeFile ~filename:snapFilename ~contents;
None
) else (
(* snapshot file exists *)
let snapContents = IO.readFile ~filename:snapFilename in
(* check for equality, do diffing later if needed *)
if contents = snapContents then None else Some snapContents
)
in
match diff with
| Some snapContents ->
prerr_string ("❌ snapshot " ^ filename);
prerr_newline();
Res_diff.diffTwoStrings snapContents contents;
exit 1
| None ->
print_endline (
if !forceUpdate then
"✍️ updated snapshot " ^ filename
else
"✅ snapshot " ^ filename
)
end
let usage = "Usage: test.exe <options>\nOptions are:"
let spec = [
("-update-snapshot", Arg.Unit (fun () -> Snapshot.forceUpdate.contents <- true), "Update snapshots")
]
let () = Arg.parse spec (fun _ -> ()) usage
(* test printing of .res file*)
let () =
let filename = "./tests/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 = "./tests/api/resiSyntax.resi" in
let prettySource = Res_multi_printer.print `res ~input:filename in
assert (
prettySource = {|// test interface file
let x: int
|}
)
let refmtBinaryPath = "./lib/refmt.exe"
(* test printing of reason .re file *)
let () =
let filename = "./tests/api/reasonSyntax.re" in
let prettySource = Res_multi_printer.print (`refmt refmtBinaryPath) ~input:filename in
assert (
prettySource = {|// test .re file
let \"+++" = (a, b) => a + b
let unicode = "🙈 😅 🙌"
let d = `Sehr Schön` /* test */
let () = print_endline("foo")
|}
)
(* test printing of reason .rei file *)
let () =
let filename = "./tests/api/reiSyntax.rei" in
let prettySource = Res_multi_printer.print (`refmt refmtBinaryPath) ~input:filename in
assert (
prettySource = {|// test .rei file
let x: int
|}
)
(* test printing of ocaml .ml file *)
let () =
let filename = "./tests/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 = "./tests/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 parseFile filename =
let result = Res_driver.parsingEngine.parseImplementation ~forPrinter:false ~filename in
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
let outcomeOfStructure structure =
Lazy.force Res_outcome_printer.setup;
Compmisc.init_path false;
let env = 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;
| _ ->
prerr_string "Unknown error while trying to print outcome tree";
exit 1
(* `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/oprint.res.snapshot` *)
let run () =
let testFileName = "tests/oprint/oprint.res" in
let printedOutcomeTree =
parseFile testFileName |> outcomeOfStructure
in
Snapshot.take ~filename:testFileName ~contents:printedOutcomeTree
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.scanner.rdOffset == 7);
assert (parser.token = Res_token.Let);
print_endline "✅ Parser make: initializes parser defaulting to line 1 "
let seedLineNumber () =
let src = "let x = 1\nlet y = 2\nlet z = 3" in
let parser = Res_parser.make ~line:2 src "test.res" in
assert (parser.scanner.lnum == 2);
assert (parser.scanner.lineOffset == 0);
assert (parser.scanner.offset == 3);
assert (parser.scanner.rdOffset == 4);
assert (parser.token = Res_token.Let);
print_endline "✅ Parser make: initializes parser with line set to 2"
let run () =
makeDefault();
seedLineNumber()
end
let () = OutcomePrinterTests.run()
let () = ParserApiTest.run()