-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcomplexity_test.go
More file actions
465 lines (437 loc) · 14.4 KB
/
Copy pathcomplexity_test.go
File metadata and controls
465 lines (437 loc) · 14.4 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package rubyextractor
import (
"testing"
"github.com/enola-labs/enola/internal/facts"
)
// --- helpers (symbolsByName / hasCall live in ruby_test.go) ---
func rbIntProp(t *testing.T, f facts.Fact, key string) int {
t.Helper()
v, ok := f.Props[key]
if !ok {
return 0
}
switch n := v.(type) {
case int:
return n
case float64:
return int(n)
}
t.Fatalf("prop %q is not numeric: %T", key, v)
return 0
}
func rbStrSlice(f facts.Fact, key string) []string {
v, ok := f.Props[key]
if !ok {
return nil
}
switch s := v.(type) {
case []string:
return s
case []any:
out := make([]string, 0, len(s))
for _, a := range s {
if str, ok := a.(string); ok {
out = append(out, str)
}
}
return out
}
return nil
}
func rbContains(haystack []string, want string) bool {
for _, s := range haystack {
if s == want {
return true
}
}
return false
}
// --- tests ---
func TestRbComplexity_EachBlockIsLoop(t *testing.T) {
src := `class Worker
def run(users)
users.each do |u|
notify(u)
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/worker.rb", false, false))["Worker#run"]
if got := rbIntProp(t, f, "loop_depth"); got != 1 {
t.Errorf("loop_depth = %d, want 1", got)
}
if got := rbIntProp(t, f, "loop_count"); got != 1 {
t.Errorf("loop_count = %d, want 1", got)
}
if cil := rbStrSlice(f, "calls_in_loop"); !rbContains(cil, "notify") {
t.Errorf("calls_in_loop = %v, want to contain notify", cil)
}
}
func TestRbComplexity_NestedIterators(t *testing.T) {
src := `class Worker
def run(users)
users.each do |u|
u.posts.each do |p|
log(p)
end
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/worker.rb", false, false))["Worker#run"]
if got := rbIntProp(t, f, "loop_depth"); got != 2 {
t.Errorf("loop_depth = %d, want 2", got)
}
if cil := rbStrSlice(f, "calls_in_loop"); !rbContains(cil, "log") {
t.Errorf("calls_in_loop = %v, want to contain log", cil)
}
}
func TestRbComplexity_ConstantBoundLoopNoDepth(t *testing.T) {
// A constant-bounded iterator runs a fixed number of times, so it is a loop
// (cyclomatic) but adds NO scaling depth.
for _, tc := range []struct{ name, body string }{
{"integer.times", "6.times { |i| work(i) }"},
{"literal array", "[:a, :b, :c].each { |x| work(x) }"},
{"word array", "%w[a b c].each { |x| work(x) }"},
{"screaming constant", "STOP_CHARS.each { |c| work(c) }"},
} {
src := "class Worker\n def run\n " + tc.body + "\n end\nend\n"
f := symbolsByName(extractFileAST([]byte(src), "app/worker.rb", false, false))["Worker#run"]
if _, present := f.Props["loop_depth"]; present {
t.Errorf("%s: loop_depth = %v, want unset (constant-bounded loop)", tc.name, f.Props["loop_depth"])
}
if got := rbIntProp(t, f, "loop_count"); got != 1 {
t.Errorf("%s: loop_count = %d, want 1 (still a loop for cyclomatic)", tc.name, got)
}
}
}
func TestRbComplexity_LiteralChainLoopIsBounded(t *testing.T) {
// A bounded literal behind a trailing chain method stays bounded: the inner
// `[a, b].compact.all?` is ≤2 elements, so nesting it in a scaling outer loop is
// O(n), not O(n²).
src := `class Gate
def valid?(items)
items.each do |it|
[it.a, it.b].compact.all? do |url|
allowed?(url)
end
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/gate.rb", false, false))["Gate#valid?"]
if got := rbIntProp(t, f, "loop_depth"); got != 1 {
t.Errorf("loop_depth = %d, want 1 (literal-chain inner loop is bounded)", got)
}
// A chained bounded literal as an outer loop adds no depth at all.
src2 := `class Gate
def run
[1, 2].map { |x| x }.each { |y| work(y) }
end
end
`
g := symbolsByName(extractFileAST([]byte(src2), "app/gate.rb", false, false))["Gate#run"]
if _, present := g.Props["loop_depth"]; present {
t.Errorf("loop_depth = %v, want unset ([1,2].map.each is bounded)", g.Props["loop_depth"])
}
}
func TestRbComplexity_ConstantInnerLoopDoesNotMultiply(t *testing.T) {
// A scaling outer loop with a CONSTANT inner loop is O(n), not O(n²): only the
// outer contributes depth, but per-iteration I/O is still measured against n.
src := `class Worker
def run(users)
users.each do |u|
STOP_CHARS.each do |c|
u.save
end
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/worker.rb", false, false))["Worker#run"]
if got := rbIntProp(t, f, "loop_depth"); got != 1 {
t.Errorf("loop_depth = %d, want 1 (constant inner loop must not multiply)", got)
}
if cil := rbStrSlice(f, "calls_in_loop"); !rbContains(cil, "save") {
t.Errorf("calls_in_loop = %v, want to contain save (per-iteration I/O still flagged)", cil)
}
}
func TestRbComplexity_IteratorReceiverEvaluatedOnce(t *testing.T) {
// User.where(...) is the iterator's receiver — evaluated once, NOT per element.
// Mailer.deliver(u) runs inside the block, so it is the in-loop call.
src := `class Worker
def run
User.where(active: true).each do |u|
Mailer.deliver(u)
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/worker.rb", true, false))["Worker#run"]
if !hasCall(f, "User.where") || !hasCall(f, "Mailer.deliver") {
t.Errorf("expected call edges to User.where and Mailer.deliver; relations=%v", f.Relations)
}
cil := rbStrSlice(f, "calls_in_loop")
if !rbContains(cil, "Mailer.deliver") {
t.Errorf("calls_in_loop = %v, want to contain Mailer.deliver", cil)
}
if rbContains(cil, "User.where") {
t.Errorf("calls_in_loop = %v, must NOT contain User.where (iterator receiver runs once)", cil)
}
}
func TestRbComplexity_WhileLoop(t *testing.T) {
src := `class Worker
def run
while pending?
process
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/worker.rb", false, false))["Worker#run"]
if got := rbIntProp(t, f, "loop_depth"); got != 1 {
t.Errorf("loop_depth = %d, want 1", got)
}
if got := rbIntProp(t, f, "loop_count"); got != 1 {
t.Errorf("loop_count = %d, want 1", got)
}
if cil := rbStrSlice(f, "calls_in_loop"); !rbContains(cil, "process") {
t.Errorf("calls_in_loop = %v, want to contain process", cil)
}
}
func TestRbComplexity_RecursiveSelf(t *testing.T) {
src := `class Calc
def fib(n)
return n if n < 2
fib(n - 1) + fib(n - 2)
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/calc.rb", false, false))["Calc#fib"]
v, ok := f.Props["recursive_self"].(bool)
if !ok || !v {
t.Errorf("recursive_self = %v (ok=%v), want true", f.Props["recursive_self"], ok)
}
// 1 (base) + `return n if n < 2` (if_modifier) = 2
if got := rbIntProp(t, f, "cyclomatic"); got != 2 {
t.Errorf("cyclomatic = %d, want 2", got)
}
if _, present := f.Props["loop_depth"]; present {
t.Errorf("loop_depth should be omitted for a loop-free method, got %v", f.Props["loop_depth"])
}
}
func TestRbComplexity_SuperIsNotRecursion(t *testing.T) {
// `super` climbs the inheritance chain and terminates — it is not self-recursion.
// It was the dominant recursion false positive (every override calling super).
src := `class ShadowUser < Base
def hood_id
deprecate(__method__)
super
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/shadow_user.rb", false, false))["ShadowUser#hood_id"]
if v, _ := f.Props["recursive_self"].(bool); v {
t.Errorf("recursive_self = true for a method whose only self-name call is `super`; want unset")
}
// The `super` call edge must still be recorded (dead-code marks the ancestor used).
if !hasCall(f, "hood_id") {
t.Errorf("super should still record a call edge to the same-named ancestor; relations = %v", f.Relations)
}
}
func TestRbComplexity_DelegationIsNotRecursion(t *testing.T) {
// A same-named call on an explicit, non-self receiver (SimpleDelegator/decorator,
// `@delegate.render`) is a call on a DIFFERENT object, not self-recursion. A
// receiverless self-call still is.
delegating := `class Presenter
def render(x)
@delegate_object.render(x)
end
end
`
f := symbolsByName(extractFileAST([]byte(delegating), "app/presenter.rb", false, false))["Presenter#render"]
if v, _ := f.Props["recursive_self"].(bool); v {
t.Errorf("recursive_self = true for a delegated same-named call; want unset")
}
genuine := `class Walker
def render(node)
render(node.child)
end
end
`
g := symbolsByName(extractFileAST([]byte(genuine), "app/walker.rb", false, false))["Walker#render"]
if v, ok := g.Props["recursive_self"].(bool); !ok || !v {
t.Errorf("recursive_self = %v (ok=%v) for a receiverless self-call; want true", g.Props["recursive_self"], ok)
}
}
func TestRbComplexity_SelfClassSiblingIsNotRecursion(t *testing.T) {
// An instance method delegating to the same-named CLASS method
// (`self.class.photo_url`) calls a DIFFERENT method — not recursion. But plain
// `self.foo` (same object, same method) still is.
sibling := `class Presenter
def photo_url(user)
self.class.photo_url(user)
end
end
`
f := symbolsByName(extractFileAST([]byte(sibling), "app/presenter.rb", false, false))["Presenter#photo_url"]
if v, _ := f.Props["recursive_self"].(bool); v {
t.Errorf("recursive_self = true for a self.class sibling delegation; want unset")
}
selfDispatch := `class Worker
def run
self.run
end
end
`
g := symbolsByName(extractFileAST([]byte(selfDispatch), "app/worker.rb", false, false))["Worker#run"]
if v, ok := g.Props["recursive_self"].(bool); !ok || !v {
t.Errorf("recursive_self = %v (ok=%v) for `self.run`; want true", g.Props["recursive_self"], ok)
}
}
func TestRbComplexity_TryDispatchOnOtherReceiverIsNotRecursion(t *testing.T) {
// `object.try(:ben?)` dispatches `ben?` to a DIFFERENT object — not recursion.
src := `class AuthorType
def ben?
!!object.try(:ben?)
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/author_type.rb", false, false))["AuthorType#ben?"]
if v, _ := f.Props["recursive_self"].(bool); v {
t.Errorf("recursive_self = true for `object.try(:ben?)` on another receiver; want unset")
}
}
func TestRbComplexity_InLoopAssociationRead(t *testing.T) {
// The classic N+1: a no-arg association read inside an iterator block. It must
// land in calls_in_loop (for the perf metric) but NOT become a graph edge, and
// a plain attribute read (u.name) must be excluded by the cheap-method stoplist.
src := `class Report
def run(users)
users.each do |u|
u.posts
puts u.name
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/report.rb", false, false))["Report#run"]
cil := rbStrSlice(f, "calls_in_loop")
if !rbContains(cil, "posts") {
t.Errorf("calls_in_loop = %v, want to contain posts (association read)", cil)
}
if rbContains(cil, "name") {
t.Errorf("calls_in_loop = %v, must NOT contain name (cheap attribute)", cil)
}
// Metrics-only: no RelCalls graph edge for the no-arg instance call.
if hasCall(f, "posts") || hasCall(f, "u.posts") {
t.Errorf("u.posts must not become a call edge; relations=%v", f.Relations)
}
}
func TestRbComplexity_InLoopAssociationChain(t *testing.T) {
// u.posts.count — the inner association read is captured via normal recursion.
src := `class Report
def run(users)
users.each { |u| total += u.posts.count }
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/report.rb", false, false))["Report#run"]
if cil := rbStrSlice(f, "calls_in_loop"); !rbContains(cil, "posts") {
t.Errorf("calls_in_loop = %v, want to contain posts", cil)
}
}
func TestRbAssociationFactCarriesName(t *testing.T) {
src := `class User < ApplicationRecord
has_many :posts
belongs_to :account
end
`
result := extractFileAST([]byte(src), "app/models/user.rb", true, false)
var gotPosts, gotAccount bool
for _, f := range result {
if f.Kind != facts.KindDependency {
continue
}
if _, ok := f.Props["association_kind"]; !ok {
continue
}
switch f.Props["association"] {
case "posts":
gotPosts = true
case "account":
gotAccount = true
}
}
if !gotPosts || !gotAccount {
t.Errorf("association facts missing Props[\"association\"] (posts=%v account=%v)", gotPosts, gotAccount)
}
}
func TestRbComplexity_NonIteratorBlockNotLoop(t *testing.T) {
// transaction takes a block but runs it once — it is NOT a loop.
src := `class Worker
def run
ActiveRecord::Base.transaction do
persist
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/worker.rb", true, false))["Worker#run"]
if _, present := f.Props["loop_depth"]; present {
t.Errorf("transaction block must not be a loop; loop_depth=%v", f.Props["loop_depth"])
}
if cil := rbStrSlice(f, "calls_in_loop"); rbContains(cil, "persist") {
t.Errorf("calls_in_loop = %v, must NOT contain persist (block runs once)", cil)
}
if !hasCall(f, "persist") {
t.Errorf("persist should still be a call edge; relations=%v", f.Relations)
}
}
func TestRbComplexity_BlockParamNotCall(t *testing.T) {
// A block parameter is a local, not a method call — even when referenced bare
// (as an argument value) and even when its name matches an association. This is
// the dominant N+1 false positive: `each do |user| … user … end`. A genuine
// association read on the block var (user.posts) must still be captured, proving
// the fix is surgical.
src := `class Worker
def run(users)
users.each do |user|
notify(user)
user.posts
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/worker.rb", false, false))["Worker#run"]
cil := rbStrSlice(f, "calls_in_loop")
if rbContains(cil, "user") {
t.Errorf("calls_in_loop = %v, must NOT contain user (block-local variable)", cil)
}
if !rbContains(cil, "notify") {
t.Errorf("calls_in_loop = %v, want to contain notify", cil)
}
if !rbContains(cil, "posts") {
t.Errorf("calls_in_loop = %v, want to contain posts (real association read on block var)", cil)
}
}
func TestRbComplexity_FindInBatchesNotElementLoop(t *testing.T) {
// find_in_batches yields a batch (array); the inner .map over that batch is the
// real per-element loop. The pair must score loop_depth 1 (a single O(n) pass),
// not 2 — otherwise a batched reindex is mislabeled O(n²).
src := `class Reindex
def run(model)
model.find_in_batches do |batch|
batch.map { |obj| present(obj) }
end
end
end
`
f := symbolsByName(extractFileAST([]byte(src), "app/reindex.rb", false, false))["Reindex#run"]
if got := rbIntProp(t, f, "loop_depth"); got != 1 {
t.Errorf("loop_depth = %d, want 1 (find_in_batches yields batches, not elements)", got)
}
if cil := rbStrSlice(f, "calls_in_loop"); !rbContains(cil, "present") {
t.Errorf("calls_in_loop = %v, want to contain present", cil)
}
}