Skip to content

Commit e805e8c

Browse files
committed
(T): make typification tests more data driven
1 parent 82ce579 commit e805e8c

File tree

5 files changed

+137
-133
lines changed

5 files changed

+137
-133
lines changed

crates/ra_hir_ty/src/test_db.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ impl TestDB {
154154
});
155155
(buf, count)
156156
}
157+
158+
pub fn all_files(&self) -> Vec<FileId> {
159+
let mut res = Vec::new();
160+
let crate_graph = self.crate_graph();
161+
for krate in crate_graph.iter() {
162+
let crate_def_map = self.crate_def_map(krate);
163+
for (module_id, _) in crate_def_map.modules.iter() {
164+
let file_id = crate_def_map[module_id].origin.file_id();
165+
res.extend(file_id)
166+
}
167+
}
168+
res
169+
}
157170
}
158171

159172
impl TestDB {

crates/ra_hir_ty/src/tests.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use ra_syntax::{
2828
SyntaxNode,
2929
};
3030
use stdx::format_to;
31+
use test_utils::extract_annotations;
3132

3233
use crate::{
3334
db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
@@ -37,6 +38,21 @@ use crate::{
3738
// against snapshots of the expected results using insta. Use cargo-insta to
3839
// update the snapshots.
3940

41+
fn check_types(ra_fixture: &str) {
42+
let db = TestDB::with_files(ra_fixture);
43+
let mut checked_one = false;
44+
for file_id in db.all_files() {
45+
let text = db.parse(file_id).syntax_node().to_string();
46+
let annotations = extract_annotations(&text);
47+
for (offset, expected) in annotations {
48+
let actual = type_at_pos(&db, FilePosition { file_id, offset });
49+
assert_eq!(expected, actual);
50+
checked_one = true;
51+
}
52+
}
53+
assert!(checked_one, "no `//^` annotations found");
54+
}
55+
4056
fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
4157
type_at_pos_displayed(db, pos, |ty, _| ty.display(db).to_string())
4258
}

crates/ra_hir_ty/src/tests/never_type.rs

Lines changed: 46 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,267 +1,241 @@
11
use insta::assert_snapshot;
22

3-
use super::{infer_with_mismatches, type_at};
3+
use super::{check_types, infer_with_mismatches};
44

55
#[test]
66
fn infer_never1() {
7-
let t = type_at(
7+
check_types(
88
r#"
9-
//- /main.rs
109
fn test() {
1110
let t = return;
12-
t<|>;
13-
}
11+
t;
12+
} //^ !
1413
"#,
1514
);
16-
assert_eq!(t, "!");
1715
}
1816

1917
#[test]
2018
fn infer_never2() {
21-
let t = type_at(
19+
check_types(
2220
r#"
23-
//- /main.rs
2421
fn gen<T>() -> T { loop {} }
2522
2623
fn test() {
2724
let a = gen();
2825
if false { a } else { loop {} };
29-
a<|>;
30-
}
26+
a;
27+
} //^ !
3128
"#,
3229
);
33-
assert_eq!(t, "!");
3430
}
3531

3632
#[test]
3733
fn infer_never3() {
38-
let t = type_at(
34+
check_types(
3935
r#"
40-
//- /main.rs
4136
fn gen<T>() -> T { loop {} }
4237
4338
fn test() {
4439
let a = gen();
4540
if false { loop {} } else { a };
46-
a<|>;
41+
a;
42+
//^ !
4743
}
4844
"#,
4945
);
50-
assert_eq!(t, "!");
5146
}
5247

5348
#[test]
5449
fn never_type_in_generic_args() {
55-
let t = type_at(
50+
check_types(
5651
r#"
57-
//- /main.rs
5852
enum Option<T> { None, Some(T) }
5953
6054
fn test() {
6155
let a = if true { Option::None } else { Option::Some(return) };
62-
a<|>;
63-
}
56+
a;
57+
} //^ Option<!>
6458
"#,
6559
);
66-
assert_eq!(t, "Option<!>");
6760
}
6861

6962
#[test]
7063
fn never_type_can_be_reinferred1() {
71-
let t = type_at(
64+
check_types(
7265
r#"
73-
//- /main.rs
7466
fn gen<T>() -> T { loop {} }
7567
7668
fn test() {
7769
let a = gen();
7870
if false { loop {} } else { a };
79-
a<|>;
71+
a;
72+
//^ ()
8073
if false { a };
8174
}
8275
"#,
8376
);
84-
assert_eq!(t, "()");
8577
}
8678

8779
#[test]
8880
fn never_type_can_be_reinferred2() {
89-
let t = type_at(
81+
check_types(
9082
r#"
91-
//- /main.rs
9283
enum Option<T> { None, Some(T) }
9384
9485
fn test() {
9586
let a = if true { Option::None } else { Option::Some(return) };
96-
a<|>;
87+
a;
88+
//^ Option<i32>
9789
match 42 {
9890
42 => a,
9991
_ => Option::Some(42),
10092
};
10193
}
10294
"#,
10395
);
104-
assert_eq!(t, "Option<i32>");
10596
}
10697

10798
#[test]
10899
fn never_type_can_be_reinferred3() {
109-
let t = type_at(
100+
check_types(
110101
r#"
111-
//- /main.rs
112102
enum Option<T> { None, Some(T) }
113103
114104
fn test() {
115105
let a = if true { Option::None } else { Option::Some(return) };
116-
a<|>;
106+
a;
107+
//^ Option<&str>
117108
match 42 {
118109
42 => a,
119110
_ => Option::Some("str"),
120111
};
121112
}
122113
"#,
123114
);
124-
assert_eq!(t, "Option<&str>");
125115
}
126116

127117
#[test]
128118
fn match_no_arm() {
129-
let t = type_at(
119+
check_types(
130120
r#"
131-
//- /main.rs
132121
enum Void {}
133122
134123
fn test(a: Void) {
135124
let t = match a {};
136-
t<|>;
137-
}
125+
t;
126+
} //^ !
138127
"#,
139128
);
140-
assert_eq!(t, "!");
141129
}
142130

143131
#[test]
144132
fn match_unknown_arm() {
145-
let t = type_at(
133+
check_types(
146134
r#"
147-
//- /main.rs
148135
fn test(a: Option) {
149136
let t = match 0 {
150137
_ => unknown,
151138
};
152-
t<|>;
153-
}
139+
t;
140+
} //^ {unknown}
154141
"#,
155142
);
156-
assert_eq!(t, "{unknown}");
157143
}
158144

159145
#[test]
160146
fn if_never() {
161-
let t = type_at(
147+
check_types(
162148
r#"
163-
//- /main.rs
164149
fn test() {
165150
let i = if true {
166151
loop {}
167152
} else {
168153
3.0
169154
};
170-
i<|>;
171-
}
155+
i;
156+
} //^ f64
172157
"#,
173158
);
174-
assert_eq!(t, "f64");
175159
}
176160

177161
#[test]
178162
fn if_else_never() {
179-
let t = type_at(
163+
check_types(
180164
r#"
181-
//- /main.rs
182165
fn test(input: bool) {
183166
let i = if input {
184167
2.0
185168
} else {
186169
return
187170
};
188-
i<|>;
189-
}
171+
i;
172+
} //^ f64
190173
"#,
191174
);
192-
assert_eq!(t, "f64");
193175
}
194176

195177
#[test]
196178
fn match_first_arm_never() {
197-
let t = type_at(
179+
check_types(
198180
r#"
199-
//- /main.rs
200181
fn test(a: i32) {
201182
let i = match a {
202183
1 => return,
203184
2 => 2.0,
204185
3 => loop {},
205186
_ => 3.0,
206187
};
207-
i<|>;
208-
}
188+
i;
189+
} //^ f64
209190
"#,
210191
);
211-
assert_eq!(t, "f64");
212192
}
213193

214194
#[test]
215195
fn match_second_arm_never() {
216-
let t = type_at(
196+
check_types(
217197
r#"
218-
//- /main.rs
219198
fn test(a: i32) {
220199
let i = match a {
221200
1 => 3.0,
222201
2 => loop {},
223202
3 => 3.0,
224203
_ => return,
225204
};
226-
i<|>;
227-
}
205+
i;
206+
} //^ f64
228207
"#,
229208
);
230-
assert_eq!(t, "f64");
231209
}
232210

233211
#[test]
234212
fn match_all_arms_never() {
235-
let t = type_at(
213+
check_types(
236214
r#"
237-
//- /main.rs
238215
fn test(a: i32) {
239216
let i = match a {
240217
2 => return,
241218
_ => loop {},
242219
};
243-
i<|>;
244-
}
220+
i;
221+
} //^ !
245222
"#,
246223
);
247-
assert_eq!(t, "!");
248224
}
249225

250226
#[test]
251227
fn match_no_never_arms() {
252-
let t = type_at(
228+
check_types(
253229
r#"
254-
//- /main.rs
255230
fn test(a: i32) {
256231
let i = match a {
257232
2 => 2.0,
258233
_ => 3.0,
259234
};
260-
i<|>;
261-
}
235+
i;
236+
} //^ f64
262237
"#,
263238
);
264-
assert_eq!(t, "f64");
265239
}
266240

267241
#[test]

0 commit comments

Comments
 (0)