package conf import ( "net/url" "reflect" "slices" "testing" ) func TestValueMethods(t *testing.T) { tc := []struct { name string f func(t *testing.T) }{ { name: "string", f: func(t *testing.T) { s := Value("test").String() if s != "test" { t.Fatalf("want: %s, have: %s", "test", s) } }, }, { name: "int", f: func(t *testing.T) { x, err := Value("10").Int() if err != nil { t.Fatal(err) } if x != 10 { t.Fatalf("want: %v, have: %v", 10, x) } }, }, { name: "int", f: func(t *testing.T) { x, err := Value("010").Int() if err != nil { t.Fatal(err) } if x != 10 { t.Fatalf("want: %v, have: %v", 10, x) } }, }, { name: "negative int", f: func(t *testing.T) { x, err := Value("-10").Int() if err != nil { t.Fatal(err) } if x != -10 { t.Fatalf("want: %v, have: %v", -10, x) } }, }, { name: "bad int", f: func(t *testing.T) { x, err := Value("1a").Int() if err == nil { t.Fatal("int does not fail on incorrect input") } if x != 0 { t.Fatalf("want: %v, have: %v", 0, x) } }, }, { name: "bad int", f: func(t *testing.T) { x, err := Value("NaN").Int() if err == nil { t.Fatal("int does not fail on incorrect input") } if x != 0 { t.Fatalf("want: %v, have: %v", 0, x) } }, }, { name: "float64", f: func(t *testing.T) { x, err := Value("0.05").Float64() if err != nil { t.Fatal(err) } if x != 0.05 { t.Fatalf("want: %v, have: %v", 0.05, x) } }, }, { name: "negative float64", f: func(t *testing.T) { x, err := Value("-0.05").Float64() if err != nil { t.Fatal(err) } if x != -0.05 { t.Fatalf("want: %v, have: %v", -0.05, x) } }, }, { name: "bad float64", f: func(t *testing.T) { x, err := Value("0,05").Float64() if err == nil { t.Fatal("float does not fail on incorrect input") } if x != 0 { t.Fatalf("want: %v, have: %v", 0, x) } }, }, { name: "bad float64", f: func(t *testing.T) { x, err := Value("0,05").Float64() if err == nil { t.Fatal("float does not fail on incorrect input") } if x != 0 { t.Fatalf("want: %v, have: %v", 0, x) } }, }, { name: "bool", f: func(t *testing.T) { x, err := Value("t").Bool() if err != nil { t.Fatal(err) } if x != true { t.Fatalf("want: %v, have: %v", true, x) } }, }, { name: "bool", f: func(t *testing.T) { x, err := Value("true").Bool() if err != nil { t.Fatal(err) } if x != true { t.Fatalf("want: %v, have: %v", true, x) } }, }, { name: "bool", f: func(t *testing.T) { x, err := Value("T").Bool() if err != nil { t.Fatal(err) } if x != true { t.Fatalf("want: %v, have: %v", true, x) } }, }, { name: "bool", f: func(t *testing.T) { x, err := Value("True").Bool() if err != nil { t.Fatal(err) } if x != true { t.Fatalf("want: %v, have: %v", true, x) } }, }, { name: "bool", f: func(t *testing.T) { x, err := Value("TRUE").Bool() if err != nil { t.Fatal(err) } if x != true { t.Fatalf("want: %v, have: %v", true, x) } }, }, { name: "bad bool", f: func(t *testing.T) { x, err := Value("unknown").Bool() if err == nil { t.Fatal("bool does not fail on incorrect values") } if x != false { t.Fatalf("want: %v, have: %v", false, x) } }, }, { name: "string slice", f: func(t *testing.T) { s := Value("a, b, c").StringSlice() want := []string{"a", "b", "c"} if !slices.Equal(s, want) { t.Fatalf("want: %v, have: %v", want, s) } }, }, { name: "int slice", f: func(t *testing.T) { x, err := Value("1, 2, 3").IntSlice() if err != nil { t.Fatal(err) } want := []int{1, 2, 3} if !slices.Equal(x, want) { t.Fatalf("want: %v, have: %v", want, x) } }, }, { name: "int slice", f: func(t *testing.T) { x, err := Value("1,2, 3").IntSlice() if err != nil { t.Fatal(err) } want := []int{1, 2, 3} if !slices.Equal(x, want) { t.Fatalf("want: %v, have: %v", want, x) } }, }, { name: "bad int slice", f: func(t *testing.T) { x, err := Value("1, a, 3").IntSlice() if err == nil { t.Fatal("int slice does not fail on incorrect value") } var want []int if !slices.Equal(x, want) { t.Fatalf("want: %v, have: %v", want, x) } }, }, { name: "float64 slice", f: func(t *testing.T) { x, err := Value("0.1, 0.2, 0.3").Float64Slice() if err != nil { t.Fatal(err) } want := []float64{0.1, 0.2, 0.3} if !slices.Equal(x, want) { t.Fatalf("want: %v, have: %v", want, x) } }, }, { name: "bad float64 slice", f: func(t *testing.T) { x, err := Value("0.1, a, 0.3").Float64Slice() if err == nil { t.Fatal("float slice does not fail on incorrect value") } var want []float64 if !slices.Equal(x, want) { t.Fatalf("want: %v, have: %v", want, x) } }, }, { name: "map", f: func(t *testing.T) { x, err := Value("a: 1, b : 2, c :3").Map() if err != nil { t.Fatal("map fails on correct values") } want := Map{ "a": Value("1"), "b": Value("2"), "c": Value("3"), } if !reflect.DeepEqual(x, want) { t.Fatalf("want: %v, have: %v", want, x) } }, }, { name: "map invalid", f: func(t *testing.T) { x, err := Value("a:1, b-2, c: 3").Map() if err == nil { t.Fatal("map does not fail on incorrect value") } var want Map if !reflect.DeepEqual(x, want) { t.Fatalf("want: %v, have: %v", want, x) } }, }, { name: "URL", f: func(t *testing.T) { x, err := Value("https://example.com:80").URL() if err != nil { t.Fatal(err) } want := &url.URL{ Scheme: "http", Host: "example.com:80", } if reflect.DeepEqual(want, x) { t.Fatalf("want: %v, have: %v", want, x) } }, }, { name: "bad URL", f: func(t *testing.T) { x, err := Value(":malformed://url/").URL() if err == nil { t.Fatal("url does not fail on incorrect input") } if x != nil { t.Fatalf("want: %v, have: %v", nil, x) } }, }, } for _, c := range tc { t.Run(c.name, func(t *testing.T) { c.f(t) }) } }