github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/test.go (about)

     1  package starlib
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/emcfarlane/larking/starlib/starlarkthread"
     7  	"github.com/emcfarlane/starlarkassert"
     8  	"go.starlark.net/starlark"
     9  )
    10  
    11  // RunTests calls starlarkassert.RunTests with options for larking libraries.
    12  // To use add it to a Test function:
    13  //
    14  // 	func TestStarlark(t *testing.T) {
    15  // 		starlib.RunTests(b, "testdata/*.star", nil)
    16  // 	}
    17  //
    18  func RunTests(t *testing.T, pattern string, globals starlark.StringDict) {
    19  	t.Helper()
    20  
    21  	g := NewGlobals()
    22  	for key, val := range globals {
    23  		g[key] = val
    24  	}
    25  	loader := NewLoader(g)
    26  
    27  	starlarkassert.RunTests(
    28  		t, pattern, g,
    29  		starlarkthread.AssertOption,
    30  		starlarkassert.WithLoad(loader.Load),
    31  		func(_ testing.TB, thread *starlark.Thread) func() {
    32  			thread.Name = "file://./?metadata=skip"
    33  			return nil
    34  		},
    35  	)
    36  }
    37  
    38  // RunBenches calls starlarkassert.RunBenches with options for larking libraries.
    39  // To use add it to a Benchmark function:
    40  //
    41  // 	func BenchmarkStarlark(b *testing.B) {
    42  // 		starlib.RunBenches(b, "testdata/*.star", nil)
    43  // 	}
    44  //
    45  func RunBenches(b *testing.B, pattern string, globals starlark.StringDict) {
    46  	b.Helper()
    47  
    48  	g := NewGlobals()
    49  	for key, val := range globals {
    50  		g[key] = val
    51  	}
    52  	loader := NewLoader(g)
    53  
    54  	starlarkassert.RunBenches(
    55  		b, pattern, g,
    56  		starlarkthread.AssertOption,
    57  		starlarkassert.WithLoad(loader.Load),
    58  		func(_ testing.TB, thread *starlark.Thread) func() {
    59  			thread.Name = "file://./?metadata=skip"
    60  			return nil
    61  		},
    62  	)
    63  }