Skip to content

Commit 2d210bb

Browse files
committed
add AnonymizeIP() tests; move goldmark dependency to the optional template subpackage
1 parent f728581 commit 2d210bb

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

format/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/etkecc/go-kit/format
2+
3+
go 1.22
4+
5+
require github.com/yuin/goldmark v1.7.4

format/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
2+
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/etkecc/go-kit
22

33
go 1.22
4-
5-
require github.com/yuin/goldmark v1.7.4

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
2-
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=

ip.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ func AnonymizeIP(ip string) string {
2626

2727
// IPv6
2828
ipParts := strings.Split(parsedIP.String(), ":")
29-
if len(ipParts) > 0 {
30-
ipParts[len(ipParts)-1] = "0"
31-
return strings.Join(ipParts, ":")
32-
}
33-
return ip // not an ip
29+
ipParts[len(ipParts)-1] = "0"
30+
return strings.Join(ipParts, ":")
3431
}

ip_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package kit
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAnonymizeIP(t *testing.T) {
8+
tests := []struct {
9+
input string
10+
expected string
11+
}{
12+
{"", ""},
13+
{"hello", "hello"},
14+
{"192.168.1.42", "192.168.1.0"},
15+
{"8.8.8.8", "8.8.8.0"},
16+
{"255.255.255.255", "255.255.255.0"},
17+
{"0.0.0.0", "0.0.0.0"},
18+
{"192.168.1", "192.168.1"},
19+
{"::1", "::0"},
20+
{"2001:db8::1234", "2001:db8::0"},
21+
{"fe80::a6db:30ff:fe98:e946", "fe80::a6db:30ff:fe98:0"},
22+
{"2001:db8:abcd:0012:0000:0000:0000:0001", "2001:db8:abcd:12::0"},
23+
{"::", "::0"},
24+
{"2001:db8:85a3::8a2e:370:7334", "2001:db8:85a3::8a2e:370:0"},
25+
{"abcd::", "abcd::0"},
26+
{"1:2:3:4:5:6:7:8", "1:2:3:4:5:6:7:0"},
27+
}
28+
29+
for i, tc := range tests {
30+
got := AnonymizeIP(tc.input)
31+
if got != tc.expected {
32+
t.Errorf("case %d: input=%q got=%q want=%q", i, tc.input, got, tc.expected)
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)