2020-02-23 11:35:47 +00:00
|
|
|
package flagutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
2020-05-06 19:27:15 +00:00
|
|
|
"reflect"
|
2020-02-23 11:35:47 +00:00
|
|
|
"testing"
|
2020-12-15 10:51:12 +00:00
|
|
|
"time"
|
2020-02-23 11:35:47 +00:00
|
|
|
)
|
|
|
|
|
2020-12-15 10:51:12 +00:00
|
|
|
var (
|
2022-10-01 15:26:05 +00:00
|
|
|
fooFlagString ArrayString
|
2020-12-15 10:51:12 +00:00
|
|
|
fooFlagDuration ArrayDuration
|
|
|
|
fooFlagBool ArrayBool
|
2021-02-01 12:27:05 +00:00
|
|
|
fooFlagInt ArrayInt
|
2022-10-01 15:26:05 +00:00
|
|
|
fooFlagBytes ArrayBytes
|
2020-12-15 10:51:12 +00:00
|
|
|
)
|
2020-02-23 11:35:47 +00:00
|
|
|
|
|
|
|
func init() {
|
2022-10-01 15:26:05 +00:00
|
|
|
os.Args = append(os.Args, "-fooFlagString=foo", "-fooFlagString=bar")
|
|
|
|
os.Args = append(os.Args, "-fooFlagDuration=10s", "-fooFlagDuration=5m")
|
|
|
|
os.Args = append(os.Args, "-fooFlagBool=true", "-fooFlagBool=false,true", "-fooFlagBool")
|
|
|
|
os.Args = append(os.Args, "-fooFlagInt=1", "-fooFlagInt=2,3")
|
|
|
|
os.Args = append(os.Args, "-fooFlagBytes=10MB", "-fooFlagBytes=23,10kib")
|
|
|
|
flag.Var(&fooFlagString, "fooFlagString", "test")
|
2020-12-15 10:51:12 +00:00
|
|
|
flag.Var(&fooFlagDuration, "fooFlagDuration", "test")
|
|
|
|
flag.Var(&fooFlagBool, "fooFlagBool", "test")
|
2021-02-01 12:27:05 +00:00
|
|
|
flag.Var(&fooFlagInt, "fooFlagInt", "test")
|
2022-10-01 15:26:05 +00:00
|
|
|
flag.Var(&fooFlagBytes, "fooFlagBytes", "test")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.Parse()
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayString(t *testing.T) {
|
|
|
|
expected := ArrayString{
|
2021-02-01 12:27:05 +00:00
|
|
|
"foo",
|
|
|
|
"bar",
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-10-01 15:26:05 +00:00
|
|
|
if !reflect.DeepEqual(expected, fooFlagString) {
|
|
|
|
t.Fatalf("unexpected flag values; got\n%q\nwant\n%q", fooFlagString, expected)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 19:27:15 +00:00
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayString_Set(t *testing.T) {
|
2023-08-12 11:17:55 +00:00
|
|
|
f := func(s, expectedResult string) {
|
2020-05-06 19:27:15 +00:00
|
|
|
t.Helper()
|
2022-10-01 15:26:05 +00:00
|
|
|
var a ArrayString
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
result := a.String()
|
|
|
|
if result != expectedResult {
|
|
|
|
t.Fatalf("unexpected values parsed;\ngot\n%s\nwant\n%s", result, expectedResult)
|
2020-05-06 19:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-29 04:10:16 +00:00
|
|
|
// Zero args
|
2023-08-12 11:17:55 +00:00
|
|
|
f("", "")
|
2023-03-29 04:10:16 +00:00
|
|
|
|
|
|
|
// Single arg
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`foo`, `foo`)
|
|
|
|
f(`fo"o`, `"fo\"o"`)
|
|
|
|
f(`fo'o`, `"fo'o"`)
|
|
|
|
f(`fo{o`, `"fo{o"`)
|
|
|
|
f(`fo[o`, `"fo[o"`)
|
|
|
|
f(`fo(o`, `"fo(o"`)
|
2023-03-29 04:10:16 +00:00
|
|
|
|
|
|
|
// Single arg with Prometheus label filters
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`foo{bar="baz",x="y"}`, `"foo{bar=\"baz\",x=\"y\"}"`)
|
|
|
|
f(`foo{bar="ba}z",x="y"}`, `"foo{bar=\"ba}z\",x=\"y\"}"`)
|
|
|
|
f(`foo{bar='baz',x="y"}`, `"foo{bar='baz',x=\"y\"}"`)
|
|
|
|
f(`foo{bar='baz',x='y'}`, `"foo{bar='baz',x='y'}"`)
|
|
|
|
f(`foo{bar='ba}z',x='y'}`, `"foo{bar='ba}z',x='y'}"`)
|
|
|
|
f(`{foo="ba[r",baz='a'}`, `"{foo=\"ba[r\",baz='a'}"`)
|
2023-03-29 04:10:16 +00:00
|
|
|
|
|
|
|
// Single arg with JSON
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`[1,2,3]`, `"[1,2,3]"`)
|
|
|
|
f(`{"foo":"ba,r",baz:x}`, `"{\"foo\":\"ba,r\",baz:x}"`)
|
2023-03-29 04:10:16 +00:00
|
|
|
|
|
|
|
// Single quoted arg
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`"foo"`, `foo`)
|
|
|
|
f(`"fo,'o"`, `"fo,'o"`)
|
|
|
|
f(`"f\\o,\'\"o"`, `"f\\o,\\'\"o"`)
|
|
|
|
f(`"foo{bar='baz',x='y'}"`, `"foo{bar='baz',x='y'}"`)
|
|
|
|
f(`'foo'`, `foo`)
|
|
|
|
f(`'fo,"o'`, `"fo,\"o"`)
|
|
|
|
f(`'f\\o,\'\"o'`, `"f\\o,'\\\"o"`)
|
|
|
|
f(`'foo{bar="baz",x="y"}'`, `"foo{bar=\"baz\",x=\"y\"}"`)
|
2023-03-29 04:10:16 +00:00
|
|
|
|
|
|
|
// Multiple args
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`foo,bar,baz`, `foo,bar,baz`)
|
|
|
|
f(`"foo",'bar',{[(ba'",z"`, `foo,bar,"{[(ba'\",z\""`)
|
|
|
|
f(`foo,b"'ar,"baz,d`, `foo,"b\"'ar,\"baz",d`)
|
|
|
|
f(`{foo="b,ar"},baz{x="y",z="d"}`, `"{foo=\"b,ar\"}","baz{x=\"y\",z=\"d\"}"`)
|
2023-03-29 04:10:16 +00:00
|
|
|
|
|
|
|
// Empty args
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`""`, ``)
|
|
|
|
f(`''`, ``)
|
|
|
|
f(`,`, `,`)
|
|
|
|
f(`,foo,,ba"r,`, `,foo,,"ba\"r",`)
|
2023-03-29 04:10:16 +00:00
|
|
|
|
|
|
|
// Special chars inside double quotes
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`"foo,b\nar"`, `"foo,b\nar"`)
|
|
|
|
f(`"foo\x23bar"`, "foo\x23bar")
|
2020-05-06 19:27:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayString_GetOptionalArg(t *testing.T) {
|
2020-05-06 19:27:15 +00:00
|
|
|
f := func(s string, argIdx int, expectedValue string) {
|
|
|
|
t.Helper()
|
2022-10-01 15:26:05 +00:00
|
|
|
var a ArrayString
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2020-05-06 19:27:15 +00:00
|
|
|
v := a.GetOptionalArg(argIdx)
|
|
|
|
if v != expectedValue {
|
|
|
|
t.Fatalf("unexpected value; got %q; want %q", v, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", 0, "")
|
|
|
|
f("", 1, "")
|
|
|
|
f("foo", 0, "foo")
|
|
|
|
f("foo", 23, "foo")
|
|
|
|
f("foo,bar", 0, "foo")
|
|
|
|
f("foo,bar", 1, "bar")
|
|
|
|
f("foo,bar", 2, "")
|
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayString_String(t *testing.T) {
|
2020-05-06 19:27:15 +00:00
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
2022-10-01 15:26:05 +00:00
|
|
|
var a ArrayString
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2020-05-06 19:27:15 +00:00
|
|
|
result := a.String()
|
|
|
|
if result != s {
|
|
|
|
t.Fatalf("unexpected string;\ngot\n%s\nwant\n%s", result, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("")
|
|
|
|
f("foo")
|
|
|
|
f("foo,bar")
|
|
|
|
f(",")
|
|
|
|
f(",foo,")
|
|
|
|
f(`", foo","b\"ar",`)
|
|
|
|
f(`,"\nfoo\\",bar`)
|
2023-03-29 04:10:16 +00:00
|
|
|
f(`"foo{bar=~\"baz\",a!=\"b\"}","{a='b,{[(c'}"`)
|
2020-05-06 19:27:15 +00:00
|
|
|
}
|
2020-12-15 10:51:12 +00:00
|
|
|
|
|
|
|
func TestArrayDuration(t *testing.T) {
|
2022-10-01 15:26:05 +00:00
|
|
|
expected := ArrayDuration{
|
2023-08-12 11:17:55 +00:00
|
|
|
a: []time.Duration{
|
|
|
|
time.Second * 10,
|
|
|
|
time.Minute * 5,
|
|
|
|
},
|
2020-12-15 10:51:12 +00:00
|
|
|
}
|
2022-10-01 15:26:05 +00:00
|
|
|
if !reflect.DeepEqual(expected, fooFlagDuration) {
|
2023-08-12 11:17:55 +00:00
|
|
|
t.Fatalf("unexpected flag values; got\n%s\nwant\n%s", &fooFlagDuration, &expected)
|
2020-12-15 10:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayDuration_Set(t *testing.T) {
|
2023-08-12 11:17:55 +00:00
|
|
|
f := func(s, expectedResult string) {
|
2020-12-15 10:51:12 +00:00
|
|
|
t.Helper()
|
|
|
|
var a ArrayDuration
|
2024-02-07 18:53:01 +00:00
|
|
|
a.defaultValue = 42 * time.Second
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
result := a.String()
|
|
|
|
if result != expectedResult {
|
|
|
|
t.Fatalf("unexpected values parsed;\ngot\n%q\nwant\n%q", result, expectedResult)
|
2020-12-15 10:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 01:15:04 +00:00
|
|
|
f("", "42s")
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`1m`, `1m0s`)
|
|
|
|
f(`5m,1s,1h`, `5m0s,1s,1h0m0s`)
|
2024-02-07 18:53:01 +00:00
|
|
|
f(`5m,,1h`, `5m0s,42s,1h0m0s`)
|
2020-12-15 10:51:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayDuration_GetOptionalArg(t *testing.T) {
|
|
|
|
f := func(s string, argIdx int, defaultValue, expectedValue time.Duration) {
|
2020-12-15 10:51:12 +00:00
|
|
|
t.Helper()
|
|
|
|
var a ArrayDuration
|
2023-08-12 11:17:55 +00:00
|
|
|
a.defaultValue = defaultValue
|
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
v := a.GetOptionalArg(argIdx)
|
2020-12-15 10:51:12 +00:00
|
|
|
if v != expectedValue {
|
|
|
|
t.Fatalf("unexpected value; got %q; want %q", v, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", 0, time.Second, time.Second)
|
|
|
|
f("", 1, time.Minute, time.Minute)
|
|
|
|
f("10s,1m", 1, time.Minute, time.Minute)
|
2022-10-01 15:26:05 +00:00
|
|
|
f("10s", 3, time.Minute, time.Second*10)
|
2020-12-15 10:51:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayDuration_String(t *testing.T) {
|
2020-12-15 10:51:12 +00:00
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
var a ArrayDuration
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2020-12-15 10:51:12 +00:00
|
|
|
result := a.String()
|
|
|
|
if result != s {
|
|
|
|
t.Fatalf("unexpected string;\ngot\n%s\nwant\n%s", result, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("10s,1m0s")
|
|
|
|
f("5m0s,1s")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArrayBool(t *testing.T) {
|
2022-10-01 15:26:05 +00:00
|
|
|
expected := ArrayBool{
|
2020-12-15 10:51:12 +00:00
|
|
|
true, false, true, true,
|
|
|
|
}
|
2022-10-01 15:26:05 +00:00
|
|
|
if !reflect.DeepEqual(expected, fooFlagBool) {
|
|
|
|
t.Fatalf("unexpected flag values; got\n%v\nwant\n%v", fooFlagBool, expected)
|
2020-12-15 10:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayBool_Set(t *testing.T) {
|
2023-08-12 11:17:55 +00:00
|
|
|
f := func(s, expectedResult string) {
|
2020-12-15 10:51:12 +00:00
|
|
|
t.Helper()
|
|
|
|
var a ArrayBool
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
result := a.String()
|
|
|
|
if result != expectedResult {
|
|
|
|
t.Fatalf("unexpected values parsed;\ngot\n%v\nwant\n%v", result, expectedResult)
|
2020-12-15 10:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 01:15:04 +00:00
|
|
|
f("", "false")
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`true`, `true`)
|
|
|
|
f(`false,True,False`, `false,true,false`)
|
2024-02-07 18:53:01 +00:00
|
|
|
f(`1,,False`, `true,false,false`)
|
2020-12-15 10:51:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayBool_GetOptionalArg(t *testing.T) {
|
2020-12-15 10:51:12 +00:00
|
|
|
f := func(s string, argIdx int, expectedValue bool) {
|
|
|
|
t.Helper()
|
|
|
|
var a ArrayBool
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2020-12-15 10:51:12 +00:00
|
|
|
v := a.GetOptionalArg(argIdx)
|
|
|
|
if v != expectedValue {
|
|
|
|
t.Fatalf("unexpected value; got %v; want %v", v, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", 0, false)
|
|
|
|
f("", 1, false)
|
|
|
|
f("true,true,false", 1, true)
|
|
|
|
f("true", 2, true)
|
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayBool_String(t *testing.T) {
|
2020-12-15 10:51:12 +00:00
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
var a ArrayBool
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2020-12-15 10:51:12 +00:00
|
|
|
result := a.String()
|
|
|
|
if result != s {
|
|
|
|
t.Fatalf("unexpected string;\ngot\n%s\nwant\n%s", result, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("true")
|
|
|
|
f("true,false")
|
|
|
|
f("false,true")
|
|
|
|
}
|
2021-02-01 12:27:05 +00:00
|
|
|
|
|
|
|
func TestArrayInt(t *testing.T) {
|
2023-08-12 11:17:55 +00:00
|
|
|
expected := ArrayInt{
|
|
|
|
a: []int{1, 2, 3},
|
|
|
|
}
|
2022-10-01 15:26:05 +00:00
|
|
|
if !reflect.DeepEqual(expected, fooFlagInt) {
|
|
|
|
t.Fatalf("unexpected flag values; got\n%d\nwant\n%d", fooFlagInt, expected)
|
2021-02-01 12:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayInt_Set(t *testing.T) {
|
2023-12-08 21:27:53 +00:00
|
|
|
f := func(s, expectedResult string, expectedValues []int) {
|
2021-02-01 12:27:05 +00:00
|
|
|
t.Helper()
|
|
|
|
var a ArrayInt
|
2024-02-07 18:53:01 +00:00
|
|
|
a.defaultValue = 42
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
result := a.String()
|
|
|
|
if result != expectedResult {
|
|
|
|
t.Fatalf("unexpected values parsed;\ngot\n%q\nwant\n%q", result, expectedResult)
|
2021-02-01 12:27:05 +00:00
|
|
|
}
|
2023-12-08 21:27:53 +00:00
|
|
|
values := a.Values()
|
|
|
|
if !reflect.DeepEqual(values, expectedValues) {
|
|
|
|
t.Fatalf("unexpected values;\ngot\n%d\nwant\n%d", values, expectedValues)
|
|
|
|
}
|
2021-02-01 12:27:05 +00:00
|
|
|
}
|
2024-02-09 01:15:04 +00:00
|
|
|
f("", "42", []int{42})
|
2023-12-08 21:27:53 +00:00
|
|
|
f(`1`, `1`, []int{1})
|
|
|
|
f(`-2,3,-64`, `-2,3,-64`, []int{-2, 3, -64})
|
2024-02-07 18:53:01 +00:00
|
|
|
f(`,,-64,`, `42,42,-64,42`, []int{42, 42, -64, 42})
|
2021-02-01 12:27:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayInt_GetOptionalArg(t *testing.T) {
|
|
|
|
f := func(s string, argIdx, defaultValue, expectedValue int) {
|
2021-02-01 12:27:05 +00:00
|
|
|
t.Helper()
|
|
|
|
var a ArrayInt
|
2023-08-12 11:17:55 +00:00
|
|
|
a.defaultValue = defaultValue
|
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
v := a.GetOptionalArg(argIdx)
|
2021-02-01 12:27:05 +00:00
|
|
|
if v != expectedValue {
|
|
|
|
t.Fatalf("unexpected value; got %d; want %d", v, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", 0, 123, 123)
|
|
|
|
f("", 1, -34, -34)
|
2022-10-01 15:26:05 +00:00
|
|
|
f("10,1", 1, 234, 1)
|
|
|
|
f("10", 3, -34, 10)
|
2021-02-01 12:27:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 15:26:05 +00:00
|
|
|
func TestArrayInt_String(t *testing.T) {
|
2021-02-01 12:27:05 +00:00
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
var a ArrayInt
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2021-02-01 12:27:05 +00:00
|
|
|
result := a.String()
|
|
|
|
if result != s {
|
|
|
|
t.Fatalf("unexpected string;\ngot\n%s\nwant\n%s", result, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("10,1")
|
|
|
|
f("-5,1,123")
|
|
|
|
}
|
2022-10-01 15:26:05 +00:00
|
|
|
|
|
|
|
func TestArrayBytes(t *testing.T) {
|
2022-12-15 03:26:24 +00:00
|
|
|
expected := []int64{10000000, 23, 10240}
|
2023-08-12 11:17:55 +00:00
|
|
|
result := make([]int64, len(fooFlagBytes.a))
|
|
|
|
for i, b := range fooFlagBytes.a {
|
2022-10-01 15:26:05 +00:00
|
|
|
result[i] = b.N
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, result) {
|
|
|
|
t.Fatalf("unexpected flag values; got\n%d\nwant\n%d", result, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArrayBytes_Set(t *testing.T) {
|
2023-08-12 11:17:55 +00:00
|
|
|
f := func(s, expectedResult string) {
|
2022-10-01 15:26:05 +00:00
|
|
|
t.Helper()
|
|
|
|
var a ArrayBytes
|
2024-02-07 18:53:01 +00:00
|
|
|
a.defaultValue = 42
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2022-10-01 15:26:05 +00:00
|
|
|
}
|
2023-08-12 11:17:55 +00:00
|
|
|
result := a.String()
|
|
|
|
if result != expectedResult {
|
|
|
|
t.Fatalf("unexpected values parsed;\ngot\n%s\nwant\n%s", result, expectedResult)
|
2022-10-01 15:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 01:15:04 +00:00
|
|
|
f("", "42")
|
2023-08-12 11:17:55 +00:00
|
|
|
f(`1`, `1`)
|
|
|
|
f(`-2,3,10kb`, `-2,3,10KB`)
|
2024-02-07 18:53:01 +00:00
|
|
|
f(`,,10kb`, `42,42,10KB`)
|
2022-10-01 15:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestArrayBytes_GetOptionalArg(t *testing.T) {
|
2022-12-15 03:26:24 +00:00
|
|
|
f := func(s string, argIdx int, defaultValue, expectedValue int64) {
|
2022-10-01 15:26:05 +00:00
|
|
|
t.Helper()
|
|
|
|
var a ArrayBytes
|
2023-08-12 11:17:55 +00:00
|
|
|
a.defaultValue = defaultValue
|
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
v := a.GetOptionalArg(argIdx)
|
2022-10-01 15:26:05 +00:00
|
|
|
if v != expectedValue {
|
|
|
|
t.Fatalf("unexpected value; got %d; want %d", v, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", 0, 123, 123)
|
|
|
|
f("", 1, -34, -34)
|
|
|
|
f("10,1", 1, 234, 1)
|
|
|
|
f("10,1", 3, 234, 234)
|
|
|
|
f("10Kb", 3, -34, 10000)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArrayBytes_String(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
var a ArrayBytes
|
2023-08-12 11:17:55 +00:00
|
|
|
if err := a.Set(s); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2022-10-01 15:26:05 +00:00
|
|
|
result := a.String()
|
|
|
|
if result != s {
|
|
|
|
t.Fatalf("unexpected string;\ngot\n%s\nwant\n%s", result, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("10.5KiB,1")
|
|
|
|
f("-5,1,123MB")
|
|
|
|
}
|