Documentation ¶
Overview ¶
The golden package provides methods for producing and comparing golden files.
Golden files are described in the testfiles repository:
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { // The format of the input file. Format string `json:",omitempty"` // The structure of the output file. Struct string `json:",omitempty"` }
Config contains options that configure a group.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder is a low-level encoder for values. An initialized Encoder is received by a Handler.
type Field ¶
type Field struct { Name string Value interface{} }
Field represents the field of a JSON object.
type Golden ¶
type Golden struct {
// contains filtered or unexported fields
}
Golden is an encoder that writes the golden format.
func NewGolden ¶
NewGolden returns a Golden that writes to w. It is initialized by writing the fields in config. For correctly formatted data, Golden must be closed once all data has been written.
func (*Golden) Array ¶
Array writes a Data field as a JSON array. Each argument is an element in the array.
No attempt is made to ensure that the field name is unique.
Does nothing if the encoder is closed.
func (*Golden) Close ¶
Close finishes encoding the root object, flushes any remaining data to be written, and closes the encoder.
func (*Golden) Object ¶
Object writes a Data field as a JSON object. Each argument is a field in the object.
No attempt is made to ensure that the field name is unique.
Does nothing if the encoder is closed.
func (*Golden) SetHandler ¶
SetHandler sets a function to call when Value does not match any types. If the handler returns false, then an error value is written instead.
func (*Golden) Value ¶
Value writes a Data field as an arbitrary value. The following types are detected by default:
- nil is written as a JSON null.
- bool is written as a JSON bool.
- primitive numeric types are written as a JSON number.
- Infinity and NaN are written as a JSON string.
- Literal and json.Number are written as-is. Note that an improperly formatted value can cause the resulting JSON to become invalid.
- []byte is written as a JSON array of human-readable strings.
- string is written as a JSON string, or according to []byte if the string contains non-graphic characters.
- error is written according to string.
- Array and []interface{} are written as a JSON array.
- Object is written as a JSON object with ordered fields. No attempt is made to make fields unique.
- map[string]interface{} is written as a JSON object with fields ordered lexicographically.
If the type is not known, then the function set by SetHandler is called. If the handler returns false, or no handler is set, then `<UNKNOWN:type>` is emitted, causing the resulting JSON to be invalid.
No attempt is made to ensure that the field name is unique.
Does nothing if the encoder is closed.
type Group ¶
func GroupOf ¶
GroupOf returns the group corresponding to the given file. The resulting group will be empty if the file does not exist or cannot be a part of a group.
func Groups ¶
Groups returns a list of file groups produced by walking the given directory.
Sibling files form a group when their names match. The part of the name used to match depends on the type:
- An input file uses its full name.
- A golden file uses its name without the `.golden` extension.
- A config file uses its name without the `.golden-config` extension.
Each resulting Group will have an Input, file other fields are set only if the file exists. Paths in each Group are relative to the input directory.