starlarkassert

package module
v0.0.0-...-620f479 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: BSD-3-Clause Imports: 16 Imported by: 1

README

starlarkassert

Go Reference

Package starlarkassert binds starlark scripts to go's testing framework. Create tests in starlark files by prefixing starlark functions with test_ that take one arg t. Each function will be bound to go's test runner. Set the test files in a root Go project and provide any global functions or methods to callback. Than run go test as usual.

# tests are prefixed with "test_"
def test_are_prefix(assert):
    assert.true(True)
    print("here")  # print formats in go's t.Log(...)

# tests can run subtests with "assert.run()"
def test_subtest(assert):
    for name in ["test", "names"]:
        assert.run(name, lambda assert: print(name))
# benches are prefixed with "bench_"
def bench_append(b):
    a = []
    b.restart()
    for i in range(b.n):
        a.append(i)

Integrate starlark Scripts with go's test framework:

func TestScript(t *testing.T) {
	globals := starlark.StringDict{
		"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
	}
	RunTests(t, "testdata/*.star", globals)
}

Then run tests like you would with Go:

$ go test -v .
=== RUN   TestRunTests
=== RUN   TestRunTests/test_here
    testdata/test.star:6:10 here
=== RUN   TestRunTests/test_array
    testdata/test.star:11:14 name: lord
    testdata/test.star:11:14 name: of
    testdata/test.star:11:14 name: the
    testdata/test.star:11:14 name: rings
=== RUN   TestRunTests/test_t_run
=== RUN   TestRunTests/test_t_run/harry
    testdata/test.star:16:36 harry
=== RUN   TestRunTests/test_t_run/potter
    testdata/test.star:16:36 potter
=== RUN   TestRunTests/test_globals
=== RUN   TestRunTests/test_globals_frozen
=== RUN   TestRunTests/test_load
    testdata/test.star:35:10 hello, world
--- PASS: TestRunTests (0.00s)
    --- PASS: TestRunTests/test_here (0.00s)
    --- PASS: TestRunTests/test_array (0.00s)
    --- PASS: TestRunTests/test_t_run (0.00s)
        --- PASS: TestRunTests/test_t_run/harry (0.00s)
        --- PASS: TestRunTests/test_t_run/potter (0.00s)
    --- PASS: TestRunTests/test_globals (0.00s)
    --- PASS: TestRunTests/test_globals_frozen (0.00s)
    --- PASS: TestRunTests/test_load (0.00s)
=== RUN   Test_depsInterface
    testing_test.go:28: 
--- SKIP: Test_depsInterface (0.00s)
PASS
ok  	github.com/emcfarlane/starlarkassert	(cached)

test

test·error

t.error(msg) reports the error msg to the test runner.

Parameter Type Description
msg string Message.
test·fail

t.fail() fails the test and halts test running.

test·fatal

t.fatal(msg) reports the error msg to the test runner, and fails the test.

Parameter Type Description
msg value Message.
test·freeze

t.freeze(val) the value, for testing freeze behaviour. All mutations after freeze should fail.

Parameter Type Description
val value Value to freeze.
test·run

t.run(subtest) runs the function with a test instance as the first arg.

Parameter Type Description
subtest function Function to run as a subtest.
test·skip

t.skip() skips the current test.

test·equal

t.equal(a, b) compares two values of the same type are equal. If the value is diffable it will report the difference between the two.

Parameter Type Description
a value Value expected.
b value Value given.
test·not_equal

t.not_equal(a, b) compares two values of the same type are not equal, r

Parameter Type Description
a value Value expected.
b value Value given.
test·less_than

t.less_than(a, b) compares two values of the same type are less than.

Parameter Type Description
a value Value expected.
b value Value given.
test·true

t.true(a, msg) checks truthyness reporting the message if falsy.

Parameter Type Description
a value Value expected to be truthy.
msg string Message to report on falsyness.
test·contains

t.contains(a, b) checks b is in a.

Parameter Type Description
a iterable Iterable item.
b value Value expected.
test·fails

t.fails(f, pattern) runs the function and checks the returned error matches the regex pattern.

Parameter Type Description
f function value to run.
pattern string Regex pattern to match.

bench

Bench is a superset of test. All attributes are included plus the following.

bench·restart

b.restart() the benchmark clock.

bench·start

b.start() the benchmark clock.

bench·stop

b.stop() the benchmark clock.

bench·n

b.n returns the current benchmark iteration size.

Documentation

Overview

Package starlarkassert is an extension of go.starlark.net/starlarktest to integrate into go's testing pacakge.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BenchFile

func BenchFile(b *testing.B, filename string, src interface{}, globals starlark.StringDict, opts ...TestOption)

BenchFile runs each function with the prefix "bench_" as a b.Run func.

func InParallel

func InParallel(t testing.TB, _ *starlark.Thread) func()

func RunBenches

func RunBenches(b *testing.B, pattern string, globals starlark.StringDict, opts ...TestOption)

RunBenches is a local bench suite runner. Each file in the pattern glob is ran. To use add it to a Benchmark function:

func BenchmarkStarlark(b *testing.B) {
	globals := starlark.StringDict{}
	RunBenches(b, "testdata/*.star", globals)
}

func RunTests

func RunTests(t *testing.T, pattern string, globals starlark.StringDict, opts ...TestOption)

RunTests is a local test suite runner. Each file in the pattern glob is ran. To use add it to a Test function:

func TestStarlark(t *testing.T) {
	globals := starlark.StringDict{}
	RunTests(t, "testdata/*_test.star", globals)
}

func TestFile

func TestFile(t *testing.T, filename string, src interface{}, globals starlark.StringDict, opts ...TestOption)

TestFile runs each function with the prefix "test_" as a t.Run func. To run in parallel, use the InParallel option.

Types

type Bench

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

Bench is passed to starlark benchmark functions. Interface is based on Go's *testing.B.

def bench_bar(b):
   for _ in range(b.n):
      ...work...

func NewBench

func NewBench(b *testing.B) *Bench

func (*Bench) Attr

func (b *Bench) Attr(name string) (starlark.Value, error)

func (*Bench) AttrNames

func (*Bench) AttrNames() []string

func (*Bench) Freeze

func (*Bench) Freeze()

func (*Bench) Hash

func (*Bench) Hash() (uint32, error)

func (*Bench) String

func (*Bench) String() string

func (*Bench) Truth

func (*Bench) Truth() starlark.Bool

func (*Bench) Type

func (*Bench) Type() string

type Diffable

type Diffable interface {
	starlark.Comparable

	// DiffSameType compares a value of the same Type().
	// Returns a human-readable report of the difference between the two values.
	// Returns an empty string if the two values are equal.
	// Implementation should be similar to cmp.Diff().
	DiffSameType(y starlark.Value) (string, error)
}

A Diffable is a value that can report it's difference.

type MatchStringOnly

type MatchStringOnly func(pat, str string) (bool, error)

MatchStringOnly is an implementation of the internal testing.testDeps interface. Interface is unstable and likely to break in new go versions. Current go 1.18.

func (MatchStringOnly) CheckCorpus

func (f MatchStringOnly) CheckCorpus([]any, []reflect.Type) error

func (MatchStringOnly) CoordinateFuzzing

func (f MatchStringOnly) CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error

func (MatchStringOnly) ImportPath

func (f MatchStringOnly) ImportPath() string

func (MatchStringOnly) MatchString

func (f MatchStringOnly) MatchString(pat, str string) (bool, error)

func (MatchStringOnly) ReadCorpus

func (f MatchStringOnly) ReadCorpus(string, []reflect.Type) ([]corpusEntry, error)

func (MatchStringOnly) ResetCoverage

func (f MatchStringOnly) ResetCoverage()

func (MatchStringOnly) RunFuzzWorker

func (f MatchStringOnly) RunFuzzWorker(func(corpusEntry) error) error

func (MatchStringOnly) SetPanicOnExit0

func (f MatchStringOnly) SetPanicOnExit0(bool)

func (MatchStringOnly) SnapshotCoverage

func (f MatchStringOnly) SnapshotCoverage()

func (MatchStringOnly) StartCPUProfile

func (f MatchStringOnly) StartCPUProfile(w io.Writer) error

func (MatchStringOnly) StartTestLog

func (f MatchStringOnly) StartTestLog(io.Writer)

func (MatchStringOnly) StopCPUProfile

func (f MatchStringOnly) StopCPUProfile()

func (MatchStringOnly) StopTestLog

func (f MatchStringOnly) StopTestLog() error

func (MatchStringOnly) WriteProfileTo

func (f MatchStringOnly) WriteProfileTo(string, io.Writer, int) error

type Test

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

Test is passed to starlark testing functions. Interface is based on Go's *testing.T.

def test_foo(t):
    ...check...

func NewTest

func NewTest(t *testing.T) *Test

func (*Test) Attr

func (t *Test) Attr(name string) (starlark.Value, error)

func (*Test) AttrNames

func (t *Test) AttrNames() []string

func (*Test) Freeze

func (t *Test) Freeze()

func (*Test) Hash

func (t *Test) Hash() (uint32, error)

func (*Test) String

func (t *Test) String() string

func (*Test) Truth

func (t *Test) Truth() starlark.Bool

func (*Test) Type

func (t *Test) Type() string

type TestOption

type TestOption func(t testing.TB, thread *starlark.Thread) func()

TestOption is called on setup with an optional cleanup func called on teardown.

func WithLoad

func WithLoad(load func(*starlark.Thread, string) (starlark.StringDict, error)) TestOption

WithLoad adds a loader to the thread. If the loader returns nil, the previous loader will be called.

Jump to

Keyboard shortcuts

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