formattest

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package formattest checks, in a format package's tests, that a YAML format this repository defines follows the rules in AGENTS.md: it refuses unknown fields and versions it does not support, writes the same bytes every time it saves the same value, and loads and saves each of its testdata files without changing a byte. It also checks that the format has an entry on the File Formats reference page, docs/en/formats.md and docs/zh/formats.md, whose examples load and save to the same bytes.

Every package that defines such a format runs Run from a test, and TestFormatPackagesRunConformance in internal/repocheck fails a package that decodes YAML and does not, unless it is listed there as one that reads no format of its own.

Index

Constants

View Source
const Marker = "<!-- format: "

Marker is how the line that marks a format's entry on a reference page starts; the entry's ID and " -->" follow, as in "<!-- format: quest -->".

View Source
const UpdateEnv = "FORMATTEST_UPDATE"

UpdateEnv is the environment variable that, set to 1, makes Run rewrite each testdata file with what the format saves for it before checking it, to write a new fixture in the format's canonical form. The other checks still run, so a format that saves different bytes each time still fails.

Variables

View Source
var DocPages = []string{"docs/en/formats.md", "docs/zh/formats.md"}

DocPages are the reference pages that list every format, relative to the module root. Run checks the entry Format.Doc names on each.

Functions

func Run

func Run[T any](t *testing.T, f Format[T])

Run checks f, each rule in a subtest of its own: that Decode refuses an unknown field, a missing version, a version of 0 and a future version in each testdata file; that Encode writes the same bytes each time it saves a value, including one it loaded from its own output; and that each testdata file loads and saves to the same bytes.

It also checks the entry f.Doc names on each of DocPages: that the entry exists once and has at least one YAML example, and that each example loads and saves to the same bytes. The test must run in the format's package directory, as go test runs it.

Types

type Block

type Block struct {
	Lang string
	Data []byte
	Line int
}

Block is a fenced code block: the language its opening fence names, such as "yaml" or "json", its contents with a newline after each line, and the line of its first line of contents.

type Entry

type Entry struct {
	ID     string
	Line   int
	Blocks []Block
}

Entry is a format's entry on a reference page: the ID its marker line names, the line of that marker, and the fenced code blocks from there to the next marker or level-1 or level-2 heading.

func ParseEntries

func ParseEntries(data []byte) ([]Entry, error)

ParseEntries returns the entries a reference page marks, in page order. Run and the test of the page's entries in internal/repocheck read the page with it, so both see the same entries and examples.

An entry starts at a line "<!-- format: ID -->", where ID has no spaces, and ends at the next such line or at a line that starts with "# " or "## ". Trailing spaces and a carriage return on a line are ignored. A fenced code block starts at a line of three backticks and a language, possibly indented, and ends at a line of three backticks; markers and headings inside one are its contents. ParseEntries fails on a marker that is indented, malformed or given twice, on a block whose language is "yml" (write "yaml", which the checks look for) and on a block that is not closed, naming the line.

type Example

type Example struct {
	Page string
	Block
}

Example is a code block of an entry on one of DocPages, with the page's path relative to the repository root, for messages.

func Examples

func Examples(t *testing.T, id, lang string) []Example

Examples returns the code blocks in language lang, such as "json", of the entry id on each of DocPages, for a test that loads a format's examples with its reader when the format is not YAML and so has no Run. It fails t when a page does not parse or the entry has no such block. The test must run in a package directory of the module, as go test runs it.

type Format

type Format[T any] struct {
	// Decode loads a document, as the format's Parse function does. It must refuse a document with an unknown top-level field, with no version field, with a version of 0 and with a version newer than the one it supports, naming the field in its error.
	Decode func(data []byte) (T, error)
	// Encode saves a value, as the format's Encode function does.
	Encode func(v T) ([]byte, error)
	// Files are glob patterns, relative to the package directory, of the testdata files to check; together they must match at least one file. Each file must be in the format's canonical form, the bytes Encode writes for it.
	Files []string
	// VersionField is the top-level key that holds the document's schema version, an integer; empty means "version".
	VersionField string
	// MaxVersion is the newest schema version the format supports; when set, the future-version check tests max(document version, MaxVersion) + 1 instead of the document's version + 1, so a multi-version format can check older fixtures against an unsupported future version.
	MaxVersion int
	// Doc names the format's entry on the reference pages docs/en/formats.md and docs/zh/formats.md: the package's path relative to the module root, such as "quest" or "importsettings", followed by "#" and a name of the format when the package defines more than one, such as "achievements#progress". Each page marks the entry with the line "<!-- format: Doc -->" before its heading, and every YAML code block in the entry must load and save to the same bytes, so the page never shows an example the format refuses or would rewrite. Run requires it.
	Doc string
}

Format describes one YAML format for Run: how to load and save it, and the testdata files that hold documents in it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL