testfile

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package testfile reads and writes the consolidated corpus format shared by the sqlc parser family: cases separated by `== <name>` lines, input and expectation separated by a `--` line, with a metadata.json sidecar per file tracking which cases are still todo.

Layout of a .test file:

== 001
SELECT 1
--
{"version":170007,...}
== 002
...

Because the inputs are SQL — where a line consisting of exactly `--` is a perfectly ordinary comment — content lines that would collide with the markers are escaped with a leading `|` (and lines starting with `|` are escaped too, making the encoding a bijection). Files are written only by cmd/regenerate; expectations are never edited by hand.

Index

Constants

View Source
const TruncateLimitPrefix = "-- truncate_limit: "

The summary_truncate suite carries the truncation limit as a leading comment line in the case input; both cmd/regenerate and the corpus harness strip it before calling Summary, so the SQL the oracle and oliphant see is byte-identical to the raw statement.

Variables

This section is empty.

Functions

func IsError

func IsError(expected string) bool

IsError reports whether a golden expectation is an error block.

func RenderError

func RenderError(e ErrorExpectation) string

RenderError encodes an error expectation block:

ERROR: syntax error at or near "$"
CURSORPOS: 8
FILENAME: scan.l
FUNCNAME: scanner_yyerror

func RenderScanToken

func RenderScanToken(start, end int32, token, keywordKind string) string

RenderScanToken encodes one token of a scan golden:

0 6 SELECT RESERVED_KEYWORD

Token and keyword kind are the protobuf enum value names; a full scan golden is these lines joined with "\n".

func RenderSplit

func RenderSplit(stmts []string) string

RenderSplit encodes a statement-split golden as one statement per line, each Go-quoted (statements routinely contain newlines).

func RenderSummary

func RenderSummary(s SummaryExpectation) string

RenderSummary encodes a SummaryResult golden, one entry per line. Repeated fields keep their protobuf order; aliases (a protobuf map, unordered by definition) are sorted by key. The empty summary renders as "".

TABLE: name="public.x" schema="public" table="x" ctx=DDL
ALIAS: "a"="b"
CTE: "cte"
FUNCTION: name="pg_catalog.substr" function="substr" schema="pg_catalog" ctx=Call
FILTER: schema="" table="t" column="c"
STMT: SelectStmt
TRUNCATED: "SELECT ..."

func SplitTruncateLimit

func SplitTruncateLimit(input string) (limit int, sql string, err error)

SplitTruncateLimit strips the truncation-limit directive from a summary_truncate case input.

func WithTruncateLimit

func WithTruncateLimit(limit int, sql string) string

WithTruncateLimit prepends the truncation-limit directive to a statement.

func Write

func Write(path string, cases []Case) error

Write renders cases into the .test format.

func WriteMetadata

func WriteMetadata(testPath string, m Metadata) error

WriteMetadata writes the sidecar (sorted, stable bytes).

Types

type Case

type Case struct {
	Name     string
	Input    string
	Expected string
}

Case is one corpus entry: an input SQL string and its oracle-derived expectation (a protobuf-JSON tree, `ERROR: ...` block, token stream, …).

func Read

func Read(path string) ([]Case, error)

Read parses a .test file.

type ErrorExpectation

type ErrorExpectation struct {
	Message   string
	Cursorpos int
	Filename  string
	Funcname  string
	Context   string
}

ErrorExpectation is the golden form of a parser.Error. Lineno is excluded from conformance by design (upstream's own tests zero it: it is a C source line number).

func ParseError

func ParseError(expected string) (ErrorExpectation, error)

ParseError decodes an error expectation block written by RenderError.

type Metadata

type Metadata struct {
	Todo []string `json:"todo"`
}

Metadata is the per-file sidecar. Todo lists the case names not yet passing; `go test ./parser -check-parse` removes newly passing entries, and a case absent from Todo that fails is a regression, never a new todo.

func ReadMetadata

func ReadMetadata(testPath string) (Metadata, error)

ReadMetadata loads the sidecar for a .test file; a missing sidecar means nothing is todo.

type SummaryExpectation

type SummaryExpectation struct {
	Tables         []SummaryTable
	Aliases        map[string]string
	CteNames       []string
	Functions      []SummaryFunction
	FilterColumns  []SummaryFilterColumn
	StatementTypes []string
	TruncatedQuery string
}

type SummaryFilterColumn

type SummaryFilterColumn struct {
	SchemaName string
	TableName  string
	Column     string
}

type SummaryFunction

type SummaryFunction struct {
	Name         string
	FunctionName string
	SchemaName   string
	Context      string
}

type SummaryTable

type SummaryTable struct {
	Name       string
	SchemaName string
	TableName  string
	Context    string
}

SummaryTable / SummaryFunction / SummaryFilterColumn / SummaryExpectation mirror the SummaryResult protobuf field-for-field, with Context enums as their protobuf value names ("None", "Select", "DML", "DDL", "Call").

Jump to

Keyboard shortcuts

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