...
1 package commontest
2
3 import (
4 "regexp"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8 )
9
10
11
12
13 type WriterTestSuite struct {
14
15
16
17 ContextFactory func() TestContext
18
19
20
21 ValueTestFactory ValueTestFactory
22
23
24
25
26
27 EncodeAsHex func(rune) bool
28 }
29
30
31 func (s WriterTestSuite) Run(t *testing.T) {
32 tf := testFactory{
33 valueTestFactory: s.ValueTestFactory,
34 encodingBehavior: encodingBehavior{
35 encodeAsHex: s.EncodeAsHex,
36 },
37 }
38 tds := tf.MakeAllValueTests()
39 for _, td := range tds {
40 t.Run(td.name, func(t *testing.T) {
41 c := s.ContextFactory()
42 t.Cleanup(func() {
43 if t.Failed() {
44 t.Logf("JSON output: `%s`", string(c.JSONData()))
45 }
46 })
47 require.NoError(t, td.action(c))
48 output := string(c.JSONData())
49 require.Regexp(t, makeOutputRegex(td.encoding), output)
50 })
51 }
52 }
53
54
55 func makeOutputRegex(outputParts []string) *regexp.Regexp {
56 regex := "\\w*"
57 for _, outputPart := range outputParts {
58 regex += regexp.QuoteMeta(outputPart)
59 regex += "\\w*"
60 }
61 return regexp.MustCompile(regex)
62 }
63
View as plain text