examples

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package examples reads the fenced code blocks of the pages, runs them and checks their output (spec 07).

Index

Constants

This section is empty.

Variables

View Source
var Expects = []string{"compile", "runtime", "exit", "exception", "error", "panic", "timeout", "sanitizer"}

Expects are the kinds of failure signature of spec 07.2.

View Source
var NorunReasons = []string{"network", "gui", "platform", "hardware", "ub", "vulnerable", "fragment", "pseudo", "long", "interactive"}

NorunReasons are the codes of spec 07.8.

Functions

func Clean

func Clean(s string) string

Clean gives the text that -update writes in an output block: the run output with the line ends, the colour codes and the trailing spaces made the same as Normalize makes them.

func Collect

func Collect(file, src string, line0 int) ([]Case, []Issue)

Collect gives the cases of one Markdown file, and the issues of the rules X01 and X02. The blocks of the languages that prog run can run need a mode; the other blocks, such as text or json, do not.

func LangID

func LangID(name string) string

LangID gives the language id of a name in an info string.

func Normalize

func Normalize(s string, extra []string) string

Normalize applies the default normalizers of spec 07.6 and the extra ones that are named: addr, time, tmp, hash and float:<digits>.

func Stream

func Stream(got lang.RunResult, stream string) string

Stream gives the text of a stream of the run: stdout, stderr or both.

func Update

func Update(src string, results []Result) (string, int)

Update writes the output of the runs into the output blocks of a file (prog run -update). It replaces the text of an output block that differs from the run, and adds an output block after a run or wrong block that has none. It does not change an output block with match=regex or match=any, because a person wrote the pattern. The results must be of the cases of src. It returns the new text and the number of blocks that changed, and it sets Updated on each result whose blocks now match the run.

Types

type Block

type Block struct {
	// Lang is the language id after the aliases, such as python or cpp. It is "output" or "input" for those blocks.
	Lang  string
	Info  string
	Attrs map[string]string
	// Words are the attributes without a value, such as run or fails.
	Words []string
	Code  string
	// Line is the line of the opening fence, from 1.
	Line int
	// Start and End are the byte offsets of the code in the file. Start is the first byte after the opening fence line and End is the first byte of the closing fence line.
	Start, End int
	// Closed is false when the file ends before the closing fence.
	Closed bool
	// After is the byte offset just after the closing fence line.
	After int
}

Block is one fenced code block of a Markdown file.

func Parse

func Parse(src string, line0 int) []Block

Parse finds the fenced code blocks of a Markdown file, as CommonMark defines them: a fence of three or more backticks or tildes, indented by at most three spaces, closed by a fence of the same character that is at least as long. line0 is the line of the file where src starts, from 1.

func (*Block) Has

func (b *Block) Has(w string) bool

Has tells if the block has a word attribute.

func (*Block) Mode

func (b *Block) Mode() string

Mode is the run mode of the block: run, fails, wrong or norun. It is "" when the block has none, and "many" when it has more than one.

type Case

type Case struct {
	File string
	// Block is the block that owns the run: the block itself, or the last runnable block of a group.
	Block *Block
	// Parts are the blocks of the program in the order of the page. A block without file= adds its code to the main file of the language.
	Parts   []*Block
	Outputs []*Block
	// Stdin is the text of the input block that stdin= names.
	Stdin string
}

Case is one program to run: a block, or the blocks of a group, with its output blocks.

func (*Case) Files

func (c *Case) Files(p lang.Program) map[string]string

Files gives the files of the program. The parts without file= go to the main file of the language, in order.

func (*Case) ID

func (c *Case) ID() string

ID is the id of the case: the id= of the block, or ex<n> from the order of the blocks.

func (*Case) Mode

func (c *Case) Mode() string

Mode is run, fails or wrong.

type Issue

type Issue struct {
	File string
	Line int
	Rule string
	Msg  string
}

Issue is a broken rule of spec 07.2.

func (Issue) String

func (i Issue) String() string

type Result

type Result struct {
	File   string `json:"file"`
	Line   int    `json:"line"`
	ID     string `json:"id"`
	Lang   string `json:"lang"`
	Mode   string `json:"mode"`
	Status string `json:"status"` // pass, fail or skip
	Why    string `json:"why,omitempty"`
	Tool   string `json:"tool,omitempty"`
	Ms     int64  `json:"ms"`
	// Updated is set by Update when the output blocks of the case were written from this run.
	Updated bool `json:"updated,omitempty"`
	// Got is the output of the run, for -update.
	Got lang.RunResult `json:"-"`
	// Case is the case of the result.
	Case *Case `json:"-"`
}

Result is the result of one case.

type Runner

type Runner struct {
	R *lang.Runner
	// contains filtered or unexported fields
}

Runner runs the cases. It keeps the version of each toolchain.

func (*Runner) Run

func (x *Runner) Run(c *Case) Result

Run runs one case and checks it.

Jump to

Keyboard shortcuts

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