// Package op_string ... // // Description : op_string ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2025-10-13 12:21 package op_string import ( "testing" "encoding/json" . "github.com/smartystreets/goconvey/convey" ) func TestToMap(t *testing.T) { Convey("map[string]any转换成功", t, func() { testData := `{ "name": "baicha", "age": 18 }` res := ToMap[string, any](testData) So(res.Err, ShouldBeNil) So(res.Value, ShouldNotBeNil) So(res.Value["name"], ShouldEqual, "baicha") So(res.Value["age"], ShouldEqual, json.Number("18")) }) Convey("map[string]any转换失败", t, func() { testData := ` "name": "baicha", "age": 18 }` res := ToMap[string, any](testData) So(res.Err, ShouldNotBeNil) So(res.Value, ShouldBeNil) }) } func TestToStruct(t *testing.T) { Convey("struct转换成功", t, func() { testData := `{ "name": "baicha", "age": 18 }` type User struct { Name string `json:"name"` Age int `json:"age"` } var u User res := ToStruct(testData, &u) So(res.Err, ShouldBeNil) So(res.Value, ShouldNotBeNil) So(res.Value.Name, ShouldEqual, "baicha") So(res.Value.Age, ShouldEqual, 18) }) Convey("struct转换失败", t, func() { testData := ` "name": "baicha", "age": 18 }` type User struct { Name string `json:"name"` Age int `json:"age"` } var u User res := ToStruct(testData, &u) So(res.Err, ShouldNotBeNil) So(res.Value, ShouldNotBeNil) So(res.Value.Name, ShouldEqual, "") So(res.Value.Age, ShouldEqual, 0) }) }