gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/uroot/test/foo/foo.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  
    11  	"github.com/u-root/u-root/pkg/uroot/test/bar"
    12  )
    13  
    14  var (
    15  	assignedTwice = "foo"
    16  )
    17  
    18  var assignWithoutType = "bla"
    19  
    20  var aWT1, aWT2 = "foo", "bar"
    21  
    22  var declOnly1, declOnly2 string
    23  
    24  var (
    25  	groupedDecl1               string
    26  	groupedDecl2, groupedDecl3 string
    27  )
    28  
    29  var (
    30  	groupedDeclOnlyIntf interface{}
    31  	nonConstantAssign   = fmt.Errorf("foo")
    32  )
    33  
    34  var (
    35  	nil1 interface{} = nil
    36  )
    37  
    38  var (
    39  	f1 func() string
    40  	f2 = debug
    41  	f3 = f1
    42  )
    43  
    44  func debug() string {
    45  	return "hahaha"
    46  }
    47  
    48  type someStuff interface{}
    49  type someStruct struct{}
    50  
    51  var (
    52  	_ someStuff = &someStruct{}
    53  	_           = "assign to no name"
    54  )
    55  
    56  func init() {
    57  	groupedDecl1 = "foo"
    58  }
    59  
    60  func init() {
    61  	groupedDecl2 = "urgh"
    62  }
    63  
    64  func init() {
    65  	assignedTwice = "bar"
    66  }
    67  
    68  func main() {
    69  	if err := verify(); err != nil {
    70  		log.Fatalln(err)
    71  	}
    72  }
    73  
    74  func verify() error {
    75  	for _, tt := range []struct {
    76  		name  string
    77  		thing *string
    78  		want  string
    79  	}{
    80  		{
    81  			name:  "assignWithoutType",
    82  			thing: &assignWithoutType,
    83  			want:  "bla",
    84  		},
    85  		{
    86  			name:  "aWT1",
    87  			thing: &aWT1,
    88  			want:  "foo",
    89  		},
    90  		{
    91  			name:  "aWT2",
    92  			thing: &aWT2,
    93  			want:  "bar",
    94  		},
    95  		{
    96  			name:  "declOnly1",
    97  			thing: &declOnly1,
    98  			want:  "",
    99  		},
   100  		{
   101  			name:  "declOnly2",
   102  			thing: &declOnly2,
   103  			want:  "",
   104  		},
   105  		{
   106  			name:  "groupedDecl1",
   107  			thing: &groupedDecl1,
   108  			want:  "foo",
   109  		},
   110  		{
   111  			name:  "groupedDecl2",
   112  			thing: &groupedDecl2,
   113  			want:  "urgh",
   114  		},
   115  		{
   116  			name:  "groupedDecl3",
   117  			thing: &groupedDecl3,
   118  			want:  "",
   119  		},
   120  	} {
   121  		if got := *tt.thing; got != tt.want {
   122  			return fmt.Errorf("%s is %s, want %s", tt.name, got, tt.want)
   123  		}
   124  	}
   125  
   126  	if f1 != nil {
   127  		return fmt.Errorf("f1 is non-nil, want nil")
   128  	}
   129  	if got := f2(); got != "hahaha" {
   130  		return fmt.Errorf("f2 should return hahaha, but got %q", got)
   131  	}
   132  	if f3 != nil {
   133  		return fmt.Errorf("f3 is non-nil, want nil")
   134  	}
   135  
   136  	if nil1 != interface{}(nil) {
   137  		return fmt.Errorf("nil1 is %v, want nil interface", nil1)
   138  	}
   139  
   140  	// Test unused method elimination.
   141  	var b bar.Interface
   142  	b = bar.Bar{}
   143  	b.UsedInterfaceMethod()
   144  
   145  	return nil
   146  }