bitbucket.org/ai69/amoy@v0.2.3/shell_test.go (about)

     1  package amoy
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  )
     7  
     8  func TestCurrentFunctionName(t *testing.T) {
     9  	tests := []struct {
    10  		name string
    11  		want string
    12  	}{
    13  		{"Repeat1", "bitbucket.org/ai69/amoy.TestCurrentFunctionName.func1"},
    14  		{"Repeat2", "bitbucket.org/ai69/amoy.TestCurrentFunctionName.func1"},
    15  		{"Repeat3", "bitbucket.org/ai69/amoy.TestCurrentFunctionName.func1"},
    16  	}
    17  	for _, tt := range tests {
    18  		t.Run(tt.name, func(t *testing.T) {
    19  			if got := CurrentFunctionName(); got != tt.want {
    20  				t.Errorf("CurrentFunctionName() = %v, want %v", got, tt.want)
    21  			}
    22  		})
    23  	}
    24  }
    25  
    26  func myFunc1() {}
    27  func myFunc2() {}
    28  func myFunc3() {}
    29  
    30  func TestFunctionName(t *testing.T) {
    31  	tests := []struct {
    32  		name string
    33  		f    interface{}
    34  		want string
    35  	}{
    36  		{"Func1", myFunc1, "bitbucket.org/ai69/amoy.myFunc1"},
    37  		{"Func2", myFunc2, "bitbucket.org/ai69/amoy.myFunc2"},
    38  		{"Func3", myFunc3, "bitbucket.org/ai69/amoy.myFunc3"},
    39  	}
    40  	for _, tt := range tests {
    41  		t.Run(tt.name, func(t *testing.T) {
    42  			if got := FunctionName(tt.f); got != tt.want {
    43  				t.Errorf("FunctionName() = %v, want %v", got, tt.want)
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func TestShortFunctionName(t *testing.T) {
    50  	tests := []struct {
    51  		name string
    52  		f    interface{}
    53  		want string
    54  	}{
    55  		{"Func1", myFunc1, "myFunc1"},
    56  		{"Func2", myFunc2, "myFunc2"},
    57  		{"Func3", myFunc3, "myFunc3"},
    58  	}
    59  	for _, tt := range tests {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			if got := ShortFunctionName(tt.f); got != tt.want {
    62  				t.Errorf("ShortFunctionName() = %v, want %v", got, tt.want)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func TestIsInterfaceNil(t *testing.T) {
    69  	type customStruct struct {
    70  		Number int
    71  	}
    72  	type customInterface interface{}
    73  	var (
    74  		sliceNil    []int
    75  		mapNil      map[string]int
    76  		strNil      *string
    77  		structEmpty customStruct
    78  		structNil   *customStruct
    79  		chanNil     chan int
    80  		funcNil     func(string) error
    81  	)
    82  	tests := []struct {
    83  		name string
    84  		i    interface{}
    85  		want bool
    86  	}{
    87  		{"Nil", nil, true},
    88  		{"Int 0", 0, false},
    89  		{"Int 1", 1, false},
    90  		{"String Empty", "", false},
    91  		{"String Has", "Hello", false},
    92  		{"Slice Nil", sliceNil, true},
    93  		{"Slice Empty", []int{}, false},
    94  		{"Slice Has", []int{1, 2, 3}, false},
    95  		{"Map Nil", mapNil, true},
    96  		{"Map Empty", map[string]int{}, false},
    97  		{"Map Has", map[string]int{"a": 1, "b": 2}, false},
    98  		{"Ptr String Nil", strNil, true},
    99  		{"Ptr Struct Nil", structNil, true},
   100  		{"Ptr Struct Empty", &structEmpty, false},
   101  		{"Struct Empty", structEmpty, false},
   102  		{"Unsafe Pointer Nil", unsafe.Pointer(nil), true},
   103  		{"Unsafe Pointer Struct Nil", unsafe.Pointer(structNil), true},
   104  		{"Unsafe Pointer Struct Empty", unsafe.Pointer(&structEmpty), false},
   105  		{"Custom Interface Nil", customInterface(nil), true},
   106  		{"Custom Interface Struct Nil", customInterface(structNil), true},
   107  		{"Custom Interface Struct Empty", customInterface(&structEmpty), false},
   108  		{"Chan Nil", chanNil, true},
   109  		{"Chan Empty", make(chan int), false},
   110  		{"Chan Has", make(chan int, 1), false},
   111  		{"Func Nil", funcNil, true},
   112  		{"Func Has", func(string) error { return nil }, false},
   113  	}
   114  	for _, tt := range tests {
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			if got := IsInterfaceNil(tt.i); got != tt.want {
   117  				t.Errorf("IsInterfaceNil() = %v, want %v", got, tt.want)
   118  			}
   119  		})
   120  	}
   121  }