Debian Codegraph @

v2/interface.go

raw symbols SCIP
1 package goldie
2
3 import (
4 "os"
5 "testing"
6 )
7
8 // Compile time assurance
9 var _ Tester = (*Goldie)(nil)
10 var _ OptionProcessor = (*Goldie)(nil)
11
12 // Option defines the signature of a functional option method that can apply
13 // options to an OptionProcessor.
14 type Option func(OptionProcessor) error
15
16 // Tester defines the methods that any golden tester should support.
17 type Tester interface {
18 Assert(t *testing.T, name string, actualData []byte)
19 AssertJson(t *testing.T, name string, actualJsonData interface{})
20 AssertXml(t *testing.T, name string, actualXmlData interface{})
21 AssertWithTemplate(t *testing.T, name string, data interface{}, actualData []byte)
22 Update(t *testing.T, name string, actualData []byte) error
23 GoldenFileName(t *testing.T, name string) string
24 }
25
26 // DiffFn takes in an actual and expected and will return a diff string
27 // representing the differences between the two.
28 type DiffFn func(actual string, expected string) string
29
30 // DiffEngine is used to enumerate the diff engine processors that are
31 // available.
32 type DiffEngine int
33
34 //noinspection GoUnusedConst
35 const (
36 // UndefinedDiff represents any undefined diff processor. If a new diff
37 // engine is implemented, it should be added to this enumeration and to the
38 // `diff` helper function.
39 UndefinedDiff DiffEngine = iota
40
41 // ClassicDiff produces a diff similar to what the `diff` tool would
42 // produce.
43 // +++ Actual
44 // @@ -1 +1 @@
45 // -Lorem dolor sit amet.
46 // +Lorem ipsum dolor.
47 //
48 ClassicDiff
49
50 // ColoredDiff produces a diff that will use red and green colors to
51 // distinguish the diffs between the two values.
52 ColoredDiff
53
54 // Simple is a very plain way of printing the byte comparison. It will look
55 // like this:
56 //
57 // Expected: <data>
58 // Got: <data>
59 Simple
60 )
61
62 // OptionProcessor defines the functions that can be called to set values for
63 // a tester. To expand this list, add a function to this interface and then
64 // implement the generic option setter below.
65 type OptionProcessor interface {
66 WithFixtureDir(dir string) error
67 WithNameSuffix(suffix string) error
68 WithFilePerms(mode os.FileMode) error
69 WithDirPerms(mode os.FileMode) error
70
71 WithDiffEngine(engine DiffEngine) error
72 WithDiffFn(fn DiffFn) error
73 WithIgnoreTemplateErrors(ignoreErrors bool) error
74 WithTestNameForDir(use bool) error
75 WithSubTestNameForDir(use bool) error
76 }
77
78 // === OptionProcessor ===============================
79
80 // WithFixtureDir sets the fixture directory.
81 //
82 // Defaults to `testdata`
83 func WithFixtureDir(dir string) Option {
84 return func(o OptionProcessor) error {
85 return o.WithFixtureDir(dir)
86 }
87 }
88
89 // WithNameSuffix sets the file suffix to be used for the golden file.
90 //
91 // Defaults to `.golden`
92 func WithNameSuffix(suffix string) Option {
93 return func(o OptionProcessor) error {
94 return o.WithNameSuffix(suffix)
95 }
96 }
97
98 // WithFilePerms sets the file permissions on the golden files that are
99 // created.
100 //
101 // Defaults to 0644.
102 //noinspection GoUnusedExportedFunction
103 func WithFilePerms(mode os.FileMode) Option {
104 return func(o OptionProcessor) error {
105 return o.WithFilePerms(mode)
106 }
107 }
108
109 // WithDirPerms sets the directory permissions for the directories in which the
110 // golden files are created.
111 //
112 // Defaults to 0755.
113 //noinspection GoUnusedExportedFunction
114 func WithDirPerms(mode os.FileMode) Option {
115 return func(o OptionProcessor) error {
116 return o.WithDirPerms(mode)
117 }
118 }
119
120 // WithDiffEngine sets the `diff` engine that will be used to generate the
121 // `diff` text.
122 //
123 // Default: ClassicDiff
124 //noinspection GoUnusedExportedFunction
125 func WithDiffEngine(engine DiffEngine) Option {
126 return func(o OptionProcessor) error {
127 return o.WithDiffEngine(engine)
128 }
129 }
130
131 // WithDiffFn sets the `diff` engine to be a function that implements the
132 // DiffFn signature. This allows for any customized diff logic you would like
133 // to create.
134 //noinspection GoUnusedExportedFunction
135 func WithDiffFn(fn DiffFn) Option {
136 return func(o OptionProcessor) error {
137 return o.WithDiffFn(fn)
138 }
139 }
140
141 // WithIgnoreTemplateErrors allows template processing to ignore any variables
142 // in the template that do not have corresponding data values passed in.
143 //
144 // Default value is false.
145 //noinspection GoUnusedExportedFunction
146 func WithIgnoreTemplateErrors(ignoreErrors bool) Option {
147 return func(o OptionProcessor) error {
148 return o.WithIgnoreTemplateErrors(ignoreErrors)
149 }
150 }
151
152 // WithTestNameForDir will create a directory with the test's name in the
153 // fixture directory to store all the golden files.
154 //
155 // Default value is false.
156 //noinspection GoUnusedExportedFunction
157 func WithTestNameForDir(use bool) Option {
158 return func(o OptionProcessor) error {
159 return o.WithTestNameForDir(use)
160 }
161 }
162
163 // WithSubTestNameForDir will create a directory with the sub test's name to
164 // store all the golden files. If WithTestNameForDir is enabled, it will be in
165 // the test name's directory. Otherwise, it will be in the fixture directory.
166 //
167 // Default value is false.
168 //noinspection GoUnusedExportedFunction
169 func WithSubTestNameForDir(use bool) Option {
170 return func(o OptionProcessor) error {
171 return o.WithSubTestNameForDir(use)
172 }
173 }