rclone/fs/cutoffmode_test.go
Nick Craig-Wood ae3963e4b4 fs: Add string alternatives for setting options over the rc
Before this change options were read and set in native format. This
means for example nanoseconds for durations or an integer for
enumerated types, which isn't very convenient for humans.

This change enables these types to be set with a string with the
syntax as used in the command line instead, so `"10s"` rather than
`10000000000` or `"DEBUG"` rather than `8` for log level.
2021-02-07 14:56:41 +00:00

77 lines
1.6 KiB
Go

package fs
import (
"encoding/json"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Check it satisfies the interface
var _ flagger = (*CutoffMode)(nil)
func TestCutoffModeString(t *testing.T) {
for _, test := range []struct {
in CutoffMode
want string
}{
{CutoffModeHard, "HARD"},
{CutoffModeSoft, "SOFT"},
{99, "CutoffMode(99)"},
} {
cm := test.in
got := cm.String()
assert.Equal(t, test.want, got, test.in)
}
}
func TestCutoffModeSet(t *testing.T) {
for _, test := range []struct {
in string
want CutoffMode
err bool
}{
{"hard", CutoffModeHard, false},
{"SOFT", CutoffModeSoft, false},
{"Cautious", CutoffModeCautious, false},
{"Potato", 0, true},
} {
cm := CutoffMode(0)
err := cm.Set(test.in)
if test.err {
require.Error(t, err, test.in)
} else {
require.NoError(t, err, test.in)
}
assert.Equal(t, test.want, cm, test.in)
}
}
func TestCutoffModeUnmarshalJSON(t *testing.T) {
for _, test := range []struct {
in string
want CutoffMode
err bool
}{
{`"hard"`, CutoffModeHard, false},
{`"SOFT"`, CutoffModeSoft, false},
{`"Cautious"`, CutoffModeCautious, false},
{`"Potato"`, 0, true},
{strconv.Itoa(int(CutoffModeHard)), CutoffModeHard, false},
{strconv.Itoa(int(CutoffModeSoft)), CutoffModeSoft, false},
{`99`, 0, true},
{`-99`, 0, true},
} {
var cm CutoffMode
err := json.Unmarshal([]byte(test.in), &cm)
if test.err {
require.Error(t, err, test.in)
} else {
require.NoError(t, err, test.in)
}
assert.Equal(t, test.want, cm, test.in)
}
}