Represent empty position ranges as single character ranges when giving them to the...
[hiphop-php.git] / hphp / hack / test / unit / utils / diagnostic_subscription_test.ml
blob6841c13e33ecd83ee0922f058427c1724549202e
1 open Hh_core
2 open Reordered_argument_collections
4 module DS = Diagnostic_subscription
6 let errors_to_string buf x =
7 List.iter x ~f: begin fun error ->
8 Printf.bprintf buf "%s\n" (Errors.to_string error)
9 end
11 let diagnostics_to_string x =
12 let buf = Buffer.create 1024 in
13 SMap.iter x ~f:begin fun path errors ->
14 Printf.bprintf buf "%s:\n" path;
15 errors_to_string buf errors;
16 end;
17 Buffer.contents buf
19 let error_list_to_string_buffer buf x =
20 List.iter x ~f: begin fun error ->
21 Printf.bprintf buf "%s\n" Errors.(error |> to_absolute |> to_string)
22 end
24 let error_list_to_string errors =
25 let buf = Buffer.create 1024 in
26 error_list_to_string_buffer buf errors;
27 Buffer.contents buf
29 let create_path x = Relative_path.(create Root ("/" ^ x))
31 let error_in path message = Errors.parsing_error ((Pos.make_from path), message)
33 let test_update () =
34 let a_path = create_path "A" in
35 let b_path = create_path "B" in
37 let foo_error_a, () =
38 Errors.do_with_context a_path Errors.Parsing begin fun () ->
39 error_in a_path "foo";
41 end in
42 let bar_error_a, () =
43 Errors.do_with_context a_path Errors.Parsing begin fun () ->
44 error_in a_path "bar";
46 end in
47 let bar_error_a = Errors.incremental_update_set
48 ~old:foo_error_a
49 ~new_:bar_error_a
50 ~rechecked:(Relative_path.Set.singleton a_path)
51 Errors.Parsing
54 let baz_error_b, () =
55 Errors.do_with_context b_path Errors.Parsing begin fun () ->
56 error_in b_path "baz";
58 end in
59 let baz_error_b = Errors.incremental_update_set
60 ~old:bar_error_a
61 ~new_:baz_error_b
62 ~rechecked:(Relative_path.Set.singleton b_path)
63 Errors.Parsing
66 let bar_error_cleared_a = Errors.incremental_update_set
67 ~old:baz_error_b
68 ~new_:Errors.empty
69 ~rechecked:(Relative_path.Set.singleton a_path)
70 Errors.Parsing
72 let ds = DS.of_id ~id:1 ~init:Errors.empty in
74 let priority_files = Relative_path.Set.empty in
75 let reparsed = Relative_path.Set.empty in
76 let rechecked_a = Relative_path.Map.singleton a_path FileInfo.empty_names in
77 let rechecked_b = Relative_path.Map.singleton b_path FileInfo.empty_names in
79 let ds, diagnostics = DS.update ds
80 ~priority_files
81 ~reparsed
82 ~rechecked:rechecked_a
83 ~global_errors:foo_error_a
84 ~full_check_done:true
85 |> DS.pop_errors ~global_errors:foo_error_a in
86 let expected =
87 "/A:\nFile \"/A\", line 0, characters 0-0:\nfoo (Parsing[1002])\n\n" in
88 Asserter.String_asserter.assert_equals expected
89 (diagnostics_to_string diagnostics)
90 "foo error in A should be pushed";
92 let ds, diagnostics = DS.update ds
93 ~priority_files
94 ~reparsed
95 ~rechecked:rechecked_a
96 ~global_errors:foo_error_a
97 ~full_check_done:false
98 |> DS.pop_errors ~global_errors:foo_error_a in
100 Asserter.Bool_asserter.assert_equals true
101 (SMap.is_empty diagnostics)
102 "Unchanged diagnostics in A should be not pushed again";
104 let expected =
105 "/A:\nFile \"/A\", line 0, characters 0-0:\nbar (Parsing[1002])\n\n" in
106 let ds, diagnostics = DS.update ds
107 ~priority_files
108 ~reparsed
109 ~rechecked:rechecked_a
110 ~global_errors:bar_error_a
111 ~full_check_done:false
112 |> DS.pop_errors ~global_errors:bar_error_a in
113 Asserter.String_asserter.assert_equals expected
114 (diagnostics_to_string diagnostics)
115 "foo error in A should be replaced with bar";
117 let priority_files = Relative_path.Set.singleton b_path in
119 let expected =
120 "/B:\nFile \"/B\", line 0, characters 0-0:\nbaz (Parsing[1002])\n\n" in
121 let ds, diagnostics = DS.update ds
122 ~priority_files
123 ~reparsed
124 ~rechecked:rechecked_b
125 ~global_errors:baz_error_b
126 ~full_check_done:false
127 |> DS.pop_errors ~global_errors:baz_error_b in
128 Asserter.String_asserter.assert_equals expected
129 (diagnostics_to_string diagnostics)
130 "baz error in B should be added";
132 let ds, diagnostics = DS.update ds
133 ~priority_files
134 ~reparsed
135 ~rechecked:rechecked_a
136 ~global_errors:bar_error_cleared_a
137 ~full_check_done:false
138 |> DS.pop_errors ~global_errors:bar_error_cleared_a in
139 let expected = "/A:\n" in
140 Asserter.String_asserter.assert_equals expected
141 (diagnostics_to_string diagnostics)
142 "A diagnostics should be cleared";
144 ignore ds;
145 true
147 let test_error_sources () =
148 let a_path = create_path "A" in
149 let b_path = create_path "B" in
151 let errors, () =
152 Errors.do_ begin fun () ->
153 Errors.run_in_context a_path Errors.Typing begin fun () ->
154 error_in a_path "error from a";
155 end;
156 Errors.run_in_context b_path Errors.Typing begin fun () ->
157 error_in a_path "error from b";
158 end;
162 let ds = DS.of_id ~id:1 ~init:Errors.empty in
164 let priority_files = Relative_path.Set.empty in
165 let rechecked = Relative_path.Map.(
166 (singleton a_path FileInfo.empty_names) |> union
167 (singleton b_path FileInfo.empty_names)
168 ) in
170 let ds = DS.update ds
171 ~priority_files
172 ~reparsed:Relative_path.Set.empty
173 ~rechecked:rechecked
174 ~global_errors:errors
175 ~full_check_done:true
178 Asserter.Bool_asserter.assert_equals true
179 (Relative_path.Set.mem (DS.error_sources ds) a_path)
180 "error_sources should contain A";
182 Asserter.Bool_asserter.assert_equals true
183 (Relative_path.Set.mem (DS.error_sources ds) b_path)
184 "error_sources should contain B";
185 true
187 let tests = [
188 "test_update", test_update;
189 "test_error_sources", test_error_sources;
192 let () =
193 Relative_path.(set_path_prefix Root (Path.make "/"));
194 Unit_test.run_all tests