github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/internal/testmain/testmain_test.go (about)

     1  package testmain_test
     2  
     3  import (
     4  	gobuild "go/build"
     5  	"go/token"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/google/go-cmp/cmp/cmpopts"
    10  	"github.com/gopherjs/gopherjs/build"
    11  	"github.com/gopherjs/gopherjs/internal/srctesting"
    12  	. "github.com/gopherjs/gopherjs/internal/testmain"
    13  )
    14  
    15  func TestScan(t *testing.T) {
    16  	xctx := build.NewBuildContext("", nil)
    17  	pkg, err := xctx.Import("github.com/gopherjs/gopherjs/internal/testmain/testdata/testpkg", "", 0)
    18  	if err != nil {
    19  		t.Fatalf("Failed to import package: %s", err)
    20  	}
    21  
    22  	fset := token.NewFileSet()
    23  
    24  	got := TestMain{Package: pkg}
    25  	if err := got.Scan(fset); err != nil {
    26  		t.Fatalf("Got: tm.Scan() returned error: %s. Want: no error.", err)
    27  	}
    28  
    29  	want := TestMain{
    30  		TestMain: &TestFunc{Location: LocInPackage, Name: "TestMain"},
    31  		Tests: []TestFunc{
    32  			{Location: LocInPackage, Name: "TestXxx"},
    33  			{Location: LocExternal, Name: "TestYyy"},
    34  		},
    35  		Benchmarks: []TestFunc{
    36  			{Location: LocInPackage, Name: "BenchmarkXxx"},
    37  			{Location: LocExternal, Name: "BenchmarkYyy"},
    38  		},
    39  		Fuzz: []TestFunc{
    40  			{Location: LocInPackage, Name: "FuzzXxx"},
    41  			{Location: LocExternal, Name: "FuzzYyy"},
    42  		},
    43  		Examples: []ExampleFunc{
    44  			{Location: LocInPackage, Name: "ExampleXxx"},
    45  			{Location: LocExternal, Name: "ExampleYyy", Output: "hello\n"},
    46  		},
    47  	}
    48  	opts := cmp.Options{
    49  		cmpopts.IgnoreFields(TestMain{}, "Package"), // Inputs.
    50  	}
    51  	if diff := cmp.Diff(want, got, opts...); diff != "" {
    52  		t.Errorf("List of test function is different from expected (-want,+got):\n%s", diff)
    53  	}
    54  }
    55  
    56  func TestSynthesize(t *testing.T) {
    57  	pkg := &build.PackageData{
    58  		Package: &gobuild.Package{ImportPath: "foo/bar"},
    59  	}
    60  
    61  	tests := []struct {
    62  		descr   string
    63  		tm      TestMain
    64  		wantSrc string
    65  	}{
    66  		{
    67  			descr: "all tests",
    68  			tm: TestMain{
    69  				Package: pkg,
    70  				Tests: []TestFunc{
    71  					{Location: LocInPackage, Name: "TestXxx"},
    72  					{Location: LocExternal, Name: "TestYyy"},
    73  				},
    74  				Benchmarks: []TestFunc{
    75  					{Location: LocInPackage, Name: "BenchmarkXxx"},
    76  					{Location: LocExternal, Name: "BenchmarkYyy"},
    77  				},
    78  				Fuzz: []TestFunc{
    79  					{Location: LocInPackage, Name: "FuzzXxx"},
    80  					{Location: LocExternal, Name: "FuzzYyy"},
    81  				},
    82  				Examples: []ExampleFunc{
    83  					{Location: LocInPackage, Name: "ExampleXxx", EmptyOutput: true},
    84  					{Location: LocExternal, Name: "ExampleYyy", EmptyOutput: true},
    85  				},
    86  			},
    87  			wantSrc: allTests,
    88  		}, {
    89  			descr: "testmain",
    90  			tm: TestMain{
    91  				Package:  pkg,
    92  				TestMain: &TestFunc{Location: LocInPackage, Name: "TestMain"},
    93  			},
    94  			wantSrc: testmain,
    95  		}, {
    96  			descr: "import only",
    97  			tm: TestMain{
    98  				Package: pkg,
    99  				Examples: []ExampleFunc{
   100  					{Location: LocInPackage, Name: "ExampleXxx"},
   101  				},
   102  			},
   103  			wantSrc: importOnly,
   104  		},
   105  	}
   106  
   107  	for _, test := range tests {
   108  		t.Run(test.descr, func(t *testing.T) {
   109  			fset := token.NewFileSet()
   110  			_, src, err := test.tm.Synthesize(fset)
   111  			if err != nil {
   112  				t.Fatalf("Got: tm.Synthesize() returned error: %s. Want: no error.", err)
   113  			}
   114  			got := srctesting.Format(t, fset, src)
   115  			if diff := cmp.Diff(test.wantSrc, got); diff != "" {
   116  				t.Errorf("Different _testmain.go source (-want,+got):\n%s", diff)
   117  				t.Logf("Got source:\n%s", got)
   118  			}
   119  		})
   120  	}
   121  }
   122  
   123  const allTests = `package main
   124  
   125  import (
   126  	"os"
   127  
   128  	"testing"
   129  	"testing/internal/testdeps"
   130  
   131  	_test "foo/bar"
   132  	_xtest "foo/bar_test"
   133  )
   134  
   135  var tests = []testing.InternalTest{
   136  	{"TestXxx", _test.TestXxx},
   137  	{"TestYyy", _xtest.TestYyy},
   138  }
   139  
   140  var benchmarks = []testing.InternalBenchmark{
   141  	{"BenchmarkXxx", _test.BenchmarkXxx},
   142  	{"BenchmarkYyy", _xtest.BenchmarkYyy},
   143  }
   144  
   145  var fuzzTargets = []testing.InternalFuzzTarget{
   146  	{"FuzzXxx", _test.FuzzXxx},
   147  	{"FuzzYyy", _xtest.FuzzYyy},
   148  }
   149  
   150  var examples = []testing.InternalExample{
   151  	{"ExampleXxx", _test.ExampleXxx, "", false},
   152  	{"ExampleYyy", _xtest.ExampleYyy, "", false},
   153  }
   154  
   155  func main() {
   156  	m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, fuzzTargets, examples)
   157  
   158  	os.Exit(m.Run())
   159  }
   160  `
   161  
   162  const testmain = `package main
   163  
   164  import (
   165  	"testing"
   166  	"testing/internal/testdeps"
   167  
   168  	_test "foo/bar"
   169  )
   170  
   171  var tests = []testing.InternalTest{}
   172  
   173  var benchmarks = []testing.InternalBenchmark{}
   174  
   175  var fuzzTargets = []testing.InternalFuzzTarget{}
   176  
   177  var examples = []testing.InternalExample{}
   178  
   179  func main() {
   180  	m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, fuzzTargets, examples)
   181  
   182  	_test.TestMain(m)
   183  }
   184  `
   185  
   186  const importOnly = `package main
   187  
   188  import (
   189  	"os"
   190  
   191  	"testing"
   192  	"testing/internal/testdeps"
   193  
   194  	_ "foo/bar"
   195  )
   196  
   197  var tests = []testing.InternalTest{}
   198  
   199  var benchmarks = []testing.InternalBenchmark{}
   200  
   201  var fuzzTargets = []testing.InternalFuzzTarget{}
   202  
   203  var examples = []testing.InternalExample{}
   204  
   205  func main() {
   206  	m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, fuzzTargets, examples)
   207  
   208  	os.Exit(m.Run())
   209  }
   210  `