|
| 1 | +package validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "gotest.tools/assert" |
| 5 | + "reflect" |
| 6 | + "regexp" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestNewStringValidator(t *testing.T) { |
| 11 | + |
| 12 | + t.Run("Test String Validator constructor", func(t *testing.T) { |
| 13 | + |
| 14 | + validRegexp, _ := regexp.Compile("^\\S*$") |
| 15 | + expectedResult := &StringValidator{validRegexp} |
| 16 | + actualResult := NewStringValidator() |
| 17 | + assert.Assert(t, reflect.DeepEqual(actualResult, expectedResult)) |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +func TestStringValidator_Evaluate(t *testing.T) { |
| 22 | + type test struct { |
| 23 | + name string |
| 24 | + value interface{} |
| 25 | + result bool |
| 26 | + } |
| 27 | + |
| 28 | + testTable := []test{ |
| 29 | + {name: "empty string", value: "", result: true}, |
| 30 | + {name: "valid value", value: "provided-value", result: true}, |
| 31 | + {name: "string with spaces", value: "provided value", result: false}, |
| 32 | + {name: "string with numbers", value: "site123", result: true}, |
| 33 | + {name: "number", value: 123, result: false}, |
| 34 | + {name: "nil value", value: nil, result: false}, |
| 35 | + } |
| 36 | + |
| 37 | + for _, test := range testTable { |
| 38 | + t.Run(test.name, func(t *testing.T) { |
| 39 | + |
| 40 | + stringValidator := NewStringValidator() |
| 41 | + expectedResult := test.result |
| 42 | + actualResult, _ := stringValidator.Evaluate(test.value) |
| 43 | + assert.Assert(t, reflect.DeepEqual(actualResult, expectedResult)) |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestNewNumberValidator(t *testing.T) { |
| 49 | + |
| 50 | + t.Run("Test Positive Int Validator constructor", func(t *testing.T) { |
| 51 | + |
| 52 | + expectedResult := &NumberValidator{PositiveInt: true} |
| 53 | + actualResult := NewNumberValidator() |
| 54 | + assert.Assert(t, reflect.DeepEqual(actualResult, expectedResult)) |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func TestIntegerValidator_Evaluate(t *testing.T) { |
| 59 | + type test struct { |
| 60 | + name string |
| 61 | + value interface{} |
| 62 | + result bool |
| 63 | + } |
| 64 | + |
| 65 | + testTable := []test{ |
| 66 | + {name: "empty string", value: "", result: false}, |
| 67 | + {name: "value greater than zero", value: 235, result: true}, |
| 68 | + {name: "zero value", value: 0, result: true}, |
| 69 | + {name: "negative number", value: -2, result: false}, |
| 70 | + {name: "not valid characters", value: "abc", result: false}, |
| 71 | + {name: "nil value", value: nil, result: false}, |
| 72 | + } |
| 73 | + |
| 74 | + for _, test := range testTable { |
| 75 | + t.Run(test.name, func(t *testing.T) { |
| 76 | + |
| 77 | + numberValidator := NewNumberValidator() |
| 78 | + expectedResult := test.result |
| 79 | + actualResult, _ := numberValidator.Evaluate(test.value) |
| 80 | + assert.Assert(t, reflect.DeepEqual(actualResult, expectedResult)) |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments