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