github.com/alibabacloud-go/tea@v1.3.10/dara/map_test.go (about)

     1  package dara
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type Person struct {
     9  	Name string
    10  	Age  int
    11  }
    12  
    13  func TestEntries(t *testing.T) {
    14  	// 定义一个包含多种类型的 map
    15  	type Person struct {
    16  		Name string
    17  		Age  int
    18  	}
    19  
    20  	testMap := map[string]interface{}{
    21  		"one":   1,
    22  		"two":   "two",
    23  		"three": &Person{Name: "Alice", Age: 30},
    24  	}
    25  
    26  	entries := Entries(testMap)
    27  
    28  	if len(entries) != 3 {
    29  		t.Errorf("expected %d entries, got %d", 3, len(entries))
    30  	}
    31  
    32  	for _, entry := range entries {
    33  		if !reflect.DeepEqual(entry.Value, testMap[entry.Key]) {
    34  			t.Errorf("expected entry %s to be %v, got %v", entry.Key, testMap[entry.Key], entry.Value)
    35  		}
    36  	}
    37  }
    38  
    39  func TestKeySet(t *testing.T) {
    40  	testMap := map[string]interface{}{
    41  		"one":   1,
    42  		"two":   "two",
    43  		"three": &Person{Name: "Alice", Age: 30},
    44  	}
    45  
    46  	keys := KeySet(testMap)
    47  	str1, str2, str3 := "one", "two", "three"
    48  	expectedKeys := []*string{&str1, &str2, &str3}
    49  
    50  	if len(keys) != len(expectedKeys) {
    51  		t.Errorf("expected %d keys, got %d", len(expectedKeys), len(keys))
    52  	}
    53  
    54  	for _, key := range keys {
    55  		if !ArrContains(expectedKeys, key) {
    56  			t.Errorf("expected key %s to be in the array %v, but not", key, expectedKeys)
    57  		}
    58  	}
    59  }