github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/luatesting/runtests.go (about)

     1  package luatesting
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/fs"
     7  	"io/ioutil"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	rt "github.com/arnodel/golua/runtime"
    13  )
    14  
    15  // RunSource compiles and runs some source code, outputting to the
    16  // provided io.Writer.
    17  func RunSource(r *rt.Runtime, source []byte) {
    18  	t := r.MainThread()
    19  	// TODO: use the file name
    20  	clos, err := t.LoadFromSourceOrCode("luatest", source, "t", rt.TableValue(r.GlobalEnv()), false)
    21  	if err != nil {
    22  		fmt.Fprintf(r.Stdout, "!!! parsing: %s", err)
    23  		return
    24  	}
    25  	cerr := rt.Call(t, rt.FunctionValue(clos), nil, rt.NewTerminationWith(nil, 0, false))
    26  	if cerr != nil {
    27  		fmt.Fprintf(r.Stdout, "!!! runtime: %s", cerr)
    28  	}
    29  }
    30  
    31  // RunLuaTest runs the lua test code in source, running setup if non-nil
    32  // beforehand (with the Runtime instance that will be used in the test).
    33  func RunLuaTest(source []byte, setup func(*rt.Runtime) func()) error {
    34  	outputBuf := new(bytes.Buffer)
    35  	r := rt.New(outputBuf)
    36  	r.SetWarner(rt.NewLogWarner(outputBuf, "Test warning: "))
    37  	if setup != nil {
    38  		cleanup := setup(r)
    39  		defer cleanup()
    40  	}
    41  	checkers := ExtractLineCheckers(source)
    42  	RunSource(r, source)
    43  	r.Close(nil)
    44  	return CheckLines(outputBuf.Bytes(), checkers)
    45  }
    46  
    47  func RunLuaTestFile(t *testing.T, path string, setup func(*rt.Runtime) func()) {
    48  	if filepath.Ext(path) != ".lua" {
    49  		return
    50  	}
    51  	isQuotasTest := strings.HasSuffix(path, ".quotas.lua")
    52  	t.Run(path, func(t *testing.T) {
    53  		if isQuotasTest {
    54  			if !rt.QuotasAvailable {
    55  				t.Skip("Skipping quotas test as build does not enforce quotas")
    56  				return
    57  			}
    58  		}
    59  		src, err := ioutil.ReadFile(path)
    60  		if err != nil {
    61  			t.Error(err)
    62  			return
    63  		}
    64  
    65  		doRun, err := checkTags(src)
    66  		if !doRun {
    67  			if err != nil {
    68  				t.Error(err)
    69  			} else {
    70  				t.Skip("Skipping because of tags")
    71  			}
    72  			return
    73  		}
    74  
    75  		err = RunLuaTest(src, setup)
    76  		if err != nil {
    77  			t.Error(err)
    78  		}
    79  	})
    80  }
    81  
    82  // RunLuaTestsInDir runs a test for each .lua file in the directory provided.
    83  func RunLuaTestsInDir(t *testing.T, dirpath string, setup func(*rt.Runtime) func(), filters ...string) {
    84  	runTest := func(path string, entry fs.DirEntry, err error) error {
    85  		for _, filter := range filters {
    86  			if !strings.Contains(entry.Name(), filter) {
    87  				return nil
    88  			}
    89  		}
    90  		RunLuaTestFile(t, path, setup)
    91  		return nil
    92  	}
    93  	if err := filepath.WalkDir(dirpath, runTest); err != nil {
    94  		t.Error(err)
    95  	}
    96  }