-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
227 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ jobs: | |
go install mvdan.cc/[email protected] | ||
go install github.com/securego/gosec/v2/cmd/[email protected] | ||
go install github.com/mgechev/[email protected] | ||
go install github.com/boumenot/[email protected] | ||
- name: Build | ||
run: make build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/dist/ | ||
coverage.out | ||
coverage.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/danroc/geoblock/pkg/utils" | ||
) | ||
|
||
func TestAny(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
values []int | ||
f func(int) bool | ||
want bool | ||
}{ | ||
{"empty slice", []int{}, func(v int) bool { return v > 0 }, false}, | ||
{"no match", []int{1, 2, 3}, func(v int) bool { return v > 3 }, false}, | ||
{"one match", []int{1, 2, 3}, func(v int) bool { return v == 2 }, true}, | ||
{"all match", []int{1, 2, 3}, func(v int) bool { return v > 0 }, true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := utils.Any(tt.values, tt.f); got != tt.want { | ||
t.Errorf("Any() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAll(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
values []int | ||
f func(int) bool | ||
want bool | ||
}{ | ||
{"empty slice", []int{}, func(v int) bool { return v > 0 }, true}, | ||
{"no match", []int{1, 2, 3}, func(v int) bool { return v > 3 }, false}, | ||
{"one match", []int{1, 2, 3}, func(v int) bool { return v == 2 }, false}, | ||
{"all match", []int{1, 2, 3}, func(v int) bool { return v > 0 }, true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := utils.All(tt.values, tt.f); got != tt.want { | ||
t.Errorf("All() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNone(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
values []int | ||
f func(int) bool | ||
want bool | ||
}{ | ||
{"empty slice", []int{}, func(v int) bool { return v > 0 }, true}, | ||
{"no match", []int{1, 2, 3}, func(v int) bool { return v > 3 }, true}, | ||
{"one match", []int{1, 2, 3}, func(v int) bool { return v == 2 }, false}, | ||
{"all match", []int{1, 2, 3}, func(v int) bool { return v > 0 }, false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := utils.None(tt.values, tt.f); got != tt.want { | ||
t.Errorf("None() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package utils_test | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/danroc/geoblock/pkg/utils" | ||
) | ||
|
||
func TestCompareIP(t *testing.T) { | ||
tests := []struct { | ||
a, b string | ||
result int | ||
}{ | ||
{"192.168.1.1", "192.168.1.1", 0}, | ||
{"192.168.1.1", "192.168.1.2", -1}, | ||
{"192.168.1.2", "192.168.1.1", 1}, | ||
{"::1", "::1", 0}, | ||
{"::1", "::2", -1}, | ||
{"::2", "::1", 1}, | ||
} | ||
|
||
for _, test := range tests { | ||
ipA := net.ParseIP(test.a) | ||
ipB := net.ParseIP(test.b) | ||
if ipA == nil || ipB == nil { | ||
t.Fatalf("Invalid IP address in test case: %s, %s", test.a, test.b) | ||
} | ||
|
||
result := utils.CompareIP(ipA, ipB) | ||
if result != test.result { | ||
t.Errorf( | ||
"CompareIP(%s, %s) = %d; want %d", | ||
test.a, | ||
test.b, | ||
result, | ||
test.result, | ||
) | ||
} | ||
} | ||
} | ||
|
||
func TestIsIPv4(t *testing.T) { | ||
tests := []struct { | ||
ip string | ||
result bool | ||
}{ | ||
{"192.168.1.1", true}, | ||
{"255.255.255.255", true}, | ||
{"0.0.0.0", true}, | ||
{"::1", false}, | ||
{"2001:db8::68", false}, | ||
{"", false}, | ||
} | ||
|
||
for _, test := range tests { | ||
ip := net.ParseIP(test.ip) | ||
if ip == nil && test.ip != "" { | ||
t.Fatalf("Invalid IP address in test case: %s", test.ip) | ||
} | ||
|
||
result := utils.IsIPv4(ip) | ||
if result != test.result { | ||
t.Errorf( | ||
"IsIPv4(%s) = %t; want %t", | ||
test.ip, | ||
result, | ||
test.result, | ||
) | ||
} | ||
} | ||
} | ||
|
||
func TestErrInvalidIP(t *testing.T) { | ||
tests := []struct { | ||
address string | ||
message string | ||
}{ | ||
{"256.256.256.256", "invalid IP address: 256.256.256.256"}, | ||
{"invalid-ip", "invalid IP address: invalid-ip"}, | ||
{"", "invalid IP address: "}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := &utils.ErrInvalidIP{Address: test.address} | ||
if err.Error() != test.message { | ||
t.Errorf( | ||
"ErrInvalidIP(%s).Error() = %s; want %s", | ||
test.address, | ||
err.Error(), | ||
test.message, | ||
) | ||
} | ||
} | ||
} |