github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/dump_test.go (about)

     1  package fs
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  // Check it satisfies the interfaces
    12  var (
    13  	_ flagger   = (*DumpFlags)(nil)
    14  	_ flaggerNP = DumpFlags(0)
    15  )
    16  
    17  func TestDumpFlagsString(t *testing.T) {
    18  	assert.Equal(t, "", DumpFlags(0).String())
    19  	assert.Equal(t, "headers", (DumpHeaders).String())
    20  	assert.Equal(t, "headers,bodies", (DumpHeaders | DumpBodies).String())
    21  	assert.Equal(t, "headers,bodies,requests,responses,auth,filters", (DumpHeaders | DumpBodies | DumpRequests | DumpResponses | DumpAuth | DumpFilters).String())
    22  	assert.Equal(t, "headers,Unknown-0x8000", (DumpHeaders | DumpFlags(0x8000)).String())
    23  }
    24  
    25  func TestDumpFlagsSet(t *testing.T) {
    26  	for _, test := range []struct {
    27  		in      string
    28  		want    DumpFlags
    29  		wantErr string
    30  	}{
    31  		{"", DumpFlags(0), ""},
    32  		{"bodies", DumpBodies, ""},
    33  		{"bodies,headers,auth", DumpBodies | DumpHeaders | DumpAuth, ""},
    34  		{"bodies,headers,auth", DumpBodies | DumpHeaders | DumpAuth, ""},
    35  		{"headers,bodies,requests,responses,auth,filters", DumpHeaders | DumpBodies | DumpRequests | DumpResponses | DumpAuth | DumpFilters, ""},
    36  		{"headers,bodies,unknown,auth", 0, "invalid choice \"unknown\""},
    37  	} {
    38  		f := DumpFlags(0xffffffffffffffff)
    39  		initial := f
    40  		err := f.Set(test.in)
    41  		if err != nil {
    42  			if test.wantErr == "" {
    43  				t.Errorf("Got an error when not expecting one on %q: %v", test.in, err)
    44  			} else {
    45  				assert.Contains(t, err.Error(), test.wantErr)
    46  			}
    47  			assert.Equal(t, initial, f, test.want)
    48  		} else {
    49  			if test.wantErr != "" {
    50  				t.Errorf("Got no error when expecting one on %q", test.in)
    51  			} else {
    52  				assert.Equal(t, test.want, f)
    53  			}
    54  		}
    55  
    56  	}
    57  }
    58  
    59  func TestDumpFlagsType(t *testing.T) {
    60  	f := DumpFlags(0)
    61  	assert.Equal(t, "DumpFlags", f.Type())
    62  }
    63  
    64  func TestDumpFlagsUnmarshallJSON(t *testing.T) {
    65  	for _, test := range []struct {
    66  		in      string
    67  		want    DumpFlags
    68  		wantErr string
    69  	}{
    70  		{`""`, DumpFlags(0), ""},
    71  		{`"bodies"`, DumpBodies, ""},
    72  		{`"bodies,headers,auth"`, DumpBodies | DumpHeaders | DumpAuth, ""},
    73  		{`"bodies,headers,auth"`, DumpBodies | DumpHeaders | DumpAuth, ""},
    74  		{`"headers,bodies,requests,responses,auth,filters"`, DumpHeaders | DumpBodies | DumpRequests | DumpResponses | DumpAuth | DumpFilters, ""},
    75  		{`"headers,bodies,unknown,auth"`, 0, "invalid choice \"unknown\""},
    76  		{`0`, DumpFlags(0), ""},
    77  		{strconv.Itoa(int(DumpBodies)), DumpBodies, ""},
    78  		{strconv.Itoa(int(DumpBodies | DumpHeaders | DumpAuth)), DumpBodies | DumpHeaders | DumpAuth, ""},
    79  	} {
    80  		f := DumpFlags(0xffffffffffffffff)
    81  		initial := f
    82  		err := json.Unmarshal([]byte(test.in), &f)
    83  		if err != nil {
    84  			if test.wantErr == "" {
    85  				t.Errorf("Got an error when not expecting one on %q: %v", test.in, err)
    86  			} else {
    87  				assert.Contains(t, err.Error(), test.wantErr)
    88  			}
    89  			assert.Equal(t, initial, f, test.want)
    90  		} else {
    91  			if test.wantErr != "" {
    92  				t.Errorf("Got no error when expecting one on %q", test.in)
    93  			} else {
    94  				assert.Equal(t, test.want, f)
    95  			}
    96  		}
    97  	}
    98  }