Răsfoiți Sursa

fix size validation error again, add tests

Aneurin Barker Snook 1 an în urmă
părinte
comite
d59fb7b944
2 a modificat fișierele cu 54 adăugiri și 1 ștergeri
  1. 1 1
      size.go
  2. 53 0
      size_test.go

+ 1 - 1
size.go

@@ -24,7 +24,7 @@ func MaxSize[T any](l int) func([]T) error {
 func MinSize[T any](l int) func([]T) error {
 	return func(value []T) error {
 		if len(value) < l {
-			return ErrTooFewChars
+			return ErrTooFewItems
 		}
 		return nil
 	}

+ 53 - 0
size_test.go

@@ -0,0 +1,53 @@
+package validate
+
+import "testing"
+
+func TestMaxSize(t *testing.T) {
+	type TestCase struct {
+		Input []int
+		L     int
+		Err   error
+	}
+
+	testCases := []TestCase{
+		{Input: []int{1, 2, 3, 4}, L: 8},
+		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8}, L: 8},
+		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8, Err: ErrTooManyItems},
+	}
+
+	for n, tc := range testCases {
+		t.Logf("(%d) Testing %q against maximum length of %d", n, tc.Input, tc.L)
+
+		f := MaxSize[int](tc.L)
+		err := f(tc.Input)
+
+		if err != tc.Err {
+			t.Errorf("Expected error %v, got %v", tc.Err, err)
+		}
+	}
+}
+
+func TestMinSize(t *testing.T) {
+	type TestCase struct {
+		Input []int
+		L     int
+		Err   error
+	}
+
+	testCases := []TestCase{
+		{Input: []int{1, 2, 3, 4}, L: 8, Err: ErrTooFewItems},
+		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8}, L: 8},
+		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8},
+	}
+
+	for n, tc := range testCases {
+		t.Logf("(%d) Testing %q against minimum length of %d", n, tc.Input, tc.L)
+
+		f := MinSize[int](tc.L)
+		err := f(tc.Input)
+
+		if err != tc.Err {
+			t.Errorf("Expected error %v, got %v", tc.Err, err)
+		}
+	}
+}