forked from luontola/gospec
/
location_test.go
55 lines (50 loc) · 1.92 KB
/
location_test.go
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
// Copyright © 2009-2011 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0
package gospec
import (
"fmt"
"github.com/orfjackal/nanospec.go/src/nanospec"
"runtime"
)
func LocationSpec(c nanospec.Context) {
c.Specify("Location of the current method can be found", func() {
loc := currentLocation() // line 16
c.Expect(loc.FileName()).Equals("location_test.go")
c.Expect(loc.Line()).Equals(16)
})
c.Specify("The line number is that of the call instruction; not where the call will return to", func() {
// This indirection is needed to reproduce the off-by-one issue, because
// there must be no instructions on the same line after the call itself.
var loc *Location
f := func() {
loc = callerLocation()
}
f() // line 27; call returns to line 28
c.Expect(loc.Line()).Equals(27)
})
c.Specify("Location of the calling method can be found", func() {
loc := callerLocation()
c.Expect(loc.FileName()).Equals("context.go")
})
c.Specify("The name of the method is provided", func() {
loc := methodWhereLocationIsCalled()
c.Expect(loc.Name()).Equals(fmt.Sprintf("%v.methodWhereLocationIsCalled", pkgPath))
})
c.Specify("Calls to newLocation are synced with the helper methods", func() {
c.Expect(newLocation(0).Name()).Equals(currentLocation().Name())
c.Expect(newLocation(0).File()).Equals(currentLocation().File())
c.Expect(newLocation(1).Name()).Equals(callerLocation().Name())
c.Expect(newLocation(1).File()).Equals(callerLocation().File())
})
c.Specify("Program Counters can be converted to Locations", func() {
expectedLine := currentLocation().Line() + 1
pc, _, _, _ := runtime.Caller(0)
loc := locationForPC(pc)
c.Expect(loc.FileName()).Equals("location_test.go")
c.Expect(loc.Line()).Equals(expectedLine)
})
}
func methodWhereLocationIsCalled() *Location {
return currentLocation()
}