2018-03-06 05:15:18 -05:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-05-17 07:11:59 -04:00
|
|
|
// Assert checks output lines at specified locations
|
2018-03-06 05:15:18 -05:00
|
|
|
func Assert(t *testing.T, actual string, expectedLines map[int]func(string) error) {
|
2018-04-18 11:02:21 -04:00
|
|
|
t.Helper()
|
2018-03-06 05:15:18 -05:00
|
|
|
for i, line := range strings.Split(actual, "\n") {
|
|
|
|
cmp, ok := expectedLines[i]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := cmp(line); err != nil {
|
|
|
|
t.Errorf("line %d: %s", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.Failed() {
|
|
|
|
t.Log(actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 07:11:59 -04:00
|
|
|
// Prefix returns whether the line has the specified string as prefix
|
2018-03-06 05:15:18 -05:00
|
|
|
func Prefix(expected string) func(string) error {
|
|
|
|
return func(actual string) error {
|
|
|
|
if strings.HasPrefix(actual, expected) {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-17 07:11:59 -04:00
|
|
|
return errors.Errorf("expected %q to start with %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suffix returns whether the line has the specified string as suffix
|
|
|
|
func Suffix(expected string) func(string) error {
|
|
|
|
return func(actual string) error {
|
|
|
|
if strings.HasSuffix(actual, expected) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Errorf("expected %q to end with %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains returns whether the line contains the specified string
|
|
|
|
func Contains(expected string) func(string) error {
|
|
|
|
return func(actual string) error {
|
|
|
|
if strings.Contains(actual, expected) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Errorf("expected %q to contain %q", actual, expected)
|
2018-03-06 05:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals returns wether the line is the same as the specified string
|
|
|
|
func Equals(expected string) func(string) error {
|
|
|
|
return func(actual string) error {
|
|
|
|
if expected == actual {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-17 07:11:59 -04:00
|
|
|
return errors.Errorf("got %q, expected %q", actual, expected)
|
2018-03-06 05:15:18 -05:00
|
|
|
}
|
|
|
|
}
|