yq/test/utils.go

61 lines
1.7 KiB
Go
Raw Permalink Normal View History

package test
2015-10-10 08:18:54 +00:00
import (
"bufio"
"bytes"
2015-10-10 08:18:54 +00:00
"fmt"
"os"
"reflect"
2015-10-10 08:18:54 +00:00
"testing"
"github.com/pkg/diff"
"github.com/pkg/diff/write"
2019-12-09 02:44:53 +00:00
yaml "gopkg.in/yaml.v3"
2015-10-10 08:18:54 +00:00
)
2019-12-09 02:44:53 +00:00
func ParseData(rawData string) yaml.Node {
var parsedData yaml.Node
2015-10-10 08:18:54 +00:00
err := yaml.Unmarshal([]byte(rawData), &parsedData)
if err != nil {
fmt.Printf("Error parsing yaml: %v\n", err)
2015-10-10 08:18:54 +00:00
os.Exit(1)
}
return parsedData
}
func AssertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
2018-05-04 15:47:08 +00:00
t.Helper()
2015-10-10 08:18:54 +00:00
if expectedValue != actualValue {
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
}
}
func AssertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) {
2018-05-04 15:47:08 +00:00
t.Helper()
if !reflect.DeepEqual(expectedValue, actualValue) {
2020-09-20 12:47:57 +00:00
t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
}
}
2020-10-16 01:29:26 +00:00
func AssertResultComplexWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
t.Helper()
if !reflect.DeepEqual(expectedValue, actualValue) {
t.Error(context)
t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
}
}
func AssertResultWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
2018-05-04 15:47:08 +00:00
t.Helper()
opts := []write.Option{write.TerminalColor()}
2015-10-10 08:18:54 +00:00
if expectedValue != actualValue {
t.Error(context)
var differenceBuffer bytes.Buffer
if err := diff.Text("expected", "actual", expectedValue, actualValue, bufio.NewWriter(&differenceBuffer), opts...); err != nil {
t.Error(err)
} else {
t.Error(differenceBuffer.String())
}
2015-10-10 08:18:54 +00:00
}
}