package perfdata // import nocternity.net/gomonop/pkg/perfdata import ( "fmt" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) 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 TestRangeMinMaxOpen(t *testing.T) { min, max := "~", "456" pdr := RangeMinMax(min, max) assert.Equal(t, "", pdr.start, "Min value not clear in 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) } } func TestRangeContains(t *testing.T) { type Test struct { pdr Range value float64 result bool } tests := []Test{ {pdr: Range{start: "0", end: "10"}, value: 0, result: false}, {pdr: Range{start: "0", end: "10"}, value: 10, result: false}, {pdr: Range{start: "0", end: "10"}, value: -1, result: true}, {pdr: Range{start: "0", end: "10"}, value: 11, result: true}, {pdr: Range{start: "", end: "10"}, value: -1000, result: false}, {pdr: Range{start: "", end: "10"}, value: 10, result: false}, {pdr: Range{start: "", end: "10"}, value: 11, result: true}, {pdr: Range{start: "10", end: ""}, value: -1000, result: true}, {pdr: Range{start: "10", end: ""}, value: 9, result: true}, {pdr: Range{start: "10", end: ""}, value: 10, result: false}, {pdr: Range{start: "10", end: "20"}, value: 9, result: true}, {pdr: Range{start: "10", end: "20"}, value: 10, result: false}, {pdr: Range{start: "10", end: "20"}, value: 20, result: false}, {pdr: Range{start: "10", end: "20"}, value: 21, result: true}, } // Test cases with the inside flag set and the opposite result n := len(tests) for i := range n { tests = append(tests, Test{ pdr: Range{ start: tests[i].pdr.start, end: tests[i].pdr.end, inside: !tests[i].pdr.inside, }, value: tests[i].value, result: !tests[i].result, }) } for _, test := range tests { t.Run(fmt.Sprintf("%v", test), func(t *testing.T) { result := test.pdr.Contains(test.value) assert.Equal( t, test.result, result, "Expected '%v', got '%v' for value '%f' and range '%s'", test.result, result, test.value, test.pdr.String(), ) }) } } func TestRangeParserOk(t *testing.T) { type Test struct { in string out Range } tests := []Test{ {in: ":0", out: Range{start: "0", end: "0"}}, {in: "0:0", out: Range{start: "0", end: "0"}}, {in: "~:0", out: Range{start: "", end: "0"}}, {in: ":123", out: Range{start: "0", end: "123"}}, {in: "0:123", out: Range{start: "0", end: "123"}}, {in: "~:123", out: Range{start: "", end: "123"}}, {in: "1", out: Range{start: "0", end: "1"}}, {in: "@:0", out: Range{start: "0", end: "0", inside: true}}, {in: "@0:0", out: Range{start: "0", end: "0", inside: true}}, {in: "@~:0", out: Range{start: "", end: "0", inside: true}}, {in: "@:123", out: Range{start: "0", end: "123", inside: true}}, {in: "@0:123", out: Range{start: "0", end: "123", inside: true}}, {in: "@~:123", out: Range{start: "", end: "123", inside: true}}, {in: "@1", out: Range{start: "0", end: "1", inside: true}}, } for _, test := range tests { result, err := ParseRange(test.in) require.NoError(t, err, "Expected no error, got '%v'", err) assert.Equal(t, test.out, *result, "Expected '%v', got '%v'", test.out, *result) } } func TestRangeParserError(t *testing.T) { type Test struct { in string errPos int } tests := []Test{ {in: "", errPos: 0}, {in: ":", errPos: 1}, {in: "x:1", errPos: 0}, {in: ":~", errPos: 1}, {in: "@", errPos: 1}, {in: "@:", errPos: 2}, {in: "@x:1", errPos: 1}, {in: "@:~", errPos: 2}, } for _, test := range tests { result, err := ParseRange(test.in) require.Error(t, err, "Expected error, got '%v'", err) assert.Nil(t, result, "Expected nil result, got '%v'", result) assert.True( t, strings.Contains(err.Error(), fmt.Sprintf("at position %d", test.errPos)), "Expected error to contain '%s', got '%s'", fmt.Sprintf("at position %d", test.errPos), err, ) } }