testfile

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package testfile reads and writes meyer's consolidated corpus files.

A corpus file (parser/testdata/<name>.test) holds many cases:

==== <case name>
<SQL, verbatim, one or more lines>
----
stmt <offset> ok
stmt <offset> err <rc> <erroff> <tail> <message>

The result lines are the raw per-statement output of the SQLite oracle (see cmd/regenerate-parse): every statement in the case was prepared independently with sqlite3_prepare_v2 against an empty in-memory database. Offsets are byte offsets into the case SQL; erroff is -1 when SQLite did not report an error position, and tail is how far sqlite3_prepare_v2 reported having got (a successful prepare always reaches the end of its statement, so ok lines carry no tail). The corpus stores this raw truth; the pass/fail policy for the parser under test is derived from it by (*Case).Expected, so the policy can evolve without regenerating.

Each corpus file has a sidecar <name>.metadata.json:

{"todo": {"case name": true, ...}}

listing cases the parser does not handle yet. The test harness skips todo cases by default and, when run with -check-parse, removes entries that have started passing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCase

func CheckCase(c Case) error

CheckCase reports whether a case can be represented in the file format: its SQL must not collide with the markers, and its messages must be single-line.

func IsSyntaxError

func IsSyntaxError(msg string) bool

IsSyntaxError reports whether msg is in the syntax-error family.

func MetadataPath

func MetadataPath(testPath string) string

MetadataPath returns the sidecar path for a corpus file path.

func Write

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

Write renders cases to path. It refuses SQL that would be ambiguous with the file format's markers.

func WriteMetadata

func WriteMetadata(path string, m Metadata) error

WriteMetadata saves a sidecar with stable formatting.

Types

type Case

type Case struct {
	Name    string
	SQL     string // always ends with exactly one "\n"
	Results []StmtResult
}

Case is a single corpus entry.

func Read

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

Read parses a corpus file.

func (*Case) Expected

func (c *Case) Expected() Expectation

Expected derives the harness expectation: the first syntax-family error in statement order wins (meyer is fail-fast); if there is none, every statement must parse, except within the ranges SQLite's own parser never reached.

type Expectation

type Expectation struct {
	OK      bool
	Message string // expected parse error message when !OK
	Offset  int    // expected error offset when !OK; -1 if unknown

	// Unreached lists byte ranges of the case SQL that SQLite's parser
	// never looked at, because a grammar action failed part-way through a
	// statement and sqlite3RunParser abandoned the rest of it. The corpus
	// says nothing about whether that text is valid SQL, so a parser under
	// test may fail inside one of these ranges without being wrong. A range
	// that reaches the end of the input includes the offset one past it,
	// which is where a parser reports running out of input.
	Unreached []Range

	// Truncated reports that the last statement failed with a non-syntax
	// message raised while its own last token was the lookahead. SQLite
	// stopped there, so it never reached the end of the input and could not
	// notice that the input was cut short: "CREATE TABLE aux1.t1(" is
	// "unknown database aux1" to SQLite and "incomplete input" to a parser
	// that reads to the end. pzTail cannot separate the two, because the
	// statement's last token is also the end of the input, so the case
	// simply cannot say who is right.
	Truncated bool
}

Expectation is the derived requirement for the parser under test.

func (Expectation) IsUnreached

func (e Expectation) IsUnreached(offset int) bool

IsUnreached reports whether offset falls in text SQLite's parser never reached. An offset of -1 (unknown) is never unreached.

type Metadata

type Metadata struct {
	Todo map[string]bool `json:"todo,omitempty"`
}

Metadata is the per-file sidecar tracking not-yet-implemented cases.

func ReadMetadata

func ReadMetadata(path string) (Metadata, error)

ReadMetadata loads a sidecar; a missing file yields empty metadata.

type Range

type Range struct{ Start, End int }

Range is a half-open byte range of a case's SQL.

type StmtResult

type StmtResult struct {
	Offset    int    // byte offset of the statement within the case SQL
	OK        bool   // sqlite3_prepare_v2 returned SQLITE_OK
	RC        int    // prepare result code when !OK
	ErrOffset int    // sqlite3_error_offset within the case SQL; -1 if unknown
	Message   string // sqlite3_errmsg when !OK

	// Tail is pzTail within the case SQL: how far the parser got. On an OK
	// result 0 is the common case and means "all of it" -- a successful
	// prepare always consumes at least one statement, so a recorded tail is
	// never 0.
	Tail int
}

StmtResult is one raw oracle observation for a single statement.

func ParseResultLine

func ParseResultLine(line string) (StmtResult, error)

ParseResultLine decodes one "stmt ..." line of oracle output. It is exported because the oracle runner in internal/sqlitesrc reads the same lines live, and a wire format with two decoders drifts.

Jump to

Keyboard shortcuts

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