1
mirror of https://github.com/rclone/rclone synced 2024-12-29 22:26:24 +01:00
rclone/fs/tristate_test.go
Nick Craig-Wood 3553cc4a5f fs: fix option types printing incorrectly for backend flags
Before this change backend types were printing incorrectly as the name
of the type, not what was defined by the Type() method.

This was not working due to not calling the Type() method. However
this needed to be defined on a non-pointer type due to the way the
options are handled.
2023-10-03 11:23:58 +01:00

106 lines
2.5 KiB
Go

package fs
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Check it satisfies the interfaces
var (
_ flagger = (*Tristate)(nil)
_ flaggerNP = Tristate{}
)
func TestTristateString(t *testing.T) {
for _, test := range []struct {
in Tristate
want string
}{
{Tristate{}, "unset"},
{Tristate{Valid: false, Value: false}, "unset"},
{Tristate{Valid: false, Value: true}, "unset"},
{Tristate{Valid: true, Value: false}, "false"},
{Tristate{Valid: true, Value: true}, "true"},
} {
got := test.in.String()
assert.Equal(t, test.want, got)
}
}
func TestTristateSet(t *testing.T) {
for _, test := range []struct {
in string
want Tristate
err bool
}{
{"", Tristate{Valid: false, Value: false}, false},
{"nil", Tristate{Valid: false, Value: false}, false},
{"null", Tristate{Valid: false, Value: false}, false},
{"UNSET", Tristate{Valid: false, Value: false}, false},
{"true", Tristate{Valid: true, Value: true}, false},
{"1", Tristate{Valid: true, Value: true}, false},
{"false", Tristate{Valid: true, Value: false}, false},
{"0", Tristate{Valid: true, Value: false}, false},
{"potato", Tristate{Valid: false, Value: false}, true},
} {
var got Tristate
err := got.Set(test.in)
if test.err {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, test.want, got)
}
}
}
func TestTristateScan(t *testing.T) {
var v Tristate
n, err := fmt.Sscan(" true ", &v)
require.NoError(t, err)
assert.Equal(t, 1, n)
assert.Equal(t, Tristate{Valid: true, Value: true}, v)
}
func TestTristateUnmarshalJSON(t *testing.T) {
for _, test := range []struct {
in string
want Tristate
err bool
}{
{`null`, Tristate{}, false},
{`true`, Tristate{Valid: true, Value: true}, false},
{`false`, Tristate{Valid: true, Value: false}, false},
{`potato`, Tristate{}, true},
{``, Tristate{}, true},
} {
var got Tristate
err := json.Unmarshal([]byte(test.in), &got)
if test.err {
require.Error(t, err, test.in)
} else {
require.NoError(t, err, test.in)
}
assert.Equal(t, test.want, got, test.in)
}
}
func TestTristateMarshalJSON(t *testing.T) {
for _, test := range []struct {
in Tristate
want string
}{
{Tristate{}, `null`},
{Tristate{Valid: true, Value: true}, `true`},
{Tristate{Valid: true, Value: false}, `false`},
} {
got, err := json.Marshal(&test.in)
require.NoError(t, err)
assert.Equal(t, test.want, string(got), fmt.Sprintf("%#v", test.in))
}
}