tidy up, add docs

This commit is contained in:
Dmitry Fedotov
2025-04-13 22:46:22 +03:00
parent 1465e4cc4c
commit 85452e3dea
10 changed files with 433 additions and 517 deletions

View File

@@ -1,6 +1,8 @@
package conf
import (
"net/url"
"reflect"
"slices"
"testing"
)
@@ -32,6 +34,19 @@ func TestValueMethods(t *testing.T) {
}
},
},
{
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) {
@@ -189,11 +204,11 @@ func TestValueMethods(t *testing.T) {
},
},
{
name: "bool",
name: "bad bool",
f: func(t *testing.T) {
x, err := Value("unknown").Bool()
if err == nil {
t.Fatal("bool doe not fail on incorrect values")
t.Fatal("bool does not fail on incorrect values")
}
if x != false {
@@ -272,7 +287,7 @@ func TestValueMethods(t *testing.T) {
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")
t.Fatal("float slice does not fail on incorrect value")
}
var want []float64
@@ -281,6 +296,70 @@ func TestValueMethods(t *testing.T) {
}
},
},
{
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 {