github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/error_test.go (about)

     1  package runtime
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestAsError(t *testing.T) {
    10  	tests := []struct {
    11  		name      string
    12  		arg       error
    13  		wantRtErr *Error
    14  		wantOk    bool
    15  	}{
    16  		{
    17  			name:      "nil",
    18  			arg:       nil,
    19  			wantRtErr: nil,
    20  			wantOk:    false,
    21  		},
    22  		{
    23  			name:      "*Error",
    24  			arg:       NewError(StringValue("hello")),
    25  			wantRtErr: NewError(StringValue("hello")),
    26  			wantOk:    true,
    27  		},
    28  		{
    29  			name:      "string error",
    30  			arg:       errors.New("hello"),
    31  			wantRtErr: nil,
    32  			wantOk:    false,
    33  		},
    34  	}
    35  	for _, tt := range tests {
    36  		t.Run(tt.name, func(t *testing.T) {
    37  			gotRtErr, gotOk := AsError(tt.arg)
    38  			if !reflect.DeepEqual(gotRtErr, tt.wantRtErr) {
    39  				t.Errorf("AsError() gotRtErr = %v, want %v", gotRtErr, tt.wantRtErr)
    40  			}
    41  			if gotOk != tt.wantOk {
    42  				t.Errorf("AsError() gotOk = %v, want %v", gotOk, tt.wantOk)
    43  			}
    44  		})
    45  	}
    46  }
    47  
    48  func TestToError(t *testing.T) {
    49  	tests := []struct {
    50  		name string
    51  		arg  error
    52  		want *Error
    53  	}{
    54  		{
    55  			name: "nil",
    56  			arg:  nil,
    57  			want: nil,
    58  		},
    59  		{
    60  			name: "nil *Error",
    61  			arg:  (*Error)(nil),
    62  			want: nil,
    63  		},
    64  		{
    65  			name: "non nil *Error",
    66  			arg:  errors.New("hello"),
    67  			want: NewError(StringValue("hello")),
    68  		},
    69  		{
    70  			name: "string error",
    71  			arg:  errors.New("hi"),
    72  			want: NewError(StringValue("hi")),
    73  		},
    74  		// TODO: Add test cases.
    75  	}
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			if got := ToError(tt.arg); !reflect.DeepEqual(got, tt.want) {
    79  				t.Errorf("ToError() = %v, want %v", got, tt.want)
    80  			}
    81  		})
    82  	}
    83  }