github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/config_list_test.go (about)

     1  package fs
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func must(err error) {
    11  	if err != nil {
    12  		panic(err)
    13  	}
    14  }
    15  
    16  func ExampleSpaceSepList() {
    17  	for _, s := range []string{
    18  		`remotea:test/dir remoteb:`,
    19  		`"remotea:test/space dir" remoteb:`,
    20  		`"remotea:test/quote""dir" remoteb:`,
    21  	} {
    22  		var l SpaceSepList
    23  		must(l.Set(s))
    24  		fmt.Printf("%#v\n", l)
    25  	}
    26  	// Output:
    27  	// fs.SpaceSepList{"remotea:test/dir", "remoteb:"}
    28  	// fs.SpaceSepList{"remotea:test/space dir", "remoteb:"}
    29  	// fs.SpaceSepList{"remotea:test/quote\"dir", "remoteb:"}
    30  }
    31  
    32  func ExampleCommaSepList() {
    33  	for _, s := range []string{
    34  		`remotea:test/dir,remoteb:`,
    35  		`"remotea:test/space dir",remoteb:`,
    36  		`"remotea:test/quote""dir",remoteb:`,
    37  	} {
    38  		var l CommaSepList
    39  		must(l.Set(s))
    40  		fmt.Printf("%#v\n", l)
    41  	}
    42  	// Output:
    43  	// fs.CommaSepList{"remotea:test/dir", "remoteb:"}
    44  	// fs.CommaSepList{"remotea:test/space dir", "remoteb:"}
    45  	// fs.CommaSepList{"remotea:test/quote\"dir", "remoteb:"}
    46  }
    47  
    48  func TestSpaceSepListSet(t *testing.T) {
    49  	type tc struct {
    50  		in  string
    51  		out SpaceSepList
    52  		err string
    53  	}
    54  	tests := []tc{
    55  		{``, nil, ""},
    56  		{`\`, SpaceSepList{`\`}, ""},
    57  		{`\\`, SpaceSepList{`\\`}, ""},
    58  		{`potato`, SpaceSepList{`potato`}, ""},
    59  		{`po\tato`, SpaceSepList{`po\tato`}, ""},
    60  		{`potato\`, SpaceSepList{`potato\`}, ""},
    61  		{`'potato`, SpaceSepList{`'potato`}, ""},
    62  		{`pot'ato`, SpaceSepList{`pot'ato`}, ""},
    63  		{`potato'`, SpaceSepList{`potato'`}, ""},
    64  		{`"potato"`, SpaceSepList{`potato`}, ""},
    65  		{`'potato'`, SpaceSepList{`'potato'`}, ""},
    66  		{`potato apple`, SpaceSepList{`potato`, `apple`}, ""},
    67  		{`potato\ apple`, SpaceSepList{`potato\`, `apple`}, ""},
    68  		{`"potato  apple"`, SpaceSepList{`potato  apple`}, ""},
    69  		{`"potato'apple"`, SpaceSepList{`potato'apple`}, ""},
    70  		{`"potato''apple"`, SpaceSepList{`potato''apple`}, ""},
    71  		{`"potato' 'apple"`, SpaceSepList{`potato' 'apple`}, ""},
    72  		{`potato="apple"`, nil, `bare " in non-quoted-field`},
    73  		{`apple "potato`, nil, "extraneous"},
    74  		{`apple pot"ato`, nil, "bare \" in non-quoted-field"},
    75  		{`potato"`, nil, "bare \" in non-quoted-field"},
    76  	}
    77  	for _, tc := range tests {
    78  		var l SpaceSepList
    79  		err := l.Set(tc.in)
    80  		if tc.err == "" {
    81  			require.NoErrorf(t, err, "input: %q", tc.in)
    82  		} else {
    83  			require.Containsf(t, err.Error(), tc.err, "input: %q", tc.in)
    84  		}
    85  		require.Equalf(t, tc.out, l, "input: %q", tc.in)
    86  	}
    87  }