wa-lang.org/wazero@v1.0.2/sys/error_test.go (about)

     1  package sys
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"wa-lang.org/wazero/internal/testing/require"
     8  )
     9  
    10  type notExitError struct {
    11  	moduleName string
    12  	exitCode   uint32
    13  }
    14  
    15  func (e *notExitError) Error() string {
    16  	return "not exit error"
    17  }
    18  
    19  func TestIs(t *testing.T) {
    20  	err := NewExitError("some module", 2)
    21  	tests := []struct {
    22  		name    string
    23  		target  error
    24  		matches bool
    25  	}{
    26  		{
    27  			name:    "same object",
    28  			target:  err,
    29  			matches: true,
    30  		},
    31  		{
    32  			name:    "same content",
    33  			target:  NewExitError("some module", 2),
    34  			matches: true,
    35  		},
    36  		{
    37  			name:    "different module name",
    38  			target:  NewExitError("not some module", 2),
    39  			matches: false,
    40  		},
    41  		{
    42  			name:    "different exit code",
    43  			target:  NewExitError("some module", 0),
    44  			matches: false,
    45  		},
    46  		{
    47  			name: "different type",
    48  			target: &notExitError{
    49  				moduleName: "some module",
    50  				exitCode:   2,
    51  			},
    52  			matches: false,
    53  		},
    54  	}
    55  
    56  	for _, tt := range tests {
    57  		tc := tt
    58  		t.Run(tc.name, func(t *testing.T) {
    59  			matches := errors.Is(err, tc.target)
    60  			require.Equal(t, tc.matches, matches)
    61  		})
    62  	}
    63  }