-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:matryer/is
- Loading branch information
Showing
9 changed files
with
236 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,40 @@ | ||
os: | ||
- linux | ||
- osx | ||
- windows | ||
|
||
language: go | ||
|
||
sudo: required | ||
|
||
go: | ||
# "1.x" always refers to the latest Go version, inc. the patch release. | ||
# e.g. "1.x" is 1.13 until 1.13.1 is available. | ||
- 1.x | ||
- 1.6.x | ||
- 1.7.x | ||
- 1.8.x | ||
- 1.9.x | ||
- 1.10.x | ||
- 1.11.x | ||
- 1.12.x | ||
- 1.13.x | ||
- 1.14.x | ||
- tip | ||
|
||
env: | ||
- GIMME_OS=linux GIMME_ARCH=amd64 | ||
- GIMME_OS=darwin GIMME_ARCH=amd64 | ||
- GIMME_OS=windows GIMME_ARCH=amd64 | ||
matrix: | ||
allow_failures: | ||
- os: windows | ||
go: tip | ||
exclude: | ||
# OSX 1.6.4 is not present in travis. | ||
# https://github.com/travis-ci/travis-ci/issues/10309 | ||
- go: 1.6.x | ||
os: osx | ||
|
||
install: | ||
- go get -d -v ./... | ||
- go get -d -v ./... | ||
|
||
script: | ||
- go build -v ./... | ||
- go build -v ./... | ||
- go test ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// +build go1.7 | ||
|
||
package is | ||
|
||
import ( | ||
"regexp" | ||
"runtime" | ||
) | ||
|
||
// Helper marks the calling function as a test helper function. | ||
// When printing file and line information, that function will be skipped. | ||
// | ||
// Available with Go 1.7 and later. | ||
func (is *I) Helper() { | ||
is.helpers[callerName(1)] = struct{}{} | ||
} | ||
|
||
// callerName gives the function name (qualified with a package path) | ||
// for the caller after skip frames (where 0 means the current function). | ||
func callerName(skip int) string { | ||
// Make room for the skip PC. | ||
var pc [1]uintptr | ||
n := runtime.Callers(skip+2, pc[:]) // skip + runtime.Callers + callerName | ||
if n == 0 { | ||
panic("is: zero callers found") | ||
} | ||
frames := runtime.CallersFrames(pc[:n]) | ||
frame, _ := frames.Next() | ||
return frame.Function | ||
} | ||
|
||
// The maximum number of stack frames to go through when skipping helper functions for | ||
// the purpose of decorating log messages. | ||
const maxStackLen = 50 | ||
|
||
var reIsSourceFile = regexp.MustCompile("is(-1.7)?\\.go$") | ||
|
||
func (is *I) callerinfo() (path string, line int, ok bool) { | ||
var pc [maxStackLen]uintptr | ||
// Skip two extra frames to account for this function | ||
// and runtime.Callers itself. | ||
n := runtime.Callers(2, pc[:]) | ||
if n == 0 { | ||
panic("is: zero callers found") | ||
} | ||
frames := runtime.CallersFrames(pc[:n]) | ||
var firstFrame, frame runtime.Frame | ||
for more := true; more; { | ||
frame, more = frames.Next() | ||
if reIsSourceFile.MatchString(frame.File) { | ||
continue | ||
} | ||
if firstFrame.PC == 0 { | ||
firstFrame = frame | ||
} | ||
if _, ok := is.helpers[frame.Function]; ok { | ||
// Frame is inside a helper function. | ||
continue | ||
} | ||
return frame.File, frame.Line, true | ||
} | ||
// If no "non-helper" frame is found, the first non is frame is returned. | ||
return firstFrame.File, firstFrame.Line, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// +build go1.7 | ||
|
||
package is | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// TestSubtests ensures subtests work as expected. | ||
// https://github.com/matryer/is/issues/1 | ||
func TestSubtests(t *testing.T) { | ||
t.Run("sub1", func(t *testing.T) { | ||
is := New(t) | ||
is.Equal(1+1, 2) | ||
}) | ||
} | ||
|
||
func TestHelper(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
helper bool | ||
expectedFilename string | ||
}{ | ||
{ | ||
name: "without helper", | ||
helper: false, | ||
expectedFilename: "is_helper_test.go", | ||
}, | ||
{ | ||
name: "with helper", | ||
helper: true, | ||
expectedFilename: "is-1.7_test.go", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tt := &mockT{} | ||
is := NewRelaxed(tt) | ||
|
||
var buf bytes.Buffer | ||
is.out = &buf | ||
helper(is, tc.helper) | ||
actual := buf.String() | ||
t.Log(actual) | ||
if !strings.Contains(actual, tc.expectedFilename) { | ||
t.Errorf("string does not contain correct filename: %s", actual) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// +build !go1.7 | ||
|
||
package is | ||
|
||
import ( | ||
"regexp" | ||
"runtime" | ||
) | ||
|
||
var reIsSourceFile = regexp.MustCompile("is(-before-1.7)?\\.go$") | ||
|
||
func (is *I) callerinfo() (path string, line int, ok bool) { | ||
for i := 0; ; i++ { | ||
_, path, line, ok = runtime.Caller(i) | ||
if !ok { | ||
return | ||
} | ||
if reIsSourceFile.MatchString(path) { | ||
continue | ||
} | ||
return path, line, true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// +build go1.7 | ||
|
||
package is | ||
|
||
func helper(is *I, helper bool) { | ||
if helper { | ||
is.Helper() | ||
} | ||
is.True(false) | ||
} |
Oops, something went wrong.