github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/logging/logging_test.go (about)

     1  package logging
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/wasilibs/wazerox/internal/testing/require"
     8  )
     9  
    10  // TestLogScopes tests the bitset works as expected
    11  func TestLogScopes(t *testing.T) {
    12  	tests := []struct {
    13  		name   string
    14  		scopes LogScopes
    15  	}{
    16  		{
    17  			name:   "one is the smallest flag",
    18  			scopes: 1,
    19  		},
    20  		{
    21  			name:   "63 is the largest feature flag", // because uint64
    22  			scopes: 1 << 2,
    23  		},
    24  	}
    25  
    26  	for _, tt := range tests {
    27  		tc := tt
    28  
    29  		t.Run(tc.name, func(t *testing.T) {
    30  			f := LogScopes(0)
    31  
    32  			// Defaults to false
    33  			require.False(t, f.IsEnabled(tc.scopes))
    34  
    35  			// Set true makes it true
    36  			f = f | tc.scopes
    37  			require.True(t, f.IsEnabled(tc.scopes))
    38  
    39  			// Set false makes it false again
    40  			f = f ^ tc.scopes
    41  			require.False(t, f.IsEnabled(tc.scopes))
    42  		})
    43  	}
    44  }
    45  
    46  func TestLogScopes_String(t *testing.T) {
    47  	tests := []struct {
    48  		name     string
    49  		scopes   LogScopes
    50  		expected string
    51  	}{
    52  		{name: "none", scopes: LogScopeNone, expected: ""},
    53  		{name: "any", scopes: LogScopeAll, expected: "all"},
    54  		{name: "clock", scopes: LogScopeClock, expected: "clock"},
    55  		{name: "proc", scopes: LogScopeProc, expected: "proc"},
    56  		{name: "filesystem", scopes: LogScopeFilesystem, expected: "filesystem"},
    57  		{name: "poll", scopes: LogScopePoll, expected: "poll"},
    58  		{name: "random", scopes: LogScopeRandom, expected: "random"},
    59  		{name: "sock", scopes: LogScopeSock, expected: "sock"},
    60  		{name: "filesystem|random", scopes: LogScopeFilesystem | LogScopeRandom, expected: "filesystem|random"},
    61  		{name: "undefined", scopes: 1 << 14, expected: fmt.Sprintf("<unknown=%d>", 1<<14)},
    62  	}
    63  
    64  	for _, tt := range tests {
    65  		tc := tt
    66  		t.Run(tc.name, func(t *testing.T) {
    67  			require.Equal(t, tc.expected, tc.scopes.String())
    68  		})
    69  	}
    70  }