mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 16:00:25 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			80 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			80 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package utils
 | 
						|
 | 
						|
import "testing"
 | 
						|
 | 
						|
func TestGetStruct(t *testing.T) {
 | 
						|
	object := struct {
 | 
						|
		Name  string
 | 
						|
		Age   int
 | 
						|
		Books []string
 | 
						|
		Extend struct {
 | 
						|
			Location struct {
 | 
						|
				City string
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}{
 | 
						|
		Name:  "lu",
 | 
						|
		Age:   20,
 | 
						|
		Books: []string{"Golang"},
 | 
						|
		Extend: struct {
 | 
						|
			Location struct {
 | 
						|
				City string
 | 
						|
			}
 | 
						|
		}{
 | 
						|
			Location: struct {
 | 
						|
				City string
 | 
						|
			}{
 | 
						|
				City: "Beijing",
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	if Get(object, []string{"Name"}) != "lu" {
 | 
						|
		t.Fatal("[ERROR]Name != lu")
 | 
						|
	}
 | 
						|
 | 
						|
	if Get(object, []string{"Age"}) != 20 {
 | 
						|
		t.Fatal("[ERROR]Age != 20")
 | 
						|
	}
 | 
						|
 | 
						|
	if Get(object, []string{"Books", "0"}) != "Golang" {
 | 
						|
		t.Fatal("[ERROR]books.0 != Golang")
 | 
						|
	}
 | 
						|
 | 
						|
	t.Log("Extend.Location:", Get(object, []string{"Extend", "Location"}))
 | 
						|
 | 
						|
	if Get(object, []string{"Extend", "Location", "City"}) != "Beijing" {
 | 
						|
		t.Fatal("[ERROR]Extend.Location.City != Beijing")
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestGetMap(t *testing.T) {
 | 
						|
	object := map[string]interface{}{
 | 
						|
		"Name": "lu",
 | 
						|
		"Age":  20,
 | 
						|
		"Extend": map[string]interface{}{
 | 
						|
			"Location": map[string]interface{}{
 | 
						|
				"City": "Beijing",
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	if Get(object, []string{"Name"}) != "lu" {
 | 
						|
		t.Fatal("[ERROR]Name != lu")
 | 
						|
	}
 | 
						|
 | 
						|
	if Get(object, []string{"Age"}) != 20 {
 | 
						|
		t.Fatal("[ERROR]Age != 20")
 | 
						|
	}
 | 
						|
 | 
						|
	if Get(object, []string{"Books", "0"}) != nil {
 | 
						|
		t.Fatal("[ERROR]books.0 != nil")
 | 
						|
	}
 | 
						|
 | 
						|
	t.Log(Get(object, []string{"Extend", "Location"}))
 | 
						|
 | 
						|
	if Get(object, []string{"Extend", "Location", "City"}) != "Beijing" {
 | 
						|
		t.Fatal("[ERROR]Extend.Location.City != Beijing")
 | 
						|
	}
 | 
						|
}
 |