// Package op_string ... // // Description : op_string ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2025-10-13 14:36 package op_string import ( "encoding/json" "testing" "git.zhangdeman.cn/zhangdeman/serialize" . "github.com/smartystreets/goconvey/convey" ) func TestToBaseTypeSlice(t *testing.T) { Convey("序列化数据转数组成功", t, func() { testData := `[1,2,3,4,5]` res := ToBaseTypeSlice[uint](testData) So(res.Value, ShouldNotBeNil) So(res.Err, ShouldBeNil) So(len(res.Value), ShouldEqual, 5) }) Convey("序列化数据转数组失败", t, func() { testData := `[1,2,3,4,-5]` res := ToBaseTypeSlice[uint](testData) So(res.Value, ShouldNotBeNil) So(len(res.Value), ShouldEqual, 0) So(res.Err, ShouldNotBeNil) }) Convey("字符串转数组成功", t, func() { testData := `1,2,3,4,5` res := ToBaseTypeSlice[uint](testData, ",") So(res.Value, ShouldNotBeNil) So(res.Err, ShouldBeNil) So(len(res.Value), ShouldEqual, 5) }) Convey("字符串转数组失败", t, func() { testData := `1,2,3,4,-5` res := ToBaseTypeSlice[uint](testData, ",") So(res.Value, ShouldNotBeNil) So(len(res.Value), ShouldEqual, 0) So(res.Err, ShouldNotBeNil) serialize.JSON.ConsoleOutput(res) }) } func TestToMapSlice(t *testing.T) { Convey("序列化数据转map数组成功", t, func() { testData := `[{"name":"baicha", "age": 18}]` res := ToMapSlice[string, any](testData) So(res.Value, ShouldNotBeNil) So(res.Err, ShouldBeNil) So(len(res.Value), ShouldEqual, 1) So(res.Value[0]["name"], ShouldEqual, "baicha") So(res.Value[0]["age"], ShouldEqual, json.Number("18")) }) } func TestToStructSlice(t *testing.T) { Convey("序列化数据转map数组成功", t, func() { testData := `[{"name":"baicha", "age": 18}]` type User struct { Name string `json:"name"` Age int `json:"age"` } res := ToStructSlice[User](testData) So(res.Value, ShouldNotBeNil) So(res.Err, ShouldBeNil) So(len(res.Value), ShouldEqual, 1) So(res.Value[0].Name, ShouldEqual, "baicha") So(res.Value[0].Age, ShouldEqual, 18) }) }