github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+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  
    12  var (
    13  	assignedTwice = "foo"
    14  )
    15  
    16  var assignWithoutType = "bla"
    17  
    18  var aWT1, aWT2 = "foo", "bar"
    19  
    20  var declOnly1, declOnly2 string
    21  
    22  var (
    23  	groupedDecl1               string
    24  	groupedDecl2, groupedDecl3 string
    25  )
    26  
    27  var (
    28  	groupedDeclOnlyIntf interface{}
    29  	nonConstantAssign   = fmt.Errorf("foo")
    30  )
    31  
    32  var (
    33  	nil1 interface{} = nil
    34  )
    35  
    36  var (
    37  	f1 func() string
    38  	f2 = debug
    39  	f3 = f1
    40  )
    41  
    42  func debug() string {
    43  	return "hahaha"
    44  }
    45  
    46  type someStuff interface{}
    47  type someStruct struct{}
    48  
    49  var (
    50  	_ someStuff = &someStruct{}
    51  	_           = "assign to no name"
    52  )
    53  
    54  func init() {
    55  	groupedDecl1 = "foo"
    56  }
    57  
    58  func init() {
    59  	groupedDecl2 = "urgh"
    60  }
    61  
    62  func init() {
    63  	assignedTwice = "bar"
    64  }
    65  
    66  func main() {
    67  	if err := verify(); err != nil {
    68  		log.Fatalln(err)
    69  	}
    70  }
    71  
    72  func verify() error {
    73  	for _, tt := range []struct {
    74  		name  string
    75  		thing *string
    76  		want  string
    77  	}{
    78  		{
    79  			name:  "assignWithoutType",
    80  			thing: &assignWithoutType,
    81  			want:  "bla",
    82  		},
    83  		{
    84  			name:  "aWT1",
    85  			thing: &aWT1,
    86  			want:  "foo",
    87  		},
    88  		{
    89  			name:  "aWT2",
    90  			thing: &aWT2,
    91  			want:  "bar",
    92  		},
    93  		{
    94  			name:  "declOnly1",
    95  			thing: &declOnly1,
    96  			want:  "",
    97  		},
    98  		{
    99  			name:  "declOnly2",
   100  			thing: &declOnly2,
   101  			want:  "",
   102  		},
   103  		{
   104  			name:  "groupedDecl1",
   105  			thing: &groupedDecl1,
   106  			want:  "foo",
   107  		},
   108  		{
   109  			name:  "groupedDecl2",
   110  			thing: &groupedDecl2,
   111  			want:  "urgh",
   112  		},
   113  		{
   114  			name:  "groupedDecl3",
   115  			thing: &groupedDecl3,
   116  			want:  "",
   117  		},
   118  	} {
   119  		if got := *tt.thing; got != tt.want {
   120  			return fmt.Errorf("%s is %s, want %s", tt.name, got, tt.want)
   121  		}
   122  	}
   123  
   124  	if f1 != nil {
   125  		return fmt.Errorf("f1 is non-nil, want nil")
   126  	}
   127  	if got := f2(); got != "hahaha" {
   128  		return fmt.Errorf("f2 should return hahaha, but got %q", got)
   129  	}
   130  	if f3 != nil {
   131  		return fmt.Errorf("f3 is non-nil, want nil")
   132  	}
   133  
   134  	if nil1 != interface{}(nil) {
   135  		return fmt.Errorf("nil1 is %v, want nil interface", nil1)
   136  	}
   137  
   138  	return nil
   139  }