package inspect import ( "runtime" "testing" ) func TestSharedFrames(t *testing.T) { a := runtime.Frame{PC: 1, Function: "a", File: "a.go", Line: 1} b := runtime.Frame{PC: 2, Function: "b", File: "b.go", Line: 2} c := runtime.Frame{PC: 3, Function: "c", File: "c.go", Line: 3} inline := b inline.Function = "inlined" otherLine := b otherLine.Line++ for _, tt := range []struct { name string stack, parent []runtime.Frame want int }{ {"partial", []runtime.Frame{a, b}, []runtime.Frame{c, b}, 1}, {"identical", []runtime.Frame{a, b}, []runtime.Frame{a, b}, 2}, {"child shorter", []runtime.Frame{b}, []runtime.Frame{a, b}, 1}, {"parent shorter", []runtime.Frame{a, b}, []runtime.Frame{b}, 1}, {"different suffix", []runtime.Frame{a, b}, []runtime.Frame{a, c}, 0}, {"empty parent", []runtime.Frame{a}, nil, 0}, {"empty child", nil, []runtime.Frame{a}, 0}, {"inlined call", []runtime.Frame{b}, []runtime.Frame{inline}, 0}, {"different line", []runtime.Frame{b}, []runtime.Frame{otherLine}, 0}, {"recursion", []runtime.Frame{a, a, b}, []runtime.Frame{a, b}, 2}, } { t.Run(tt.name, func(t *testing.T) { if got := sharedFrames(tt.stack, tt.parent); got != tt.want { t.Fatalf("got %d shared frames, want %d", got, tt.want) } }) } }