From a5fc3f257b767bad4724768999493d0e8e49b896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Fri, 27 Mar 2020 19:07:10 +0100 Subject: [PATCH] starlark/test: embed assert.star file --- starlark/test/assert.go | 2 +- starlark/test/assert.star.go | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 starlark/test/assert.star.go diff --git a/starlark/test/assert.go b/starlark/test/assert.go index b142ba6..d4d3932 100644 --- a/starlark/test/assert.go +++ b/starlark/test/assert.go @@ -69,7 +69,7 @@ func LoadAssertModule() (starlark.StringDict, error) { } filename := DataFile("starlark/test", "assert.star") thread := new(starlark.Thread) - assert, assertErr = starlark.ExecFile(thread, filename, nil, predeclared) + assert, assertErr = starlark.ExecFile(thread, filename, assertStarFile, predeclared) }) return assert, assertErr } diff --git a/starlark/test/assert.star.go b/starlark/test/assert.star.go new file mode 100644 index 0000000..9fdbccd --- /dev/null +++ b/starlark/test/assert.star.go @@ -0,0 +1,55 @@ +package test + +const assertStarFile = ` +# Predeclared built-ins for this module: +# +# error(msg): report an error in Go's test framework without halting execution. +# This is distinct from the built-in fail function, which halts execution. +# catch(f): evaluate f() and returns its evaluation error message, if any +# matches(str, pattern): report whether str matches regular expression pattern. +# module(**kwargs): a constructor for a module. +# _freeze(x): freeze the value x and everything reachable from it. +# +# Clients may use these functions to define their own testing abstractions. + +def _eq(x, y): + if x != y: + error("%r != %r" % (x, y)) + +def _ne(x, y): + if x == y: + error("%r == %r" % (x, y)) + +def _true(cond, msg = "assertion failed"): + if not cond: + error(msg) + +def _lt(x, y): + if not (x < y): + error("%s is not less than %s" % (x, y)) + +def _contains(x, y): + if y not in x: + error("%s does not contain %s" % (x, y)) + +def _fails(f, pattern): + "assert_fails asserts that evaluation of f() fails with the specified error." + msg = catch(f) + if msg == None: + error("evaluation succeeded unexpectedly (want error matching %r)" % pattern) + elif not matches(pattern, msg): + error("regular expression (%s) did not match error (%s)" % (pattern, msg)) + +freeze = _freeze # an exported global whose value is the built-in freeze function + +assert = module( + "assert", + fail = error, + eq = _eq, + ne = _ne, + true = _true, + lt = _lt, + contains = _contains, + fails = _fails, +) +`