1// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package obj
6
7import (
8	"cmd/internal/src"
9	"fmt"
10	"testing"
11)
12
13func TestGetFileSymbolAndLine(t *testing.T) {
14	ctxt := new(Link)
15	ctxt.hash = make(map[string]*LSym)
16	ctxt.statichash = make(map[string]*LSym)
17
18	afile := src.NewFileBase("a.go", "a.go")
19	bfile := src.NewFileBase("b.go", "/foo/bar/b.go")
20	lfile := src.NewLinePragmaBase(src.MakePos(afile, 8, 1), "linedir", "linedir", 100, 1)
21
22	var tests = []struct {
23		pos  src.Pos
24		want string
25	}{
26		{src.NoPos, "??:0"},
27		{src.MakePos(afile, 1, 0), "a.go:1"},
28		{src.MakePos(afile, 2, 0), "a.go:2"},
29		{src.MakePos(bfile, 10, 4), "/foo/bar/b.go:10"},
30		{src.MakePos(lfile, 10, 0), "linedir:102"}, // 102 == 100 + (10 - (7+1))
31	}
32
33	for _, test := range tests {
34		fileIndex, line := ctxt.getFileIndexAndLine(ctxt.PosTable.XPos(test.pos))
35
36		file := "??"
37		if fileIndex >= 0 {
38			file = ctxt.PosTable.FileTable()[fileIndex]
39		}
40		got := fmt.Sprintf("%s:%d", file, line)
41
42		if got != test.want {
43			t.Errorf("ctxt.getFileSymbolAndLine(%v) = %q, want %q", test.pos, got, test.want)
44		}
45	}
46}
47