parent
b873d848a1
commit
83e720e5a2
@ -0,0 +1,5 @@
|
||||
# Integration Test Util
|
||||
|
||||
Based off of Deno's internal integration test utilities. This is meant to test
|
||||
external systems that are hard to mock or can't rely fully on dependency
|
||||
injection.
|
@ -0,0 +1,59 @@
|
||||
package integrationtest
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const WILDCARD = "[WILDCARD]"
|
||||
|
||||
func AssertMatchesText(t *testing.T, expected, actual string) {
|
||||
if !strings.Contains(expected, WILDCARD) {
|
||||
assert.Equal(t, expected, actual)
|
||||
} else if !wildcardMatch(expected, actual) {
|
||||
t.Errorf("texts do not match. expected \"%s\", got \"%s\"", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func wildcardMatch(expected, actual string) bool {
|
||||
return patternMatch(expected, actual, WILDCARD)
|
||||
}
|
||||
|
||||
func patternMatch(expected, actual, pattern string) bool {
|
||||
if expected == pattern {
|
||||
return true
|
||||
}
|
||||
|
||||
parts := strings.Split(expected, pattern)
|
||||
if len(parts) == 1 {
|
||||
return expected == actual
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(actual, parts[0]) {
|
||||
return false
|
||||
}
|
||||
|
||||
if lines := strings.Split(expected, "\n"); len(lines) > 0 && lines[0] == pattern {
|
||||
actual = "\n" + actual
|
||||
}
|
||||
|
||||
t := []string{actual[:len(parts[0])], actual[len(parts[0]):]}
|
||||
for i, part := range parts {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if i == len(parts)-1 && (part == "" || part == "\n") {
|
||||
return true
|
||||
}
|
||||
|
||||
if found := strings.Index(t[1], part); found != -1 {
|
||||
t = []string{t[1][:found+len(part)], t[1][found+len(part):]}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return t[1] == ""
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
package integrationtest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_patternMatch(t *testing.T) {
|
||||
type args struct {
|
||||
expected string
|
||||
actual string
|
||||
pattern string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "foobarbaz matches",
|
||||
args: args{
|
||||
expected: "foo[BAR]baz",
|
||||
actual: "foobarbaz",
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "foobarbaz does not match",
|
||||
args: args{
|
||||
expected: "foo[BAR]baz",
|
||||
actual: "foobazbar",
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "wildcard matches but not rest of string",
|
||||
args: args{
|
||||
expected: "foo[BAR]baz",
|
||||
actual: "foobarfizz",
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "just wildcard",
|
||||
args: args{
|
||||
expected: "[BAR]",
|
||||
actual: "foobarbaz",
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "prefix",
|
||||
args: args{
|
||||
expected: "prefix[BAR]",
|
||||
actual: "prefixbarbaz",
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "prefix does not match",
|
||||
args: args{
|
||||
expected: "prefix[BAR]",
|
||||
actual: "somethingelsebarbaz",
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "suffix",
|
||||
args: args{
|
||||
expected: "[BAR]suffix",
|
||||
actual: "foobarbazsuffix",
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "no pattern",
|
||||
args: args{
|
||||
expected: "exact match",
|
||||
actual: "exact match",
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "multiline",
|
||||
args: args{
|
||||
expected: `first line
|
||||
[BAR]
|
||||
last line`,
|
||||
actual: `first line
|
||||
second line
|
||||
last line`,
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "multiline prefix",
|
||||
args: args{
|
||||
expected: `[BAR]
|
||||
first line
|
||||
last line`,
|
||||
actual: `[BAR]
|
||||
first line
|
||||
last line`,
|
||||
pattern: "[BAR]",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := patternMatch(tt.args.expected, tt.args.actual, tt.args.pattern); got != tt.want {
|
||||
t.Errorf("patternMatch() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue