Skip to content

Commit

Permalink
Merge branch 'main' into move-packages-from-naabu
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Nov 11, 2022
2 parents 06212aa + 6f83277 commit 6d78608
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions maps/mapsutil_test.go
Expand Up @@ -3,13 +3,16 @@ package mapsutil
import (
"crypto/tls"
"io"
"net"
"net/http"
"net/url"
"strings"
"testing"
"time"

"github.com/miekg/dns"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

func TestMergeMaps(t *testing.T) {
Expand Down Expand Up @@ -82,7 +85,15 @@ func TestHTTPToMap(t *testing.T) {
}

func TestDNSToMap(t *testing.T) {
// not implemented
msg := dns.Msg{}
msg.Rcode = 1
msg.Question = []dns.Question{{Name: "test", Qtype: 1, Qclass: 1}}
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")}}
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")}}
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")}}
m := DNSToMap(&msg, "")
require.NotNil(t, m)
require.NotEmpty(t, m)
}

func TestHTTPRequestToMap(t *testing.T) {
Expand Down Expand Up @@ -148,7 +159,7 @@ func TestGetValues(t *testing.T) {
func TestDifference(t *testing.T) {
t.Run("Difference(empty)", func(t *testing.T) {
got := Difference(map[string]interface{}{}, []string{}...)
require.EqualValues(t, map[string]interface{}{}, got)
require.ElementsMatch(t, map[string]interface{}{}, got)
})

t.Run("Difference(string)", func(t *testing.T) {
Expand Down Expand Up @@ -227,3 +238,53 @@ func TestClear(t *testing.T) {
require.Empty(t, m2)
})
}

func TestFlatten(t *testing.T) {
t.Run("Flatten (flat-map)", func(t *testing.T) {
input := map[string]any{"item": 0, "item1": 1, "item2": 2}
expected := maps.Clone(input)
result := Flatten(input, ".")
require.EqualValues(t, expected, result)
})
t.Run("Flatten (nested-map)", func(t *testing.T) {
input := make(map[string]any)
testData := []string{"item", "item1", "item2"}
expected := GetKeys(map[string]any{"item.item": 0, "item1.item1": 1, "item2.item2": 2})
for i, v := range testData {
child := make(map[string]interface{})
child[v] = i
input[v] = child
}
got := Flatten(input, ".")
require.ElementsMatch(t, expected, GetKeys(got))
})
}

func TestWalk(t *testing.T) {
t.Run("Walk (flat-map)", func(t *testing.T) {
input := make(map[string]interface{})
expected := []string{"item", "item1", "item2"}
for i, v := range expected {
input[v] = i
}
var got []string
Walk(input, func(k string, v interface{}) {
got = append(got, k)
})
require.Equal(t, len(expected), len(got))
})
t.Run("Walk (nested-map)", func(t *testing.T) {
input := make(map[string]any)
expected := []string{"item", "item1", "item2"}
for i, v := range expected {
child := make(map[string]interface{})
child[v] = i
input[v] = child
}
var got []string
Walk(input, func(k string, v interface{}) {
got = append(got, k)
})
require.Equal(t, len(expected), len(got))
})
}

0 comments on commit 6d78608

Please sign in to comment.