In Development Package, API may change between versions.

import "github.com/egdaemon/eg/runtime/x/wasi/egautogen"

Package egautogen drives language-specific test generators against functions selected from the coverage data eg already records while running a module's tests.

Constants

DefaultGolangFilename is the pattern for the name of the file generated tests are written to.

const DefaultGolangFilename = "%fn.file%.%fn.name%.autogen_test.go"

DefaultRevisions is the number of rework rounds used when none are specified.

const DefaultRevisions = 3

func Sample

func Sample(filters ...coverage.Filter) squirrel.SelectBuilder

Sample is the query selecting the functions matching the filters in a random order. it is not run, see From, nor limited, apply Seq.Limit for a sample of n functions.

func Worst

func Worst(filters ...coverage.Filter) squirrel.SelectBuilder

Worst is the query selecting the functions with the lowest statement coverage first, of those matching the filters. it is not run, see From, nor limited, see Seq.Limit.

type Filters

Filters is a conjunction of coverage filters built by chaining, e.g.) Language("go").Coverage(0, 80).SkipGenerated(). it is a coverage.Filter itself so it can be passed to Worst and Sample, alone or alongside other filters.

type Filters []coverage.Filter

func Coverage

func Coverage(min, max float32) Filters

Coverage selects the functions whose statement coverage is within min and max percent, both inclusive.

func ExcludePaths

func ExcludePaths(globs ...string) Filters

ExcludePaths removes the functions of files matching any of the glob patterns, see Paths.

func Language

func Language(names ...string) Filters

Language selects the functions of files in any of the languages, e.g.) "go", matched on the extension of the file. the name is not case sensitive and an unknown language is an error.

func Paths

func Paths(globs ...string) Filters

Paths selects the functions of files matching any of the glob patterns. patterns match the whole recorded path, which is relative to the workspace. `*` matches any run of characters including separators, `?` any single character.

func SkipGenerated

func SkipGenerated() Filters

SkipGenerated removes the functions of generated files, see coverage.TransformMarkGenerated.

func (Filters) Coverage

func (t Filters) Coverage(min, max float32) Filters

Coverage selects the functions whose statement coverage is within min and max percent, see the package function.

func (Filters) ExcludePaths

func (t Filters) ExcludePaths(globs ...string) Filters

ExcludePaths removes the functions of files matching any of the glob patterns, see the package function.

func (Filters) Language

func (t Filters) Language(names ...string) Filters

Language selects the functions of files in any of the languages, see the package function.

func (Filters) Paths

func (t Filters) Paths(globs ...string) Filters

Paths selects the functions of files matching any of the glob patterns, see the package function.

func (Filters) SkipGenerated

func (t Filters) SkipGenerated() Filters

SkipGenerated removes the functions of generated files, see the package function.

func (Filters) ToSql

func (t Filters) ToSql() (string, []any, error)

ToSql implements squirrel.Sqlizer. filters that do not restrict are skipped.

type Fn

Fn identifies a single function discovered from recorded coverage data.

type Fn struct {
    Path string
    Name string
    Hits int
}

type Generator

Generator implements the language specific parts of test generation. it prepares the material the model works from and verifies the model's response, which is expected to be only the artifact.

type Generator interface {
    // Seq are the functions to generate tests for.
    Seq() iterx.Seq[Fn]
    // Prompt is the source material the model works from, typically rendered
    // from a text/template executed with the context.
    Prompt(c PromptContext) (string, error)
    // Verify persists and validates the artifact, the model's response, which the prompt
    // requires to be only the artifact. an error discards the artifact and must leave nothing behind.
    Verify(ctx context.Context, fn Fn, artifact string) error
}

type GolangOption

GolangOption configures the Golang generator.

type GolangOption func(*golangOption)

type GolangOptions

GolangOptions is a chainable set of GolangOption, it implements Generator.

type GolangOptions []GolangOption

func Golang

func Golang(seq iterx.Seq[Fn]) GolangOptions

Golang generates tests for the Go functions in seq, combining the target function's source, the named types it operates on, and any existing tests that already exercise it, into a single prompt for the model. generated tests are only kept if they compile and pass.

func GolangLazy

func GolangLazy() GolangOptions

GolangLazy is Golang without its sequence, which is bound with From. configure the shared parts once, e.g.) Templates and Packages, then bind each sequence to a copy of the result, e.g.) a function at a time. the copies share the caches of packages and callers, and the templates are loaded once however many use them. they can be used concurrently.

func (GolangOptions) Directory

func (t GolangOptions) Directory(dir string) GolangOptions

Directory sets the directory the function's sources are read from and the generated tests are written to, e.g.) the worktree of an eggit.Change. defaults to the current directory.

func (GolangOptions) Filename

func (t GolangOptions) Filename(pattern string) GolangOptions

Filename sets the pattern for the name of the file generated tests are written to, next to the function's source. the file name must end in _test.go. in addition to the git patterns supported by eggit.StringReplace, the following are replaced:

%fn.file% -> the name of the file declaring the function, without its extension.
%fn.name% -> the lower cased name of the function, methods are Type_method.

defaults to DefaultGolangFilename.

func (GolangOptions) From

func (t GolangOptions) From(seq iterx.Seq[Fn]) GolangOptions

From binds the sequence of functions to generate tests for. the receiver is not modified.

func (GolangOptions) Prompt

func (t GolangOptions) Prompt(c PromptContext) (string, error)

func (GolangOptions) Rules

func (t GolangOptions) Rules(ensure ...string) []string

Rules are the default rules followed by the given rules, rules already among the defaults are not repeated.

func (GolangOptions) Seq

func (t GolangOptions) Seq() iterx.Seq[Fn]

func (GolangOptions) Templates

func (t GolangOptions) Templates[T Template](v ...T) GolangOptions

Templates adds example tests the generated tests should follow. the packages they import, and those imported by the function's source file and existing tests, are listed in the types block.

func (GolangOptions) Verify

func (t GolangOptions) Verify(ctx context.Context, fn Fn, artifact string) error

Verify keeps the generated test next to the function only if it compiles and passes. the test is run in place of any existing test file by overlaying it, so the existing file is only replaced once the generated test passes.

func (GolangOptions) Workspace

func (t GolangOptions) Workspace(dir string) GolangOptions

Workspace sets the directory the paths of absolute functions are relative to when moved into the directory, see Directory, e.g.) the working directory when coverage is reported from it. defaults to egenv.WorkspaceDirectory.

type Option

Option configures test generation.

type Option func(*option)

type Options

Options is a chainable set of Option.

type Options []Option

func New

func New() Options

New generates the default options.

func (Options) Attempts

func (t Options) Attempts(n int) Options

Attempts sets the number of generations to try per function.

func (Options) Ensure

func (t Options) Ensure(rules ...string) Options

Ensure adds rules every generated test must satisfy, e.g.) "no helper functions are added". rules are checked once the test is verified, a test violating them is sent back to the model to be reworked and the revision is verified again.

func (Options) Judge

func (t Options) Judge(v egllm.Judge) Options

Judge sets the judge used to generate the tests, defaults to egllm.Echo.

func (Options) Revisions

func (t Options) Revisions(n uint) Options

Revisions sets the maximum number of times a test violating the rules is sent back to the model to be reworked. 0 discards a violating test without reworking it.

func (Options) Run

func (t Options) Run(g Generator) eg.OpFn

Run generates tests with g subject to the options.

type PromptContext

PromptContext is the data a Generator renders its prompt from.

type PromptContext struct {
    // Fn is the function to generate a test for.
    Fn  Fn
    // Ensure are the rules the result must satisfy.
    Ensure []string
    // Previous is the response of the attempt before this one for Fn, empty on the first attempt.
    Previous string
    // Violations describes the rules Previous violates, empty when it satisfied them.
    Violations string
    // Results is why Previous failed verification, e.g.) the output of the compiler and the tests, empty when it passed.
    Results string
}

type Ruler

Ruler is implemented by generators that apply rules of their own in addition to the ones given, e.g.) defaults. responses are judged against the rules it returns, so what the judge considers a violation matches what the prompt asked for.

type Ruler interface {
    // Rules are all the rules the generator applies when it is given ensure.
    Rules(ensure ...string) []string
}

type Seq

Seq is the functions to generate tests for.

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

func From

func From(q sqlx.Queryer, sb squirrel.SelectBuilder) Seq

From runs sb against q, e.g.) a database opened with egdb.Open. q belongs to the caller and is not closed.

func FromDefault

func FromDefault(sb squirrel.SelectBuilder) Seq

FromDefault runs sb against the coverage recorded by the last successful run of the default workload, see FromWorkload.

func FromPath

func FromPath(path string, sb squirrel.SelectBuilder) Seq

FromPath runs sb against the coverage recorded in the database file at path, e.g.) the file provided by egdb.Latest. the file is opened when iteration starts and closed when it ends, including when it stops early.

func FromRun

func FromRun(sb squirrel.SelectBuilder) Seq

FromRun runs sb against the coverage recorded by this run.

func FromSpecified

func FromSpecified(fns ...Fn) Seq

FromSpecified builds a Seq directly from a fixed set of functions, useful for driving a Generator without recorded coverage data (e.g. in tests or one-off manual runs). paths are made relative to the working directory like recorded coverage.

func FromWorkload

func FromWorkload(workload string, sb squirrel.SelectBuilder) Seq

FromWorkload runs sb against the coverage recorded by the last successful run of the workload, instead of the coverage recorded by this run, see FromRun. the run is looked up, see egdb.Latest, while the functions are selected, which fails when the workload has no successful run.

func (Seq) Limit

func (t Seq) Limit(n uint) Seq

Limit bounds the functions to at most n of them, e.g.) the 5 least covered of Worst. filters apply before the limit, so the limit is of the functions that match. 0 is all of them.

func (Seq) Until

func (t Seq) Until(grace time.Duration) Seq

Until stops the functions once less than grace remains before the deadline of the context they are consumed with, leaving the function in flight time to finish. a context without a deadline yields every function.

type Template

Template is an example test, either its source or a function loading it. functions are evaluated when the prompt is built, an error fails the prompt.

type Template interface {
    // contains filtered or unexported methods
}
⬤