github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/testdata/testing.go (about)

     1  package main
     2  
     3  // TODO: also test the verbose version.
     4  
     5  import (
     6  	"errors"
     7  	"flag"
     8  	"io"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestFoo(t *testing.T) {
    14  	t.Log("log Foo.a")
    15  	t.Log("log Foo.b")
    16  }
    17  
    18  func TestBar(t *testing.T) {
    19  	t.Log("log Bar")
    20  	t.Log("log g\nh\ni\n")
    21  	t.Run("Bar1", func(t *testing.T) {})
    22  	t.Run("Bar2", func(t *testing.T) {
    23  		t.Log("log Bar2\na\nb\nc")
    24  		t.Error("failed")
    25  		t.Log("after failed")
    26  	})
    27  	t.Run("Bar3", func(t *testing.T) {})
    28  	t.Log("log Bar end")
    29  }
    30  
    31  func TestAllLowercase(t *testing.T) {
    32  	names := []string {
    33  		"alpha",
    34  		"BETA",
    35  		"gamma",
    36  		"BELTA",
    37  	}
    38  
    39  	for _, name := range names {
    40  		t.Run(name, func(t *testing.T) {
    41  			if 'a' <= name[0] && name[0] <= 'a' {
    42  				t.Logf("expected lowercase name, and got one, so I'm happy")
    43  			} else {
    44  				t.Errorf("expected lowercase name, got %s", name)
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  var tests = []testing.InternalTest{
    51  	{"TestFoo", TestFoo},
    52  	{"TestBar", TestBar},
    53  	{"TestAllLowercase", TestAllLowercase},
    54  }
    55  
    56  var benchmarks = []testing.InternalBenchmark{}
    57  
    58  var fuzzes = []testing.InternalFuzzTarget{}
    59  
    60  var examples = []testing.InternalExample{}
    61  
    62  // A fake regexp matcher.
    63  // Inflexible, but saves 50KB of flash and 50KB of RAM per -size full,
    64  // and lets tests pass on cortex-m.
    65  // Must match the one in src/testing/match.go that is substituted on bare-metal platforms,
    66  // or "make test" will fail there.
    67  func fakeMatchString(pat, str string) (bool, error) {
    68  	if pat == ".*" {
    69  		return true, nil
    70  	}
    71  	matched := strings.Contains(str, pat)
    72  	return matched, nil
    73  }
    74  
    75  func main() {
    76  	if testing.Testing() {
    77  		println("not running a test at the moment, testing.Testing() should return false")
    78  	}
    79  	testing.Init()
    80  	flag.Set("test.run", ".*/B")
    81  	m := testing.MainStart(matchStringOnly(fakeMatchString /*regexp.MatchString*/), tests, benchmarks, fuzzes, examples)
    82  
    83  	exitcode := m.Run()
    84  	if exitcode != 0 {
    85  		println("exitcode:", exitcode)
    86  	}
    87  }
    88  
    89  var errMain = errors.New("testing: unexpected use of func Main")
    90  
    91  // matchStringOnly is part of upstream, and is used below to provide a dummy deps to pass to MainStart
    92  // so it can be run with go (tested with go 1.16) to provide a baseline for the regression test.
    93  // See c56cc9b3b57276.  Unfortunately, testdeps is internal, so we can't just use &testdeps.TestDeps{}.
    94  type matchStringOnly func(pat, str string) (bool, error)
    95  
    96  func (f matchStringOnly) MatchString(pat, str string) (bool, error)   { return f(pat, str) }
    97  func (f matchStringOnly) StartCPUProfile(w io.Writer) error           { return errMain }
    98  func (f matchStringOnly) StopCPUProfile()                             {}
    99  func (f matchStringOnly) WriteProfileTo(string, io.Writer, int) error { return errMain }
   100  func (f matchStringOnly) ImportPath() string                          { return "" }
   101  func (f matchStringOnly) StartTestLog(io.Writer)                      {}
   102  func (f matchStringOnly) StopTestLog() error                          { return errMain }
   103  func (f matchStringOnly) SetPanicOnExit0(bool)                        {}