Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test cases addition mapsutil #17

Merged
merged 3 commits into from Nov 11, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 62 additions & 1 deletion 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 @@ -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)
Mzack9999 marked this conversation as resolved.
Show resolved Hide resolved
})
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))
})
}