@@ -3,13 +3,16 @@ package mapsutil
33import (
44 "crypto/tls"
55 "io"
6+ "net"
67 "net/http"
78 "net/url"
89 "strings"
910 "testing"
1011 "time"
1112
13+ "github.com/miekg/dns"
1214 "github.com/stretchr/testify/require"
15+ "golang.org/x/exp/maps"
1316)
1417
1518func TestMergeMaps (t * testing.T ) {
@@ -82,7 +85,15 @@ func TestHTTPToMap(t *testing.T) {
8285}
8386
8487func TestDNSToMap (t * testing.T ) {
85- // not implemented
88+ msg := dns.Msg {}
89+ msg .Rcode = 1
90+ msg .Question = []dns.Question {{Name : "test" , Qtype : 1 , Qclass : 1 }}
91+ msg .Extra = []dns.RR {& dns.A {Hdr : dns.RR_Header {Name : "test" , Rrtype : 1 , Class : 1 , Ttl : 1 }, A : net .ParseIP ("0.0.0.0" )}}
92+ msg .Answer = []dns.RR {& dns.A {Hdr : dns.RR_Header {Name : "test" , Rrtype : 1 , Class : 1 , Ttl : 1 }, A : net .ParseIP ("0.0.0.0" )}}
93+ msg .Ns = []dns.RR {& dns.A {Hdr : dns.RR_Header {Name : "test" , Rrtype : 1 , Class : 1 , Ttl : 1 }, A : net .ParseIP ("0.0.0.0" )}}
94+ m := DNSToMap (& msg , "" )
95+ require .NotNil (t , m )
96+ require .NotEmpty (t , m )
8697}
8798
8899func TestHTTPRequestToMap (t * testing.T ) {
@@ -148,7 +159,7 @@ func TestGetValues(t *testing.T) {
148159func TestDifference (t * testing.T ) {
149160 t .Run ("Difference(empty)" , func (t * testing.T ) {
150161 got := Difference (map [string ]interface {}{}, []string {}... )
151- require .EqualValues (t , map [string ]interface {}{}, got )
162+ require .ElementsMatch (t , map [string ]interface {}{}, got )
152163 })
153164
154165 t .Run ("Difference(string)" , func (t * testing.T ) {
@@ -227,3 +238,53 @@ func TestClear(t *testing.T) {
227238 require .Empty (t , m2 )
228239 })
229240}
241+
242+ func TestFlatten (t * testing.T ) {
243+ t .Run ("Flatten (flat-map)" , func (t * testing.T ) {
244+ input := map [string ]any {"item" : 0 , "item1" : 1 , "item2" : 2 }
245+ expected := maps .Clone (input )
246+ result := Flatten (input , "." )
247+ require .EqualValues (t , expected , result )
248+ })
249+ t .Run ("Flatten (nested-map)" , func (t * testing.T ) {
250+ input := make (map [string ]any )
251+ testData := []string {"item" , "item1" , "item2" }
252+ expected := GetKeys (map [string ]any {"item.item" : 0 , "item1.item1" : 1 , "item2.item2" : 2 })
253+ for i , v := range testData {
254+ child := make (map [string ]interface {})
255+ child [v ] = i
256+ input [v ] = child
257+ }
258+ got := Flatten (input , "." )
259+ require .ElementsMatch (t , expected , GetKeys (got ))
260+ })
261+ }
262+
263+ func TestWalk (t * testing.T ) {
264+ t .Run ("Walk (flat-map)" , func (t * testing.T ) {
265+ input := make (map [string ]interface {})
266+ expected := []string {"item" , "item1" , "item2" }
267+ for i , v := range expected {
268+ input [v ] = i
269+ }
270+ var got []string
271+ Walk (input , func (k string , v interface {}) {
272+ got = append (got , k )
273+ })
274+ require .Equal (t , len (expected ), len (got ))
275+ })
276+ t .Run ("Walk (nested-map)" , func (t * testing.T ) {
277+ input := make (map [string ]any )
278+ expected := []string {"item" , "item1" , "item2" }
279+ for i , v := range expected {
280+ child := make (map [string ]interface {})
281+ child [v ] = i
282+ input [v ] = child
283+ }
284+ var got []string
285+ Walk (input , func (k string , v interface {}) {
286+ got = append (got , k )
287+ })
288+ require .Equal (t , len (expected ), len (got ))
289+ })
290+ }
0 commit comments