File size: 7,344 Bytes
e36aeda | 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | // Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package test2json
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"unicode/utf8"
)
var update = flag.Bool("update", false, "rewrite testdata/*.json files")
func TestGolden(t *testing.T) {
files, err := filepath.Glob("testdata/*.test")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
name := strings.TrimSuffix(filepath.Base(file), ".test")
t.Run(name, func(t *testing.T) {
orig, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
// Test one line written to c at a time.
// Assume that's the most likely to be handled correctly.
var buf bytes.Buffer
c := NewConverter(&buf, "", 0)
in := append([]byte{}, orig...)
for _, line := range bytes.SplitAfter(in, []byte("\n")) {
writeAndKill(c, line)
}
c.Close()
if *update {
js := strings.TrimSuffix(file, ".test") + ".json"
t.Logf("rewriting %s", js)
if err := os.WriteFile(js, buf.Bytes(), 0666); err != nil {
t.Fatal(err)
}
return
}
want, err := os.ReadFile(strings.TrimSuffix(file, ".test") + ".json")
if err != nil {
t.Fatal(err)
}
diffJSON(t, buf.Bytes(), want)
if t.Failed() {
// If the line-at-a-time conversion fails, no point testing boundary conditions.
return
}
// Write entire input in bulk.
t.Run("bulk", func(t *testing.T) {
buf.Reset()
c = NewConverter(&buf, "", 0)
in = append([]byte{}, orig...)
writeAndKill(c, in)
c.Close()
diffJSON(t, buf.Bytes(), want)
})
// In bulk again with \r\n.
t.Run("crlf", func(t *testing.T) {
buf.Reset()
c = NewConverter(&buf, "", 0)
in = bytes.ReplaceAll(orig, []byte("\n"), []byte("\r\n"))
writeAndKill(c, in)
c.Close()
diffJSON(t, bytes.ReplaceAll(buf.Bytes(), []byte(`\r\n`), []byte(`\n`)), want)
})
// Write 2 bytes at a time on even boundaries.
t.Run("even2", func(t *testing.T) {
buf.Reset()
c = NewConverter(&buf, "", 0)
in = append([]byte{}, orig...)
for i := 0; i < len(in); i += 2 {
if i+2 <= len(in) {
writeAndKill(c, in[i:i+2])
} else {
writeAndKill(c, in[i:])
}
}
c.Close()
diffJSON(t, buf.Bytes(), want)
})
// Write 2 bytes at a time on odd boundaries.
t.Run("odd2", func(t *testing.T) {
buf.Reset()
c = NewConverter(&buf, "", 0)
in = append([]byte{}, orig...)
if len(in) > 0 {
writeAndKill(c, in[:1])
}
for i := 1; i < len(in); i += 2 {
if i+2 <= len(in) {
writeAndKill(c, in[i:i+2])
} else {
writeAndKill(c, in[i:])
}
}
c.Close()
diffJSON(t, buf.Bytes(), want)
})
// Test with very small output buffers, to check that
// UTF8 sequences are not broken up.
for b := 5; b <= 8; b++ {
t.Run(fmt.Sprintf("tiny%d", b), func(t *testing.T) {
oldIn := inBuffer
oldOut := outBuffer
defer func() {
inBuffer = oldIn
outBuffer = oldOut
}()
inBuffer = 64
outBuffer = b
buf.Reset()
c = NewConverter(&buf, "", 0)
in = append([]byte{}, orig...)
writeAndKill(c, in)
c.Close()
diffJSON(t, buf.Bytes(), want)
})
}
})
}
}
// writeAndKill writes b to w and then fills b with Zs.
// The filling makes sure that if w is holding onto b for
// future use, that future use will have obviously wrong data.
func writeAndKill(w io.Writer, b []byte) {
w.Write(b)
for i := range b {
b[i] = 'Z'
}
}
// diffJSON diffs the stream we have against the stream we want
// and fails the test with a useful message if they don't match.
func diffJSON(t *testing.T, have, want []byte) {
t.Helper()
type event map[string]any
// Parse into events, one per line.
parseEvents := func(b []byte) ([]event, []string) {
t.Helper()
var events []event
var lines []string
for _, line := range bytes.SplitAfter(b, []byte("\n")) {
if len(line) > 0 {
line = bytes.TrimSpace(line)
var e event
err := json.Unmarshal(line, &e)
if err != nil {
t.Errorf("unmarshal %s: %v", b, err)
continue
}
events = append(events, e)
lines = append(lines, string(line))
}
}
return events, lines
}
haveEvents, haveLines := parseEvents(have)
wantEvents, wantLines := parseEvents(want)
if t.Failed() {
return
}
// Make sure the events we have match the events we want.
// At each step we're matching haveEvents[i] against wantEvents[j].
// i and j can move independently due to choices about exactly
// how to break up text in "output" events.
i := 0
j := 0
// Fail reports a failure at the current i,j and stops the test.
// It shows the events around the current positions,
// with the current positions marked.
fail := func() {
var buf bytes.Buffer
show := func(i int, lines []string) {
for k := -2; k < 5; k++ {
marker := ""
if k == 0 {
marker = "» "
}
if 0 <= i+k && i+k < len(lines) {
fmt.Fprintf(&buf, "\t%s%s\n", marker, lines[i+k])
}
}
if i >= len(lines) {
// show marker after end of input
fmt.Fprintf(&buf, "\t» \n")
}
}
fmt.Fprintf(&buf, "have:\n")
show(i, haveLines)
fmt.Fprintf(&buf, "want:\n")
show(j, wantLines)
t.Fatal(buf.String())
}
var outputTest string // current "Test" key in "output" events
var wantOutput, haveOutput string // collected "Output" of those events
// getTest returns the "Test" setting, or "" if it is missing.
getTest := func(e event) string {
s, _ := e["Test"].(string)
return s
}
// checkOutput collects output from the haveEvents for the current outputTest
// and then checks that the collected output matches the wanted output.
checkOutput := func() {
for i < len(haveEvents) && haveEvents[i]["Action"] == "output" && getTest(haveEvents[i]) == outputTest {
haveOutput += haveEvents[i]["Output"].(string)
i++
}
if haveOutput != wantOutput {
t.Errorf("output mismatch for Test=%q:\nhave %q\nwant %q", outputTest, haveOutput, wantOutput)
fail()
}
haveOutput = ""
wantOutput = ""
}
// Walk through wantEvents matching against haveEvents.
for j = range wantEvents {
e := wantEvents[j]
if e["Action"] == "output" && getTest(e) == outputTest {
wantOutput += e["Output"].(string)
continue
}
checkOutput()
if e["Action"] == "output" {
outputTest = getTest(e)
wantOutput += e["Output"].(string)
continue
}
if i >= len(haveEvents) {
t.Errorf("early end of event stream: missing event")
fail()
}
if !reflect.DeepEqual(haveEvents[i], e) {
t.Errorf("events out of sync")
fail()
}
i++
}
checkOutput()
if i < len(haveEvents) {
t.Errorf("extra events in stream")
fail()
}
}
func TestTrimUTF8(t *testing.T) {
s := "hello α ☺ 😂 world" // α is 2-byte, ☺ is 3-byte, 😂 is 4-byte
b := []byte(s)
for i := 0; i < len(s); i++ {
j := trimUTF8(b[:i])
u := string([]rune(s[:j])) + string([]rune(s[j:]))
if u != s {
t.Errorf("trimUTF8(%q) = %d (-%d), not at boundary (split: %q %q)", s[:i], j, i-j, s[:j], s[j:])
}
if utf8.FullRune(b[j:i]) {
t.Errorf("trimUTF8(%q) = %d (-%d), too early (missed: %q)", s[:j], j, i-j, s[j:i])
}
}
}
|