2022-12-19 21:54:51 +00:00
|
|
|
package maps
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
t.Run("xx", func(t *testing.T) {
|
|
|
|
jsonStr := "{\"data\":{\"yesterday\":{\"sunrise\":\"06:19\"}}}"
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
err := json.Unmarshal([]byte(jsonStr), &data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result := Get(data, "data.yesterday", "sunrise")
|
2023-05-05 02:35:32 +00:00
|
|
|
fmt.Printf("result: %s\n", result)
|
2022-12-19 21:54:51 +00:00
|
|
|
expected := "06:19"
|
|
|
|
assert.Equal(t, expected, result)
|
|
|
|
})
|
|
|
|
t.Run("xx", func(t *testing.T) {
|
|
|
|
jsonStr := "{\"data\":{\"yesterday\": \"hahaha\"}}"
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
err := json.Unmarshal([]byte(jsonStr), &data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result := Get(data, "data.yesterday", "sunrise")
|
2023-05-05 02:35:32 +00:00
|
|
|
fmt.Printf("result: %s\n", result)
|
2022-12-19 21:54:51 +00:00
|
|
|
expected := ""
|
|
|
|
assert.Equal(t, expected, result)
|
|
|
|
})
|
|
|
|
}
|