Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix and suffix assertions #629

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*.a
*.so

# Binaries
codegen

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should not be bundled in this PR.

# Folders
_obj
_test
Expand Down
2 changes: 2 additions & 0 deletions .travis.gofmt.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

echo ".travis.gofmt.sh"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should not be bundled in this PR.

if [ -n "$(gofmt -l .)" ]; then
echo "Go code is not formatted:"
gofmt -d .
Expand Down
14 changes: 10 additions & 4 deletions .travis.gogenerate.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/bin/bash

if [[ "$TRAVIS_GO_VERSION" =~ ^1\.[45](\..*)?$ ]]; then
exit 0
fi
set -e
set -o pipefail

echo ".travis.gogenerate.sh"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should not be bundled in this PR.


go build -o codegen ./_codegen
go get github.com/ernesto-jimenez/gogen/imports
go build -o codegen ./_codegen
go generate ./...
if [ -n "$(git diff)" ]; then
if [ -n "$(git diff assert/ mock/ require/ suite/)" ]; then
echo "Go generate had not been run"
git diff
exit 1
else
echo "Go generate had been run"
fi

5 changes: 4 additions & 1 deletion .travis.govet.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/bin/bash

echo ".travis.govet.sh"

cd "$(dirname $0)"
DIRS=". assert require mock _codegen"
DIRS=". assert require mock suite _codegen"
set -e
for subdir in $DIRS; do
pushd $subdir
go vet
popd
done

17 changes: 12 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ matrix:
env: GO111MODULE=off
- go: "1.11.x"
env: GO111MODULE=on
- go: "1.12.x"
env: GO111MODULE=off
- go: "1.12.x"
env: GO111MODULE=on
- go: tip
script:
- ./.travis.gogenerate.sh
- ./.travis.gofmt.sh
- ./.travis.govet.sh
- go test -v -race $(go list ./... | grep -v vendor)
env: GO111MODULE=off
- go: tip
env: GO111MODULE=on
script:
- ./.travis.gogenerate.sh
- ./.travis.gofmt.sh
- ./.travis.govet.sh
- go test -v -race $(go list ./... | grep -v vendor)
5 changes: 5 additions & 0 deletions _codegen/importer_new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build go1.9

package main

const SOURCE_IMPORTER = true
5 changes: 5 additions & 0 deletions _codegen/importer_old.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !go1.9

package main

const SOURCE_IMPORTER = false
17 changes: 13 additions & 4 deletions _codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"os"
"path"
"regexp"
"runtime/debug"
"strings"
"text/template"

"github.com/ernesto-jimenez/gogen/imports"
)

var (
pkg = flag.String("assert-path", "github.com/stretchr/testify/assert", "Path to the assert package")
pkg = flag.String("assert-path", ".", "Path to the assert package")
includeF = flag.Bool("include-format-funcs", false, "include format functions such as Errorf and Equalf")
outputPkg = flag.String("output-package", "", "package for the resulting code")
tmplFile = flag.String("template", "", "What file to load the function template from")
Expand All @@ -43,12 +44,12 @@ func main() {
log.Fatal(err)
}

importer, funcs, err := analyzeCode(scope, docs)
imp, funcs, err := analyzeCode(scope, docs)
if err != nil {
log.Fatal(err)
}

if err := generateCode(importer, funcs); err != nil {
if err := generateCode(imp, funcs); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -193,14 +194,22 @@ func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) {
fileList[i] = f
}

var imp types.Importer
if SOURCE_IMPORTER {
imp = importer.For("source", nil)
} else {
imp = importer.Default()
}

cfg := types.Config{
Importer: importer.Default(),
Importer: imp,
}
info := types.Info{
Defs: make(map[*ast.Ident]types.Object),
}
tp, err := cfg.Check(pkg, fset, fileList, &info)
if err != nil {
log.Print(string(debug.Stack()))
return nil, nil, err
}

Expand Down
40 changes: 40 additions & 0 deletions assert/assertion_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,30 @@ func HTTPSuccessf(t TestingT, handler http.HandlerFunc, method string, url strin
return HTTPSuccess(t, handler, method, url, values, append([]interface{}{msg}, args...)...)
}

// HasPrefixf asserts that the specified string or list(array, slice...) begins with the
// specified substring or sequence of elements.
//
// assert.HasPrefixf(t, "Hello World", "Hello", "error message %s", "formatted")
// assert.HasPrefixf(t, ["Hello", "there", "World"], ["Hello", "there"], "error message %s", "formatted")
func HasPrefixf(t TestingT, s interface{}, prefix interface{}, msg string, args ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return HasPrefix(t, s, prefix, append([]interface{}{msg}, args...)...)
}

// HasSuffixf asserts that the specified string or list(array, slice...) ends with the
// specified substring or sequence of elements.
//
// assert.HasSuffixf(t, "Hello World", "World", "error message %s", "formatted")
// assert.HasSuffixf(t, ["Hello", "there", "World"], ["there", "World"], "error message %s", "formatted")
func HasSuffixf(t TestingT, s interface{}, suffix interface{}, msg string, args ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return HasSuffix(t, s, suffix, append([]interface{}{msg}, args...)...)
}

// Implementsf asserts that an object is implemented by the specified interface.
//
// assert.Implementsf(t, (*MyInterface, "error message %s", "formatted")(nil), new(MyObject))
Expand Down Expand Up @@ -412,6 +436,22 @@ func NotEqualf(t TestingT, expected interface{}, actual interface{}, msg string,
return NotEqual(t, expected, actual, append([]interface{}{msg}, args...)...)
}

// NotHasPrefixf asserts the negation of HasPrefix.
func NotHasPrefixf(t TestingT, s interface{}, prefix interface{}, msg string, args ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return NotHasPrefix(t, s, prefix, append([]interface{}{msg}, args...)...)
}

// NotHasSuffixf asserts the negation of HasSuffix.
func NotHasSuffixf(t TestingT, s interface{}, suffix interface{}, msg string, args ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return NotHasSuffix(t, s, suffix, append([]interface{}{msg}, args...)...)
}

// NotNilf asserts that the specified object is not nil.
//
// assert.NotNilf(t, err, "error message %s", "formatted")
Expand Down
80 changes: 80 additions & 0 deletions assert/assertion_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,54 @@ func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url s
return HTTPSuccessf(a.t, handler, method, url, values, msg, args...)
}

// HasPrefix asserts that the specified string or list(array, slice...) begins with the
// specified substring or sequence of elements.
//
// a.HasPrefix("Hello World", "Hello")
// a.HasPrefix(["Hello", "there", "World"], ["Hello", "there"])
func (a *Assertions) HasPrefix(s interface{}, prefix interface{}, msgAndArgs ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return HasPrefix(a.t, s, prefix, msgAndArgs...)
}

// HasPrefixf asserts that the specified string or list(array, slice...) begins with the
// specified substring or sequence of elements.
//
// a.HasPrefixf("Hello World", "Hello", "error message %s", "formatted")
// a.HasPrefixf(["Hello", "there", "World"], ["Hello", "there"], "error message %s", "formatted")
func (a *Assertions) HasPrefixf(s interface{}, prefix interface{}, msg string, args ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return HasPrefixf(a.t, s, prefix, msg, args...)
}

// HasSuffix asserts that the specified string or list(array, slice...) ends with the
// specified substring or sequence of elements.
//
// a.HasSuffix("Hello World", "World")
// a.HasSuffix(["Hello", "there", "World"], ["there", "World"])
func (a *Assertions) HasSuffix(s interface{}, suffix interface{}, msgAndArgs ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return HasSuffix(a.t, s, suffix, msgAndArgs...)
}

// HasSuffixf asserts that the specified string or list(array, slice...) ends with the
// specified substring or sequence of elements.
//
// a.HasSuffixf("Hello World", "World", "error message %s", "formatted")
// a.HasSuffixf(["Hello", "there", "World"], ["there", "World"], "error message %s", "formatted")
func (a *Assertions) HasSuffixf(s interface{}, suffix interface{}, msg string, args ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return HasSuffixf(a.t, s, suffix, msg, args...)
}

// Implements asserts that an object is implemented by the specified interface.
//
// a.Implements((*MyInterface)(nil), new(MyObject))
Expand Down Expand Up @@ -813,6 +861,38 @@ func (a *Assertions) NotEqualf(expected interface{}, actual interface{}, msg str
return NotEqualf(a.t, expected, actual, msg, args...)
}

// NotHasPrefix asserts the negation of HasPrefix.
func (a *Assertions) NotHasPrefix(s interface{}, prefix interface{}, msgAndArgs ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return NotHasPrefix(a.t, s, prefix, msgAndArgs...)
}

// NotHasPrefixf asserts the negation of HasPrefix.
func (a *Assertions) NotHasPrefixf(s interface{}, prefix interface{}, msg string, args ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return NotHasPrefixf(a.t, s, prefix, msg, args...)
}

// NotHasSuffix asserts the negation of HasSuffix.
func (a *Assertions) NotHasSuffix(s interface{}, suffix interface{}, msgAndArgs ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return NotHasSuffix(a.t, s, suffix, msgAndArgs...)
}

// NotHasSuffixf asserts the negation of HasSuffix.
func (a *Assertions) NotHasSuffixf(s interface{}, suffix interface{}, msg string, args ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return NotHasSuffixf(a.t, s, suffix, msg, args...)
}

// NotNil asserts that the specified object is not nil.
//
// a.NotNil(err)
Expand Down
Loading