1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
package litter_test
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"testing"
"github.com/sanity-io/litter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type BlankStruct struct{}
type BasicStruct struct {
Public int
private int
}
type InterfaceStruct struct {
Ifc interface{}
}
type RecursiveStruct struct {
Ptr *RecursiveStruct
}
type CustomMultiLineDumper struct {
Dummy int
}
func (cmld *CustomMultiLineDumper) LitterDump(w io.Writer) {
w.Write([]byte("{\n multi\n line\n}"))
}
type CustomSingleLineDumper int
func (csld CustomSingleLineDumper) LitterDump(w io.Writer) {
w.Write([]byte("<custom>"))
}
func TestSdump_primitives(t *testing.T) {
runTests(t, "primitives", []interface{}{
false,
true,
7,
int8(10),
int16(10),
int32(10),
int64(10),
uint8(10),
uint16(10),
uint32(10),
uint64(10),
uint(10),
float32(12.3),
float64(12.3),
complex64(12 + 10.5i),
complex128(-1.2 - 0.1i),
"string with \"quote\"",
[]int{1, 2, 3},
interface{}("hello from interface"),
BlankStruct{},
&BlankStruct{},
BasicStruct{1, 2},
nil,
interface{}(nil),
})
}
func TestSdump_customDumper(t *testing.T) {
cmld := CustomMultiLineDumper{Dummy: 1}
cmld2 := CustomMultiLineDumper{Dummy: 2}
csld := CustomSingleLineDumper(42)
csld2 := CustomSingleLineDumper(43)
runTests(t, "customDumper", map[string]interface{}{
"v1": &cmld,
"v2": &cmld,
"v2x": &cmld2,
"v3": csld,
"v4": &csld,
"v5": &csld,
"v6": &csld2,
})
}
func TestSdump_pointerAliasing(t *testing.T) {
p0 := &RecursiveStruct{Ptr: nil}
p1 := &RecursiveStruct{Ptr: p0}
p2 := &RecursiveStruct{}
p2.Ptr = p2
runTests(t, "pointerAliasing", []*RecursiveStruct{
p0,
p0,
p1,
p2,
})
}
func TestSdump_nilIntefacesInStructs(t *testing.T) {
p0 := &InterfaceStruct{nil}
p1 := &InterfaceStruct{p0}
runTests(t, "nilIntefacesInStructs", []*InterfaceStruct{
p0,
p1,
p0,
nil,
})
}
func TestSdump_config(t *testing.T) {
data := []interface{}{
litter.Config,
&BasicStruct{1, 2},
}
runTestWithCfg(t, "config_Compact", &litter.Options{
Compact: true,
}, data)
runTestWithCfg(t, "config_HidePrivateFields", &litter.Options{
HidePrivateFields: true,
}, data)
runTestWithCfg(t, "config_StripPackageNames", &litter.Options{
StripPackageNames: true,
}, data)
runTestWithCfg(t, "config_HomePackage", &litter.Options{
HomePackage: "litter_test",
}, data)
}
func TestSdump_multipleArgs(t *testing.T) {
value1 := []string{"x", "y"}
value2 := int32(42)
runTestWithCfg(t, "multipleArgs_noSeparator", &litter.Options{}, value1, value2)
runTestWithCfg(t, "multipleArgs_lineBreak", &litter.Options{Separator: "\n"}, value1, value2)
runTestWithCfg(t, "multipleArgs_separator", &litter.Options{Separator: "***"}, value1, value2)
}
func TestSdump_maps(t *testing.T) {
runTests(t, "maps", []interface{}{
map[string]string{
"hello": "there",
"something": "something something",
"another string": "indeed",
},
map[int]string{
3: "three",
1: "one",
2: "two",
},
map[int]*BlankStruct{
2: &BlankStruct{},
},
})
}
var standardCfg = litter.Options{}
func runTestWithCfg(t *testing.T, name string, cfg *litter.Options, cases ...interface{}) {
t.Run(name, func(t *testing.T) {
fileName := fmt.Sprintf("testdata/%s.dump", name)
dump := cfg.Sdump(cases...)
reference, err := ioutil.ReadFile(fileName)
if os.IsNotExist(err) {
t.Logf("Note: Test data file %s does not exist, writing it; verify contents!", fileName)
ioutil.WriteFile(fileName, []byte(dump), 0644)
return
}
assertEqualStringsWithDiff(t, string(reference), dump)
})
}
func runTests(t *testing.T, name string, cases ...interface{}) {
runTestWithCfg(t, name, &standardCfg, cases...)
}
func diffStrings(t *testing.T, expected, actual string) (*string, bool) {
if actual == expected {
return nil, true
}
dir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
defer os.RemoveAll(dir)
require.NoError(t, ioutil.WriteFile(fmt.Sprintf("%s/expected", dir), []byte(expected), 0644))
require.NoError(t, ioutil.WriteFile(fmt.Sprintf("%s/actual", dir), []byte(actual), 0644))
out, err := exec.Command("diff", "--side-by-side",
fmt.Sprintf("%s/expected", dir),
fmt.Sprintf("%s/actual", dir)).Output()
if _, ok := err.(*exec.ExitError); !ok {
require.NoError(t, err)
}
diff := string(out)
return &diff, false
}
func assertEqualStringsWithDiff(t *testing.T, expected, actual string,
msgAndArgs ...interface{}) bool {
diff, ok := diffStrings(t, expected, actual)
if ok {
return true
}
message := messageFromMsgAndArgs(msgAndArgs...)
if message == "" {
message = "Strings are different"
}
assert.Fail(t, fmt.Sprintf("%s (left is expected, right is actual):\n%s", message, *diff))
return false
}
func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return ""
}
if len(msgAndArgs) == 1 {
return msgAndArgs[0].(string)
}
if len(msgAndArgs) > 1 {
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
return ""
}
|