|
| 1 | +package rvalidation_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"testing" |
| 6 | + |
| 7 | +"github.com/patil-ashutosh/go-regex-utility/regex/rvalidation" |
| 8 | +) |
| 9 | + |
| 10 | +func TestValidateIPv4(t *testing.T) { |
| 11 | +type test struct { |
| 12 | +name string |
| 13 | +hash string |
| 14 | +expectedValidity bool |
| 15 | +} |
| 16 | + |
| 17 | +tests := []test{ |
| 18 | +{name: "validIPv4_1", hash: "0.1.2.3", expectedValidity: true}, |
| 19 | +{name: "validIPv4_2", hash: "172.16.1.2", expectedValidity: true}, |
| 20 | +{name: "validIPv4_3", hash: "172.31.1.2", expectedValidity: true}, |
| 21 | +{name: "validIPv4_4", hash: "192.167.1.2", expectedValidity: true}, |
| 22 | +{name: "validIPv4_5", hash: "255.255.255.255", expectedValidity: true}, |
| 23 | +{name: "InvalidIPv4_1", hash: "1.2.3.", expectedValidity: false}, |
| 24 | +{name: "InvalidIPv4_2", hash: "1.2.256.4", expectedValidity: false}, |
| 25 | +{name: "InvalidIPv4_3", hash: "1.256.3.4", expectedValidity: false}, |
| 26 | +{name: "InvalidIPv4_4", hash: "1.2.3.4.5", expectedValidity: false}, |
| 27 | +{name: "InvalidIPv4_5", hash: "1..3.4", expectedValidity: false}, |
| 28 | +} |
| 29 | + |
| 30 | +for _, tc := range tests { |
| 31 | +tc := tc |
| 32 | +t.Run(tc.name, func(t *testing.T) { |
| 33 | +isValidHash := rvalidation.ValidateIPv4(tc.hash) |
| 34 | +if isValidHash != tc.expectedValidity { |
| 35 | +t.Errorf("ValidateIPv4(%v) Failed: expected %t, actual %t", |
| 36 | +tc.hash, tc.expectedValidity, isValidHash) |
| 37 | +} |
| 38 | +}) |
| 39 | +} |
| 40 | +} |
| 41 | + |
| 42 | +func ExampleValidateIPv4() { |
| 43 | +fmt.Println(rvalidation.ValidateIPv4("192.168.1.2")) |
| 44 | +// Output: true |
| 45 | +} |
0 commit comments