github.com/HaHadaxigua/yaegi@v1.0.1/interp/build_test.go (about)

     1  package interp
     2  
     3  import (
     4  	"go/build"
     5  	"testing"
     6  )
     7  
     8  type testBuild struct {
     9  	src string
    10  	res bool
    11  }
    12  
    13  func TestBuildTag(t *testing.T) {
    14  	// Assume a specific OS, arch and go version no matter the real underlying system
    15  	ctx := build.Context{
    16  		GOARCH:      "amd64",
    17  		GOOS:        "linux",
    18  		BuildTags:   []string{"foo"},
    19  		ReleaseTags: []string{"go1.11"},
    20  	}
    21  
    22  	tests := []testBuild{
    23  		{"// +build linux", true},
    24  		{"// +build windows", false},
    25  		{"// +build go1.9", true},
    26  		{"// +build go1.11", true},
    27  		{"// +build go1.12", false},
    28  		{"// +build !go1.10", false},
    29  		{"// +build !go1.12", true},
    30  		{"// +build ignore", false},
    31  		{"// +build linux,amd64", true},
    32  		{"// +build linux,i386", false},
    33  		{"// +build linux,i386 go1.11", true},
    34  		{"// +build linux\n// +build amd64", true},
    35  		{"// +build linux\n\n\n// +build amd64", true},
    36  		{"// +build linux\n// +build i386", false},
    37  		{"// +build foo", true},
    38  		{"// +build !foo", false},
    39  		{"// +build bar", false},
    40  	}
    41  
    42  	i := New(Options{})
    43  	for _, test := range tests {
    44  		test := test
    45  		src := test.src + "\npackage x"
    46  		t.Run(test.src, func(t *testing.T) {
    47  			if r, _ := i.buildOk(&ctx, "", src); r != test.res {
    48  				t.Errorf("got %v, want %v", r, test.res)
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func TestBuildFile(t *testing.T) {
    55  	// Assume a specific OS, arch and go pattern no matter the real underlying system
    56  	ctx := build.Context{
    57  		GOARCH: "amd64",
    58  		GOOS:   "linux",
    59  	}
    60  
    61  	tests := []testBuild{
    62  		{"foo/bar_linux_amd64.go", false},
    63  		{"foo/bar.go", false},
    64  		{"bar.go", false},
    65  		{"bar_linux.go", false},
    66  		{"bar_maix.go", false},
    67  		{"bar_mlinux.go", false},
    68  		{"bar_aix_foo.go", false},
    69  		{"bar_aix_s390x.go", true},
    70  		{"bar_aix_amd64.go", true},
    71  		{"bar_linux_arm.go", true},
    72  	}
    73  
    74  	for _, test := range tests {
    75  		test := test
    76  		t.Run(test.src, func(t *testing.T) {
    77  			if r := skipFile(&ctx, test.src, NoTest); r != test.res {
    78  				t.Errorf("got %v, want %v", r, test.res)
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func Test_goMinorVersion(t *testing.T) {
    85  	tests := []struct {
    86  		desc     string
    87  		context  build.Context
    88  		expected int
    89  	}{
    90  		{
    91  			desc: "stable",
    92  			context: build.Context{ReleaseTags: []string{
    93  				"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9", "go1.10", "go1.11", "go1.12",
    94  			}},
    95  			expected: 12,
    96  		},
    97  		{
    98  			desc: "devel/beta/rc",
    99  			context: build.Context{ReleaseTags: []string{
   100  				"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9", "go1.10", "go1.11", "go1.12", "go1.13",
   101  			}},
   102  			expected: 13,
   103  		},
   104  	}
   105  
   106  	for _, test := range tests {
   107  		test := test
   108  		t.Run(test.desc, func(t *testing.T) {
   109  			minor := goMinorVersion(&test.context)
   110  
   111  			if minor != test.expected {
   112  				t.Errorf("got %v, want %v", minor, test.expected)
   113  			}
   114  		})
   115  	}
   116  }