bump test coverage to 87.2%
This commit is contained in:
291
value_test.go
Normal file
291
value_test.go
Normal file
@@ -0,0 +1,291 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"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: "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: "bool",
|
||||
f: func(t *testing.T) {
|
||||
x, err := Value("unknown").Bool()
|
||||
if err == nil {
|
||||
t.Fatal("bool doe 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 doe not fail on incorrect value")
|
||||
}
|
||||
|
||||
var want []float64
|
||||
if !slices.Equal(x, want) {
|
||||
t.Fatalf("want: %v, have: %v", want, x)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range tc {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
c.f(t)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user