github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/get_test.go (about) 1 package utils 2 3 import "testing" 4 5 func TestGetStruct(t *testing.T) { 6 object := struct { 7 Name string 8 Age int 9 Books []string 10 Extend struct { 11 Location struct { 12 City string 13 } 14 } 15 }{ 16 Name: "lu", 17 Age: 20, 18 Books: []string{"Golang"}, 19 Extend: struct { 20 Location struct { 21 City string 22 } 23 }{ 24 Location: struct { 25 City string 26 }{ 27 City: "Beijing", 28 }, 29 }, 30 } 31 32 if Get(object, []string{"Name"}) != "lu" { 33 t.Fatal("[ERROR]Name != lu") 34 } 35 36 if Get(object, []string{"Age"}) != 20 { 37 t.Fatal("[ERROR]Age != 20") 38 } 39 40 if Get(object, []string{"Books", "0"}) != "Golang" { 41 t.Fatal("[ERROR]books.0 != Golang") 42 } 43 44 t.Log("Extend.Location:", Get(object, []string{"Extend", "Location"})) 45 46 if Get(object, []string{"Extend", "Location", "City"}) != "Beijing" { 47 t.Fatal("[ERROR]Extend.Location.City != Beijing") 48 } 49 } 50 51 func TestGetMap(t *testing.T) { 52 object := map[string]interface{}{ 53 "Name": "lu", 54 "Age": 20, 55 "Extend": map[string]interface{}{ 56 "Location": map[string]interface{}{ 57 "City": "Beijing", 58 }, 59 }, 60 } 61 62 if Get(object, []string{"Name"}) != "lu" { 63 t.Fatal("[ERROR]Name != lu") 64 } 65 66 if Get(object, []string{"Age"}) != 20 { 67 t.Fatal("[ERROR]Age != 20") 68 } 69 70 if Get(object, []string{"Books", "0"}) != nil { 71 t.Fatal("[ERROR]books.0 != nil") 72 } 73 74 t.Log(Get(object, []string{"Extend", "Location"})) 75 76 if Get(object, []string{"Extend", "Location", "City"}) != "Beijing" { 77 t.Fatal("[ERROR]Extend.Location.City != Beijing") 78 } 79 }