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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use super::*;
#[test]
fn qualified_reference_resolves_at_an_outer_enclosing_namespace_prefix() {
// From within `Fixtures.Billing` (2 segments), a reference to
// `Common.IIdentifiable` must find `Fixtures.Common.IIdentifiable`
// by trying prefix "Fixtures" (outer), after "Fixtures.Billing"
// (innermost) fails -- exercises the walk, not just a literal or
// innermost-only match.
let files = vec![
(
"Common/IIdentifiable.cs".to_string(),
frag(
vec![def(
"Fixtures.Common.IIdentifiable",
"IIdentifiable",
"Fixtures.Common",
"interface",
)],
vec![],
vec![],
),
),
(
"Billing/Invoice.cs".to_string(),
frag(
vec![def(
"Fixtures.Billing.Invoice",
"Invoice",
"Fixtures.Billing",
"class",
)],
vec![],
vec![type_ref(
"inherits",
"IIdentifiable",
Some("Common.IIdentifiable"),
"Fixtures.Billing",
)],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
let edge =
find_edge(&g, |e| matches!(e, Edge::Inherits { .. })).expect("resolved via namespace walk");
match edge {
Edge::Inherits { to, .. } => assert_eq!(to, "Fixtures.Common.IIdentifiable"),
_ => unreachable!(),
}
}
#[test]
fn qualified_reference_with_no_matching_prefix_and_no_literal_match_is_external() {
let files = vec![(
"A/Probe.cs".to_string(),
frag(
vec![def("A.Probe", "Probe", "A", "class")],
vec![],
vec![type_ref("uses-type", "Y", Some("X.Y"), "A")],
),
)];
let g = resolve_graph(&no_git_root(), &files);
assert_eq!(g.stats.unresolved_external_count, 1);
}
// --- qualified (dotted) resolution: the suffix fallback -----------------
#[test]
fn foreign_qualified_enum_member_never_binds_to_a_same_named_in_tree_enum() {
// The RabbitMQ.Client.ExchangeType shape: an in-tree enum shares its
// bare name with a foreign one, and only the dotted TEXT tells them
// apart -- step 1b must reject it rather than let the enum's own
// unconditional-emission rule wave it through.
let files = vec![
(
"Fabric/ExchangeType.cs".to_string(),
frag(
vec![
def(
"App.Transports.Fabric.ExchangeType",
"ExchangeType",
"App.Transports.Fabric",
"enum",
),
def(
"App.Transports.Fabric.ExchangeType.Fanout",
"Fanout",
"App.Transports.Fabric",
"enum-member",
),
def(
"App.Transports.Fabric.ExchangeType.Topic",
"Topic",
"App.Transports.Fabric",
"enum-member",
),
],
vec![],
vec![],
),
),
(
"Bus/Configure.cs".to_string(),
frag(
vec![def("App.Bus.Configure", "Configure", "App.Bus", "class")],
vec![],
vec![member_ref(
"ExchangeType",
Some("RabbitMQ.Client.ExchangeType"),
"Fanout",
"App.Bus",
)],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
assert!(member_edge_targets(&g).is_empty());
// An enum MEMBER never lands in `member_name_to_defs` (built from
// methods/properties/fields only, see the module header's asymmetry
// note) -- so the scored tier has no pool to draw from either. The
// foreign qualifier is dropped silently, not guessed.
assert!(heuristic_member_edge_targets(&g).is_empty());
}
#[test]
fn own_namespace_relative_and_bare_qualified_enum_uses_still_resolve() {
// The suffix rule only forecloses a FOREIGN dotted qualifier -- every
// shape C# actually uses to name the SAME enum (an exact match, a
// relative qualification resolved by the enclosing-prefix walk at
// step 1, and a bare name through a using) must keep resolving.
let enum_defs = vec![
def(
"App.Transports.Fabric.ExchangeType",
"ExchangeType",
"App.Transports.Fabric",
"enum",
),
def(
"App.Transports.Fabric.ExchangeType.Fanout",
"Fanout",
"App.Transports.Fabric",
"enum-member",
),
def(
"App.Transports.Fabric.ExchangeType.Topic",
"Topic",
"App.Transports.Fabric",
"enum-member",
),
];
let files = vec![
(
"Fabric/ExchangeType.cs".to_string(),
frag(enum_defs, vec![], vec![]),
),
(
"Bus/Exact.cs".to_string(),
frag(
vec![def("App.Bus.Exact", "Exact", "App.Bus", "class")],
vec![],
vec![member_ref(
"ExchangeType",
Some("App.Transports.Fabric.ExchangeType"),
"Topic",
"App.Bus",
)],
),
),
(
"Relative.cs".to_string(),
frag(
vec![def("App.Relative", "Relative", "App", "class")],
vec![],
vec![member_ref(
"ExchangeType",
Some("Transports.Fabric.ExchangeType"),
"Topic",
"App",
)],
),
),
(
"Bus/Bare.cs".to_string(),
frag(
vec![def("App.Bus.Bare", "Bare", "App.Bus", "class")],
vec![FragUsing::Plain {
text: "App.Transports.Fabric".into(),
global: false,
namespace: String::new(),
}],
vec![member_ref("ExchangeType", None, "Fanout", "App.Bus")],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
let mut targets = member_edge_targets(&g);
targets.sort_unstable();
let mut expected = vec![
"App.Transports.Fabric.ExchangeType.Fanout",
"App.Transports.Fabric.ExchangeType.Topic",
"App.Transports.Fabric.ExchangeType.Topic",
];
expected.sort_unstable();
assert_eq!(targets, expected);
}
#[test]
fn foreign_qualified_static_call_never_binds_to_a_same_named_in_tree_class() {
let json = def_with(
"App.Infra.JsonSerializer",
"JsonSerializer",
"App.Infra",
"class",
&["Serialize"],
&[],
&[],
);
let foreign_ref = FragRef {
arg_count: Some(1),
..member_ref(
"JsonSerializer",
Some("System.Text.Json.JsonSerializer"),
"Serialize",
"App.Svc",
)
};
let files = vec![
(
"Infra/JsonSerializer.cs".to_string(),
frag(vec![json.clone()], vec![], vec![]),
),
(
"Svc/Foreign.cs".to_string(),
frag(
vec![def("App.Svc.Foreign", "Foreign", "App.Svc", "class")],
vec![],
vec![foreign_ref],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
assert!(
member_edge_targets(&g).is_empty(),
"the foreign qualifier must never bind the precise edge to the in-tree class"
);
// The suffix rule only tightens the PRECISE ladder (step 1b); the
// scored tier's member-name uniqueness pool is a wholly separate
// path, and "Serialize" is unique in this graph -- so the guess
// still fires. This is deliberate: the suffix rule narrows certainty,
// it does not widen what the scored tier is willing to guess.
assert_eq!(
heuristic_member_edge_targets(&g),
vec!["App.Infra.JsonSerializer"]
);
let own_ref = FragRef {
arg_count: Some(1),
..member_ref(
"JsonSerializer",
Some("App.Infra.JsonSerializer"),
"Serialize",
"App.Svc",
)
};
let files = vec![
(
"Infra/JsonSerializer.cs".to_string(),
frag(vec![json], vec![], vec![]),
),
(
"Svc/Own.cs".to_string(),
frag(
vec![def("App.Svc.Own", "Own", "App.Svc", "class")],
vec![],
vec![own_ref],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
assert_eq!(member_edge_targets(&g), vec!["App.Infra.JsonSerializer"]);
}
#[test]
fn property_hop_through_an_external_intermediate_never_binds_a_same_named_type() {
// "expr.Member.Name" (an Expression-tree walk): the extractor
// flattens the qualifier to "expr.Member", which happens to share
// its tail with an in-tree type named "Member". Neither a top-level
// nor a NESTED same-named type may answer for it -- "expr" is not a
// namespace prefix at all, and the suffix rule only cares whether the
// def's own path ends with the written text.
let top_level = def_with(
"App.Model.Member",
"Member",
"App.Model",
"class",
&[],
&["Name"],
&[],
);
let files = vec![
(
"Model/Member.cs".to_string(),
frag(vec![top_level], vec![], vec![]),
),
(
"Svc/Hop.cs".to_string(),
frag(
vec![def("App.Svc.Hop", "Hop", "App.Svc", "class")],
vec![],
vec![member_ref("Member", Some("expr.Member"), "Name", "App.Svc")],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
assert!(member_edge_targets(&g).is_empty());
let nested = def_with(
"App.Model.Outer+Member",
"Member",
"App.Model",
"class",
&[],
&["Name"],
&[],
);
let files = vec![
(
"Model/Outer.cs".to_string(),
frag(vec![nested], vec![], vec![]),
),
(
"Svc/Hop.cs".to_string(),
frag(
vec![def("App.Svc.Hop", "Hop", "App.Svc", "class")],
vec![],
vec![member_ref("Member", Some("expr.Member"), "Name", "App.Svc")],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
assert!(member_edge_targets(&g).is_empty());
}
#[test]
fn foreign_qualified_base_type_is_external_not_an_inherits_edge() {
let files = vec![
(
"Messaging/DefaultBasicConsumer.cs".to_string(),
frag(
vec![def(
"App.Messaging.DefaultBasicConsumer",
"DefaultBasicConsumer",
"App.Messaging",
"class",
)],
vec![],
vec![],
),
),
(
"Bus/Consumer.cs".to_string(),
frag(
vec![def("App.Bus.Consumer", "Consumer", "App.Bus", "class")],
vec![],
vec![type_ref(
"inherits",
"DefaultBasicConsumer",
Some("RabbitMQ.Client.DefaultBasicConsumer"),
"App.Bus",
)],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
assert!(g.edges.iter().all(|e| !matches!(e, Edge::Inherits { .. })));
assert_eq!(g.stats.unresolved_external_count, 1);
}
#[test]
fn alias_qualified_name_whose_target_lacks_the_type_is_external_even_when_another_namespace_has_it()
{
// The alias rewrite (step 1a) hands step 1b the EXPANDED text, not
// the literal "Ns.MyEnum" -- an alias pointed at the wrong namespace
// must not fall back to matching some unrelated namespace's
// same-named enum by suffix.
let files = vec![
(
"Other/MyEnum.cs".to_string(),
frag(
vec![
def("App.Other.MyEnum", "MyEnum", "App.Other", "enum"),
def("App.Other.MyEnum.On", "On", "App.Other", "enum-member"),
],
vec![],
vec![],
),
),
(
"Consumers/AliasMiss.cs".to_string(),
frag(
vec![def(
"App.Consumers.AliasMiss",
"AliasMiss",
"App.Consumers",
"class",
)],
vec![FragUsing::Alias {
alias: "Ns".into(),
target: "Some.Namespace".into(),
global: false,
}],
vec![member_ref(
"MyEnum",
Some("Ns.MyEnum"),
"On",
"App.Consumers",
)],
),
),
];
let g = resolve_graph(&no_git_root(), &files);
assert!(member_edge_targets(&g).is_empty());
}