gomonop/pkg/perfdata/range_test.go
Emmanuel BENOîT 78af496fe9
All checks were successful
Run tests and linters / test (push) Successful in 44s
Run tests and linters / build (push) Successful in 47s
Run tests and linters / lint (push) Successful in 1m20s
refactor: make internals easier to test and add unit tests (#2)
This PR refactors most of the internals to make them easier to test (and also because the names didn't make sense). It adds unit tests for all internal components.

Reviewed-on: #2
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-20 10:01:05 +02:00

67 lines
1.7 KiB
Go

package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRangeMaxInvalid(t *testing.T) {
assert.Panics(
t, func() { RangeMax("") },
"Created PerfDataRange with invalid max value",
)
}
func TestRangeMax(t *testing.T) {
value := "123"
pdr := RangeMax(value)
assert.Equal(t, "0", pdr.start, "Min value should be '0'")
assert.Equal(t, value, pdr.end, "Max value not copied to PerfDataRange")
assert.False(t, pdr.inside, "Inside flag should not be set")
}
func TestRangeMinMaxInvalid(t *testing.T) {
assert.Panics(
t, func() { RangeMinMax("", "123") },
"Created PerfDataRange with invalid min value",
)
assert.Panics(
t, func() { RangeMinMax("123", "") },
"Created PerfDataRange with invalid max value",
)
}
func TestRangeMinMax(t *testing.T) {
min, max := "123", "456"
pdr := RangeMinMax(min, max)
assert.Equal(t, min, pdr.start, "Min value not copied to PerfDataRange")
assert.Equal(t, max, pdr.end, "Max value not copied to PerfDataRange")
assert.False(t, pdr.inside, "Inside flag should not be set")
}
func TestRangeInside(t *testing.T) {
pdr := &Range{}
pdr = pdr.Inside()
assert.True(t, pdr.inside, "Inside flag should be set")
pdr = pdr.Inside()
assert.True(t, pdr.inside, "Inside flag should still be set")
}
func TestRangeString(t *testing.T) {
type Test struct {
pdr Range
out string
}
tests := []Test{
{pdr: Range{start: "Y", end: "X"}, out: "Y:X"},
{pdr: Range{end: "X"}, out: "~:X"},
{pdr: Range{start: "0", end: "X"}, out: ":X"},
{pdr: Range{inside: true, start: "Y", end: "X"}, out: "@Y:X"},
}
for _, test := range tests {
result := test.pdr.String()
assert.Equal(t, test.out, result, "Expected '%s', got '%s'", test.out, result)
}
}