Categorygithub.com/emcfarlane/starlarkassert
repositorypackage
0.0.0-20230412162503-620f4793c3ff
Repository: https://github.com/emcfarlane/starlarkassert.git
Documentation: pkg.go.dev

# 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.

ParameterTypeDescription
msgstringMessage.

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.

ParameterTypeDescription
msgvalueMessage.

test·freeze

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

ParameterTypeDescription
valvalueValue to freeze.

test·run

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

ParameterTypeDescription
subtestfunctionFunction 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.

ParameterTypeDescription
avalueValue expected.
bvalueValue given.

test·not_equal

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

ParameterTypeDescription
avalueValue expected.
bvalueValue given.

test·less_than

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

ParameterTypeDescription
avalueValue expected.
bvalueValue given.

test·true

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

ParameterTypeDescription
avalueValue expected to be truthy.
msgstringMessage to report on falsyness.

test·contains

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

ParameterTypeDescription
aiterableIterable item.
bvalueValue expected.

test·fails

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

ParameterTypeDescription
ffunctionvalue to run.
patternstringRegex 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.