github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/testing/require/require_errno_test.go (about)

     1  package require
     2  
     3  import (
     4  	"io"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/wasilibs/wazerox/experimental/sys"
     9  )
    10  
    11  func TestEqualErrno(t *testing.T) {
    12  	// This specifically chooses ENOENT and EIO as outside windows, they tend
    13  	// to have the same errno literal and text message.
    14  	if runtime.GOOS == "windows" {
    15  		t.Skipf("error literals are different on windows")
    16  	}
    17  
    18  	tests := []struct {
    19  		name        string
    20  		require     func(TestingT)
    21  		expectedLog string
    22  	}{
    23  		{
    24  			name: "EqualErrno passes on equal",
    25  			require: func(t TestingT) {
    26  				EqualErrno(t, sys.ENOENT, sys.ENOENT)
    27  			},
    28  		},
    29  		{
    30  			name: "EqualErrno fails on nil",
    31  			require: func(t TestingT) {
    32  				EqualErrno(t, sys.ENOENT, nil)
    33  			},
    34  			expectedLog: "expected a sys.Errno, but was nil",
    35  		},
    36  		{
    37  			name: "EqualErrno fails on not Errno",
    38  			require: func(t TestingT) {
    39  				EqualErrno(t, sys.ENOENT, io.EOF)
    40  			},
    41  			expectedLog: `expected EOF to be a sys.Errno`,
    42  		},
    43  		{
    44  			name: "EqualErrno fails on not equal",
    45  			require: func(t TestingT) {
    46  				EqualErrno(t, sys.ENOENT, sys.EIO)
    47  			},
    48  			expectedLog: `expected Errno 0xc(no such file or directory), but was 0x8(input/output error)`,
    49  		},
    50  		{
    51  			name: "EqualErrno fails on not equal with format",
    52  			require: func(t TestingT) {
    53  				EqualErrno(t, sys.ENOENT, sys.EIO, "pay me %d", 5)
    54  			},
    55  			expectedLog: `expected Errno 0xc(no such file or directory), but was 0x8(input/output error): pay me 5`,
    56  		},
    57  	}
    58  
    59  	for _, tt := range tests {
    60  		tc := tt
    61  		t.Run(tc.name, func(t *testing.T) {
    62  			m := &mockT{t: t}
    63  			tc.require(m)
    64  			m.require(tc.expectedLog)
    65  		})
    66  	}
    67  }