edit

package
v0.127.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2021 License: MIT Imports: 11 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OptionNotFoundError = &flux.Error{Code: codes.Invalid, Msg: "option not found"}

OptionNotFoundError variable is to handle the error gracefully in the client code

Functions

func DeleteOption added in v0.75.0

func DeleteOption(file *ast.File, name string)

DeleteOption removes an option if it exists. The file AST is mutated in place.

func DeleteProperty added in v0.75.0

func DeleteProperty(obj *ast.ObjectExpression, key string)

DeleteProperty removes a property from the object expression if it exists. The object expression AST is mutated in place.

func GetOption added in v0.75.0

func GetOption(file *ast.File, name string) (ast.Expression, error)

GetOption finds and returns the init for the option's variable assignment

func GetProperty added in v0.75.0

func GetProperty(obj *ast.ObjectExpression, key string) (ast.Expression, error)

GetProperty finds and returns the AST node for the property value.

func HasDuplicateOptions added in v0.81.0

func HasDuplicateOptions(file *ast.File, name string) bool

HasDuplicateOptions determines whether or not there are multiple assignments to the same option variable.

func Match added in v0.20.0

func Match(node ast.Node, pattern ast.Node, matchSlicesFuzzy bool) []ast.Node

Match takes an AST and a pattern and returns the nodes of the AST that match the given pattern. The pattern is an AST in turn, but it is partially specified, i.e. nil nodes are ignored while matching. In the case of slices (e.g. ast.File.Body, ast.CallExpression.Arguments) the matching mode can be "exact" or "fuzzy". Let ps be a slice of nodes in the pattern and ns in node. In "exact" mode, slices match iff len(ps) == len(ns) and every non-nil node ps[i] matches ns[i]. In "fuzzy" mode, slices match iff ns is a superset of ps; e.g. if ps is empty, it matches every slice.

func Option

func Option(node ast.Node, optionIdentifier string, fn OptionFn) (bool, error)

`Option` passes the `OptionStatement` in the AST rooted at `node` that has the specified identifier to `fn`. The function can have side effects on the option statement and/or return a non-nil `Expression` that is set as value for the option. If the value returned by the edit function is `nil` (or an error is returned) no new value is set for the option statement (but any, maybe partial, side effect is applied). `Option` returns whether it could find and edit the option (possibly with errors) or not.

func SetOption added in v0.75.0

func SetOption(file *ast.File, name string, expr ast.Expression)

SetOption replaces an existing option's init with the provided init or adds the option if it doesn't exist. The file AST is mutated in place.

func SetProperty added in v0.75.0

func SetProperty(obj *ast.ObjectExpression, key string, value ast.Expression)

SetProperty replaces an existing property definition with the provided object expression or adds the property if it doesn't exist. The object expression AST is mutated in place.

func TestcaseTransform added in v0.98.0

func TestcaseTransform(ctx context.Context, pkg *ast.Package, modules TestModules) ([]string, []*ast.Package, error)

TestcaseTransform will transform an *ast.Package into a set of *ast.Package values that represent each testcase defined within the original package.

A testcase is defined with the testcase statement such as below.

import "testing/assert"
myVar = 4
testcase addition {
    assert.equal(want: 2 + 2, got: myVar)
}

This gets transformed into a package that looks like this:

import "testing/assert"
myVar = 4
assert.equal(want: 2 + 2, got: myVar)

It is allowed to include options within the testcase block as they will be extracted to the top level.

In addition to this syntax, testcase blocks may also extend another test file. This will transform the the extended testcase in a slightly different way. The syntax for extending is as such:

import "math"
testcase addition_v2 extends "math_test" {
    option math.enable_v2 = true
    math_test.test_addition()
}

This transforms the `math_test` file with the addition testcase into:

import "testing/assert"
math_test = () => {
    myVar = 4
    test_addition = () => {
        assert.equal(want: 2 + 2, got: myVar)
        return {}
    }
    return {myVar, test_addition}
}()

The extended test file will be prepended to the list of files in the package as its own file.

If a testcase extends another testcase, it will be replaced with the given body.

test_invalid_import = () => {
    die(msg: "cannot extend an extended testcase")
}

It is allowed for an imported testcase to have an option, but no attempt is made to remove duplicate options. If there is a duplicate option, this will likely cause an error when the test is actually run.

Types

type OptionFn

type OptionFn func(opt *ast.OptionStatement) (ast.Expression, error)

`OptionFn` is a function that, provided with an `OptionStatement`, returns an `Expression` or an error. It is used by `Option` functions to edit AST's options statements.

func OptionObjectFn

func OptionObjectFn(keyMap map[string]ast.Expression) OptionFn

Creates an `OptionFn` for updating the values of an `OptionStatement` that has an `ObjectExpression` as value. Returns error if the child of the option statement is not an object expression. If some key is not a property of the object it is added.

func OptionValueFn

func OptionValueFn(expr ast.Expression) OptionFn

Creates an `OptionFn` for setting the value of an `OptionStatement`.

type TestModules added in v0.110.0

type TestModules map[string]filesystem.Service

func (*TestModules) Add added in v0.110.0

func (m *TestModules) Add(name string, fs filesystem.Service) error

func (*TestModules) Merge added in v0.110.0

func (m *TestModules) Merge(other TestModules) error

Jump to

Keyboard shortcuts

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