Emmanuel BENOîT
2fa0e37900
Reviewed-on: #1 Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net> Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
108 lines
2.5 KiB
Go
108 lines
2.5 KiB
Go
package perfdata // import nocternity.net/gomonop/pkg/perfdata
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func assert(t *testing.T, check bool, msg string) {
|
|
if !check {
|
|
t.Errorf(msg)
|
|
}
|
|
}
|
|
|
|
func assertPanic(t *testing.T, f func(), msg string) {
|
|
defer func() {
|
|
if r := recover(); r == nil {
|
|
t.Errorf(msg)
|
|
}
|
|
}()
|
|
f()
|
|
}
|
|
|
|
func TestValueCheckValid(t *testing.T) {
|
|
validValues := []string{
|
|
"0", "0.", "0.952", "1", "123", "123.", "123.45", ".1",
|
|
"-0", "-0.", "-0.952", "-1", "-123", "-123.", "-123.45", "-.1",
|
|
}
|
|
|
|
for _, value := range validValues {
|
|
if !valueCheck.MatchString(value) {
|
|
t.Errorf("'%s' is a valid value string", value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValueCheckInvalid(t *testing.T) {
|
|
invalidValues := []string{".", "-.", "a", " ", ""}
|
|
|
|
for _, value := range invalidValues {
|
|
if valueCheck.MatchString(value) {
|
|
t.Errorf("'%s' is an invalid value string", value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPdrMaxInvalid(t *testing.T) {
|
|
assertPanic(
|
|
t, func() { PDRMax("") },
|
|
"Created PerfDataRange with invalid max value",
|
|
)
|
|
}
|
|
|
|
func TestPdrMax(t *testing.T) {
|
|
value := "123"
|
|
pdr := PDRMax(value)
|
|
assert(t, pdr.start == "0", "Min value should be '0'")
|
|
assert(t, pdr.end == value, "Max value not copied to PerfDataRange")
|
|
assert(t, !pdr.inside, "Inside flag should not be set")
|
|
}
|
|
|
|
func TestPdrMinMaxInvalid(t *testing.T) {
|
|
assertPanic(
|
|
t, func() { PDRMinMax("", "123") },
|
|
"Created PerfDataRange with invalid min value",
|
|
)
|
|
assertPanic(
|
|
t, func() { PDRMinMax("123", "") },
|
|
"Created PerfDataRange with invalid max value",
|
|
)
|
|
}
|
|
|
|
func TestPdrMinMax(t *testing.T) {
|
|
min, max := "123", "456"
|
|
pdr := PDRMinMax(min, max)
|
|
assert(t, pdr.start == min, "Min value not copied to PerfDataRange")
|
|
assert(t, pdr.end == max, "Max value not copied to PerfDataRange")
|
|
assert(t, !pdr.inside, "Inside flag should not be set")
|
|
}
|
|
|
|
func TestPdrInside(t *testing.T) {
|
|
pdr := &PDRange{}
|
|
pdr = pdr.Inside()
|
|
assert(t, pdr.inside, "Inside flag should be set")
|
|
pdr = pdr.Inside()
|
|
assert(t, pdr.inside, "Inside flag should still be set")
|
|
}
|
|
|
|
func TestPdrString(t *testing.T) {
|
|
type Test struct {
|
|
pdr PDRange
|
|
out string
|
|
}
|
|
tests := []Test{
|
|
{pdr: PDRange{start: "Y", end: "X"}, out: "Y:X"},
|
|
{pdr: PDRange{end: "X"}, out: "~:X"},
|
|
{pdr: PDRange{start: "0", end: "X"}, out: ":X"},
|
|
{pdr: PDRange{inside: true, start: "Y", end: "X"}, out: "@Y:X"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
result := test.pdr.String()
|
|
assert(
|
|
t,
|
|
result == test.out,
|
|
fmt.Sprintf("Expected '%s', got '%s'", test.out, result),
|
|
)
|
|
}
|
|
}
|