test

package
v0.0.0-...-6cc8dd8 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package test provides utilities for testing Fastbelt language implementations.

Create a Fixture from your language's service container, then call Fixture.Parse or Fixture.ParseAll to build documents. Use Doc assertion methods and the generic Find/MustFind functions to inspect results.

Markers

Content strings may embed position markers that are stripped before parsing. Positions are recorded relative to the cleaned text.

  • Range marker: <|label:text|> — spans "text"; label identifies the range.
  • Range shorthand: <|label|> — equivalent to <|label:label|>.
  • Empty range: <|label:|> — zero-length span at the marker position.
  • Index marker: <|label> — single position (no text content).

Range markers are tried before index markers when both share the same opening delimiter (the default). Marker delimiters are configurable via TestMarking.

Index

Constants

This section is empty.

Variables

View Source
var DefaultMarking = &TestMarking{
	StartRange: "<|",
	EndRange:   "|>",
	StartIndex: "",
	EndIndex:   ">",
	Delimiter:  ":",
}

DefaultMarking is the marker configuration used by New. Also used if a nil *TestMarking is passed to Fixture.WithMarking.

Functions

func FindAll

func FindAll[T core.AstNode](d *Doc) []T

FindAll returns all nodes of type T in the document tree.

func FindNamedNode

func FindNamedNode[T core.AstNode](d *Doc, name string) (T, bool)

FindNamedNode returns the first node of type T whose Name() method returns the given name. T must implement core.NamedNode (or have a Name() string method) to find results.

func FindNode

func FindNode[T core.AstNode](d *Doc) (T, bool)

FindNode returns the first node of type T in the document tree, or (zero, false).

func FindNodeAtLabel

func FindNodeAtLabel[T core.AstNode](d *Doc, label string) (T, bool)

FindNodeAtLabel returns the most specific node of type T at the position of the named range marker (start offset) or index marker.

func FindNodeAtLocation

func FindNodeAtLocation[T core.AstNode](d *Doc, location core.TextLocation) (T, bool)

FindNodeAtLocation returns the most specific (smallest span) node of type T whose text range contains the given line/column position.

func FindNodeAtOffset

func FindNodeAtOffset[T core.AstNode](d *Doc, offset int) (T, bool)

FindNodeAtOffset returns the most specific (smallest span) node of type T whose text span contains the given byte offset.

func FindReference

func FindReference[T core.AstNode](d *Doc, label string) (*core.Reference[T], bool)

FindReference returns the first reference of type T whose cross-reference text is located at the given label. If the label can be found multiple times, the function returns (nil, false)

func FindReferenceWithText

func FindReferenceWithText[T core.AstNode](d *Doc, text string) (*core.Reference[T], bool)

FindReferenceWithText returns the first reference of type T whose cross-reference text matches the given string exactly.

func MustFindNamedNode

func MustFindNamedNode[T core.AstNode](d *Doc, name string) T

MustFindNamedNode fails the test if no node of type T with the given name is found.

func MustFindNode

func MustFindNode[T core.AstNode](d *Doc) T

MustFindNode fails the test if no node of type T is found in the document tree.

func MustFindNodeAtLabel

func MustFindNodeAtLabel[T core.AstNode](d *Doc, label string) T

MustFindNodeAtLabel fails the test if no node of type T is found at the given label.

func MustFindNodeAtLocation

func MustFindNodeAtLocation[T core.AstNode](d *Doc, location core.TextLocation) T

MustFindNodeAtLocation fails the test if no node of type T contains the given location.

func MustFindNodeAtOffset

func MustFindNodeAtOffset[T core.AstNode](d *Doc, offset int) T

MustFindNodeAtOffset fails the test if no node of type T contains the given offset.

func MustFindReference

func MustFindReference[T core.AstNode](d *Doc, label string) *core.Reference[T]

MustFindReference fails the test if no reference of type T is found at the given label.

func MustFindReferenceWithText

func MustFindReferenceWithText[T core.AstNode](d *Doc, text string) *core.Reference[T]

MustFindReferenceWithText fails the test if no reference of type T with the given text is found.

Types

type DiagnosticExpectation

type DiagnosticExpectation struct {
	Diagnostic *core.Diagnostic
	// contains filtered or unexported fields
}

func (*DiagnosticExpectation) WithCode

func (*DiagnosticExpectation) WithMessage

func (d *DiagnosticExpectation) WithMessage(msg string) *DiagnosticExpectation

func (*DiagnosticExpectation) WithMessageContaining

func (d *DiagnosticExpectation) WithMessageContaining(substring string) *DiagnosticExpectation

func (*DiagnosticExpectation) WithSeverity

func (*DiagnosticExpectation) WithSource

func (d *DiagnosticExpectation) WithSource(source string) *DiagnosticExpectation

func (*DiagnosticExpectation) WithTags

type Doc

type Doc struct {
	Document *core.Document
	Ranges   []RangeMarker
	Indices  []IndexMarker
	// contains filtered or unexported fields
}

Doc wraps a built core.Document with assertion methods and marker positions. Access the underlying document via the exported Document field.

func (*Doc) AssertDefinition

func (d *Doc) AssertDefinition(label string)

func (*Doc) AssertDocumentSymbol

func (d *Doc) AssertDocumentSymbol(label string, expectedName string, expectedKind lsp.SymbolKind) *Doc

AssertDocumentSymbol verifies that a document symbol exists with the given name at the marker label. Returns the Doc for chaining.

func (*Doc) AssertFoldingRanges

func (d *Doc) AssertFoldingRanges(labels ...string) *Doc

AssertFoldingRanges verifies that folding ranges exist for all specified marker labels. Returns the Doc for chaining.

func (*Doc) AssertNoDiagnostics

func (d *Doc) AssertNoDiagnostics() *Doc

AssertNoDiagnostics fails the test if any diagnostics exist at any severity.

func (*Doc) AssertNoErrors

func (d *Doc) AssertNoErrors() *Doc

AssertNoErrors fails the test if any error-severity Diagnostics exist.

func (*Doc) AssertNoParseErrors

func (d *Doc) AssertNoParseErrors() *Doc

AssertNoParseErrors fails the test if any LexerErrors or ParserErrors exist. Diagnostics are not checked.

func (*Doc) AssertReferences

func (d *Doc) AssertReferences(label string)

func (*Doc) AssertState

func (d *Doc) AssertState(flag core.DocumentState) *Doc

AssertState fails the test unless the document state includes the given flag.

func (*Doc) Ctx

func (d *Doc) Ctx() context.Context

Ctx returns the context for this document. Pass it to core.Reference.Ref.

func (*Doc) Diagnostics

func (d *Doc) Diagnostics() []*core.Diagnostic

Diagnostics returns all diagnostics collected during validation.

func (*Doc) ExpectDiagnostic

func (d *Doc) ExpectDiagnostic(label string) *DiagnosticExpectation

AssertDiagnostic fails the test unless at least one diagnostic has the given severity and a message containing msgSubstring.

func (*Doc) FindSymbolAtLabel

func (d *Doc) FindSymbolAtLabel(label string) (*lsp.DocumentSymbol, bool)

FindSymbolAtLabel finds a document symbol at the given marker label. Returns the symbol and true if found, or nil and false if not found.

func (*Doc) MarkerRange

func (d *Doc) MarkerRange(label string) (core.TextRange, error)

func (*Doc) MustFindSymbolAtLabel

func (d *Doc) MustFindSymbolAtLabel(label string) *lsp.DocumentSymbol

MustFindSymbolAtLabel finds a document symbol at the given marker label. Fails the test if the symbol is not found.

func (*Doc) Root

func (d *Doc) Root() core.AstNode

Root returns the root AST node of the document.

func (*Doc) RunRename

func (d *Doc) RunRename(label string, newName string) *Doc

Performs a rename operation using the server's RenameProvider at the position of the given marker label. Updates the affected documents with the edits returned and rebuilds the workspace.

type Fixture

type Fixture struct {
	// contains filtered or unexported fields
}

Fixture is the entry point for language tests. Create one per test with New.

func New

func New(t testing.TB, sc *service.Container) *Fixture

New creates a Fixture backed by the given service container. It uses DefaultMarking and context.Background.

func NewWithContext

func NewWithContext(t testing.TB, sc *service.Container, ctx context.Context) *Fixture

NewWithContext creates a Fixture with an explicit context.

func (*Fixture) Ctx

func (f *Fixture) Ctx() context.Context

func (*Fixture) Documents

func (f *Fixture) Documents() []*Doc

func (*Fixture) Marking

func (f *Fixture) Marking() *TestMarking

func (*Fixture) Parse

func (f *Fixture) Parse(content string) *Doc

Parse builds a single in-memory document from content. Markers are extracted before parsing. The URI is "inmemory://test<ext>" where <ext> is the first entry in WorkspaceSrv.FileExtensions (e.g. ".statemachine"), or "inmemory://test" if none are configured.

func (*Fixture) ParseAll

func (f *Fixture) ParseAll(uriContentPairs ...string) []*Doc

ParseAll builds multiple documents together, enabling cross-document reference resolution. Arguments are alternating URI/content pairs: uri1, content1, uri2, content2, ... All documents are registered in the DocumentManager before building. Results are returned in the same order as the pairs.

func (*Fixture) ParseURI

func (f *Fixture) ParseURI(content, uri string) *Doc

ParseURI builds a single in-memory document with the given URI. Markers are extracted before parsing.

func (*Fixture) Services

func (f *Fixture) Services() *service.Container

func (*Fixture) T

func (f *Fixture) T() testing.TB

func (*Fixture) WithMarking

func (f *Fixture) WithMarking(m *TestMarking) *Fixture

WithMarking returns a shallow copy of the Fixture using m as the marker configuration.

type IndexMarker

type IndexMarker struct {
	Label  string
	Offset int
}

IndexMarker records a labeled position extracted from test content. Offset is a byte offset into the clean text (markers removed).

type RangeMarker

type RangeMarker struct {
	Label string
	Start int // inclusive
	End   int // exclusive
}

RangeMarker records a labeled span extracted from test content. Start and End are byte offsets into the clean text (markers removed).

type TestMarking

type TestMarking struct {
	StartRange string // Opening delimiter for range markers.
	EndRange   string // Closing delimiter for range markers.
	StartIndex string // Opening delimiter for index markers, defaults to the current [TestMarking.StartRange] if empty.
	EndIndex   string // Closing delimiter for index markers.
	Delimiter  string // Separator between label and text in range markers.
}

TestMarking configures the delimiter syntax for position markers in test content. See DefaultMarking for the default configuration.

Jump to

Keyboard shortcuts

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