github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/cmd/internal/src/pos_test.go (about) 1 // Copyright 2016 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 5 package src 6 7 import ( 8 "fmt" 9 "testing" 10 ) 11 12 func TestPos(t *testing.T) { 13 f0 := NewFileBase("", "") 14 f1 := NewFileBase("f1", "f1") 15 f2 := NewLinePragmaBase(Pos{}, "f2", "f2", 10) 16 f3 := NewLinePragmaBase(MakePos(f1, 10, 1), "f3", "f3", 100) 17 f4 := NewLinePragmaBase(MakePos(f3, 10, 1), "f4", "f4", 100) 18 19 // line directives from issue #19392 20 fp := NewFileBase("p.go", "p.go") 21 fc := NewLinePragmaBase(MakePos(fp, 3, 0), "c.go", "c.go", 10) 22 ft := NewLinePragmaBase(MakePos(fp, 6, 0), "t.go", "t.go", 20) 23 fv := NewLinePragmaBase(MakePos(fp, 9, 0), "v.go", "v.go", 30) 24 ff := NewLinePragmaBase(MakePos(fp, 12, 0), "f.go", "f.go", 40) 25 26 for _, test := range []struct { 27 pos Pos 28 string string 29 30 // absolute info 31 filename string 32 line, col uint 33 34 // relative info 35 relFilename string 36 relLine uint 37 }{ 38 {Pos{}, "<unknown line number>", "", 0, 0, "", 0}, 39 {MakePos(nil, 2, 3), ":2:3", "", 2, 3, "", 2}, 40 {MakePos(f0, 2, 3), ":2:3", "", 2, 3, "", 2}, 41 {MakePos(f1, 1, 1), "f1:1:1", "f1", 1, 1, "f1", 1}, 42 {MakePos(f2, 7, 10), "f2:16[:7:10]", "", 7, 10, "f2", 16}, 43 {MakePos(f3, 12, 7), "f3:101[f1:12:7]", "f1", 12, 7, "f3", 101}, 44 {MakePos(f4, 25, 1), "f4:114[f3:25:1]", "f3", 25, 1, "f4", 114}, 45 46 // positions from issue #19392 47 {MakePos(fc, 4, 0), "c.go:10[p.go:4:0]", "p.go", 4, 0, "c.go", 10}, 48 {MakePos(ft, 7, 0), "t.go:20[p.go:7:0]", "p.go", 7, 0, "t.go", 20}, 49 {MakePos(fv, 10, 0), "v.go:30[p.go:10:0]", "p.go", 10, 0, "v.go", 30}, 50 {MakePos(ff, 13, 0), "f.go:40[p.go:13:0]", "p.go", 13, 0, "f.go", 40}, 51 } { 52 pos := test.pos 53 if got := pos.String(); got != test.string { 54 t.Errorf("%s: got %q", test.string, got) 55 } 56 57 // absolute info 58 if got := pos.Filename(); got != test.filename { 59 t.Errorf("%s: got filename %q; want %q", test.string, got, test.filename) 60 } 61 if got := pos.Line(); got != test.line { 62 t.Errorf("%s: got line %d; want %d", test.string, got, test.line) 63 } 64 if got := pos.Col(); got != test.col { 65 t.Errorf("%s: got col %d; want %d", test.string, got, test.col) 66 } 67 68 // relative info 69 if got := pos.RelFilename(); got != test.relFilename { 70 t.Errorf("%s: got relFilename %q; want %q", test.string, got, test.relFilename) 71 } 72 if got := pos.RelLine(); got != test.relLine { 73 t.Errorf("%s: got relLine %d; want %d", test.string, got, test.relLine) 74 } 75 } 76 } 77 78 func TestPredicates(t *testing.T) { 79 b1 := NewFileBase("b1", "b1") 80 b2 := NewFileBase("b2", "b2") 81 for _, test := range []struct { 82 p, q Pos 83 known, before, after bool 84 }{ 85 {NoPos, NoPos, false, false, false}, 86 {NoPos, MakePos(nil, 1, 0), false, true, false}, 87 {MakePos(b1, 0, 0), NoPos, true, false, true}, 88 {MakePos(nil, 1, 0), NoPos, true, false, true}, 89 90 {MakePos(nil, 1, 1), MakePos(nil, 1, 1), true, false, false}, 91 {MakePos(nil, 1, 1), MakePos(nil, 1, 2), true, true, false}, 92 {MakePos(nil, 1, 2), MakePos(nil, 1, 1), true, false, true}, 93 {MakePos(nil, 123, 1), MakePos(nil, 1, 123), true, false, true}, 94 95 {MakePos(b1, 1, 1), MakePos(b1, 1, 1), true, false, false}, 96 {MakePos(b1, 1, 1), MakePos(b1, 1, 2), true, true, false}, 97 {MakePos(b1, 1, 2), MakePos(b1, 1, 1), true, false, true}, 98 {MakePos(b1, 123, 1), MakePos(b1, 1, 123), true, false, true}, 99 100 {MakePos(b1, 1, 1), MakePos(b2, 1, 1), true, true, false}, 101 {MakePos(b1, 1, 1), MakePos(b2, 1, 2), true, true, false}, 102 {MakePos(b1, 1, 2), MakePos(b2, 1, 1), true, true, false}, 103 {MakePos(b1, 123, 1), MakePos(b2, 1, 123), true, true, false}, 104 105 // special case: unknown column (column too large to represent) 106 {MakePos(nil, 1, colMax+10), MakePos(nil, 1, colMax+20), true, false, false}, 107 } { 108 if got := test.p.IsKnown(); got != test.known { 109 t.Errorf("%s known: got %v; want %v", test.p, got, test.known) 110 } 111 if got := test.p.Before(test.q); got != test.before { 112 t.Errorf("%s < %s: got %v; want %v", test.p, test.q, got, test.before) 113 } 114 if got := test.p.After(test.q); got != test.after { 115 t.Errorf("%s > %s: got %v; want %v", test.p, test.q, got, test.after) 116 } 117 } 118 } 119 120 func TestLico(t *testing.T) { 121 for _, test := range []struct { 122 x lico 123 string string 124 line, col uint 125 }{ 126 {0, ":0:0", 0, 0}, 127 {makeLico(0, 0), ":0:0", 0, 0}, 128 {makeLico(0, 1), ":0:1", 0, 1}, 129 {makeLico(1, 0), ":1:0", 1, 0}, 130 {makeLico(1, 1), ":1:1", 1, 1}, 131 {makeLico(2, 3), ":2:3", 2, 3}, 132 {makeLico(lineMax, 1), fmt.Sprintf(":%d:1", lineMax), lineMax, 1}, 133 {makeLico(lineMax+1, 1), fmt.Sprintf(":%d:1", lineMax), lineMax, 1}, // line too large, stick with max. line 134 {makeLico(1, colMax), ":1", 1, colMax}, 135 {makeLico(1, colMax+1), ":1", 1, 0}, // column too large 136 {makeLico(lineMax+1, colMax+1), fmt.Sprintf(":%d", lineMax), lineMax, 0}, 137 } { 138 x := test.x 139 if got := format("", x.Line(), x.Col(), true); got != test.string { 140 t.Errorf("%s: got %q", test.string, got) 141 } 142 } 143 }