nurse/validation/jsonval_test.go

184 lines
4.4 KiB
Go
Raw Permalink Normal View History

package validation_test
import (
"testing"
2022-09-22 09:46:36 +00:00
"code.icb4dc0.de/prskr/nurse/validation"
)
type testCase interface {
run(t *testing.T)
name() string
}
2022-09-27 20:19:27 +00:00
type jsonValueComparatorEqualsTestCase[V validation.Value] struct {
testName string
expected V
got any
2022-06-09 20:12:45 +00:00
wantErr bool
}
2022-06-09 20:12:45 +00:00
//nolint:thelper // is not a helper
2022-09-27 20:19:27 +00:00
func (tt jsonValueComparatorEqualsTestCase[V]) run(t *testing.T) {
t.Parallel()
t.Helper()
comparator, err := validation.JSONValueComparatorFor(tt.expected)
if err != nil {
t.Fatalf("validation.JSONValueComparatorFor() err = %v", err)
}
2022-06-09 20:12:45 +00:00
if err := comparator.Equals(tt.got); err != nil {
if !tt.wantErr {
t.Errorf("Equals() = %v, want %v", err, tt.wantErr)
}
}
}
2022-09-27 20:19:27 +00:00
func (tt jsonValueComparatorEqualsTestCase[V]) name() string {
return tt.testName
}
func TestJSONValueComparator_Equals(t *testing.T) {
t.Parallel()
tests := []testCase{
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int]{
testName: "Test int equality",
expected: 42,
got: 42,
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int]{
testName: "Test int equality - wrong value",
expected: 42,
got: 43,
2022-06-09 20:12:45 +00:00
wantErr: true,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int]{
testName: "Test int equality - string value",
expected: 42,
got: "42",
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int]{
testName: "Test int equality - []byte value",
expected: 42,
got: []byte("42"),
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int]{
testName: "Test int equality - float value",
expected: 42,
got: 42.0,
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int8]{
testName: "Test int8 equality",
expected: 42,
got: 42,
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int8]{
testName: "Test int8 equality - wrong value",
expected: 42,
got: 43,
2022-06-09 20:12:45 +00:00
wantErr: true,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int8]{
testName: "Test int8 equality - int16 value",
expected: 42,
got: int16(42),
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[int8]{
testName: "Test int8 equality - uint16 value",
expected: 42,
got: uint16(42),
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[float32]{
testName: "Test float32 equality - float value",
expected: 42.0,
got: 42.0,
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[float32]{
testName: "Test float32 equality - float value",
expected: 42.0,
got: float64(42.0),
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[float64]{
testName: "Test float64 equality - float value",
expected: 42.0,
got: 42.0,
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[float64]{
testName: "Test float64 equality - int value",
expected: 42.0,
got: 42,
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[float64]{
testName: "Test float64 equality - []byte value",
expected: 42.0,
got: []byte("42"),
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[float64]{
testName: "Test float64 equality - float32 value",
expected: 42.0,
got: float32(42.0),
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[float64]{
testName: "Test float64 equality - string value",
expected: 42.0,
got: "42.0",
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[float64]{
testName: "Test float64 equality - string value without dot",
expected: 42.0,
got: "42",
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[string]{
testName: "Test string equality",
expected: "hello",
got: "hello",
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[string]{
testName: "Test string equality - []byte value",
expected: "hello",
got: []byte("hello"),
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[string]{
testName: "Test string equality - int value",
expected: "1337",
got: 1337,
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[string]{
testName: "Test string equality - float value",
expected: "13.37",
got: 13.37,
2022-06-09 20:12:45 +00:00
wantErr: false,
},
2022-09-27 20:19:27 +00:00
jsonValueComparatorEqualsTestCase[string]{
testName: "Test string equality - wrong case",
expected: "hello",
got: "HELLO",
2022-06-09 20:12:45 +00:00
wantErr: true,
},
}
//nolint:paralleltest
for _, tt := range tests {
tt := tt
t.Run(tt.name(), tt.run)
}
}