2020-02-28 14:44:15 +00:00
|
|
|
package vfscommon
|
|
|
|
|
|
|
|
import (
|
2020-12-11 17:48:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
"strconv"
|
2020-02-28 14:44:15 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check CacheMode it satisfies the pflag interface
|
|
|
|
var _ pflag.Value = (*CacheMode)(nil)
|
|
|
|
|
2020-12-11 17:48:09 +00:00
|
|
|
// Check CacheMode it satisfies the json.Unmarshaller interface
|
|
|
|
var _ json.Unmarshaler = (*CacheMode)(nil)
|
|
|
|
|
2020-02-28 14:44:15 +00:00
|
|
|
func TestCacheModeString(t *testing.T) {
|
|
|
|
assert.Equal(t, "off", CacheModeOff.String())
|
|
|
|
assert.Equal(t, "full", CacheModeFull.String())
|
2023-09-27 14:31:47 +00:00
|
|
|
assert.Equal(t, "Unknown(17)", CacheMode(17).String())
|
2020-02-28 14:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheModeSet(t *testing.T) {
|
|
|
|
var m CacheMode
|
|
|
|
|
|
|
|
err := m.Set("full")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, CacheModeFull, m)
|
|
|
|
|
|
|
|
err = m.Set("potato")
|
|
|
|
assert.Error(t, err, "Unknown cache mode level")
|
|
|
|
|
|
|
|
err = m.Set("")
|
|
|
|
assert.Error(t, err, "Unknown cache mode level")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheModeType(t *testing.T) {
|
|
|
|
var m CacheMode
|
|
|
|
assert.Equal(t, "CacheMode", m.Type())
|
|
|
|
}
|
2020-12-11 17:48:09 +00:00
|
|
|
|
|
|
|
func TestCacheModeUnmarshalJSON(t *testing.T) {
|
|
|
|
var m CacheMode
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(`"full"`), &m)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, CacheModeFull, m)
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(`"potato"`), &m)
|
|
|
|
assert.Error(t, err, "Unknown cache mode level")
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(`""`), &m)
|
|
|
|
assert.Error(t, err, "Unknown cache mode level")
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(strconv.Itoa(int(CacheModeFull))), &m)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, CacheModeFull, m)
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte("-1"), &m)
|
|
|
|
assert.Error(t, err, "Unknown cache mode level")
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte("99"), &m)
|
|
|
|
assert.Error(t, err, "Unknown cache mode level")
|
|
|
|
}
|