/******************************************************************************
 * Copyright (c)  2022 PingCAP, Inc.                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");            *
 * you may not use this file except in compliance with the License.           *
 * You may obtain a copy of the License at                                    *
 *                                                                            *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

/*******************************************************************************
 * @File: convert_test.go
 * @Description:
 * @Author: jiangxunyu@pingcap.com
 * @Version: 1.0.0
 * @Date: 2022/6/10 20:00
*******************************************************************************/

package utils

import (
	"testing"

	. "github.com/onsi/gomega"
)

type src struct {
	Id      int64    `json:"id"`
	Name    string   `json:"name"`
	Hobbies []string `json:"hobbies"`
	Gender  bool     `json:"gender"`
}

type dst src

func TestConvertObj(t *testing.T) {
	t.Run("success", func(t *testing.T) {
		g := NewGomegaWithT(t)
		s := src{
			Id:      1,
			Name:    "zhangsan",
			Hobbies: []string{"movie", "running", "swimming"},
			Gender:  true,
		}
		d := new(dst)
		g.Expect(ConvertObj(s, &d)).Should(Succeed())
		g.Expect(*d).Should(BeEquivalentTo(s))

	})

	t.Run("json unmarshal failed", func(t *testing.T) {
		g := NewGomegaWithT(t)
		g.Expect(ConvertObj("error msg", "")).Should(HaveOccurred())

	})
}

func TestConvertPtr(t *testing.T) {
	t.Run("IntToPtr", func(t *testing.T) {
		g := NewGomegaWithT(t)
		var i int64 = 1
		g.Expect(Int64Ptr(1)).Should(Equal(&i))
	})
	t.Run("Int32Ptr", func(t *testing.T) {
		g := NewGomegaWithT(t)
		var i int32 = 1
		g.Expect(Int32Ptr(1)).Should(Equal(&i))
	})
	t.Run("Uint32Ptr", func(t *testing.T) {
		g := NewGomegaWithT(t)
		var i uint32 = 1
		g.Expect(Uint32Ptr(1)).Should(Equal(&i))
	})
	t.Run("BoolToPtr", func(t *testing.T) {
		g := NewGomegaWithT(t)
		var b bool = true
		g.Expect(BoolPtr(true)).Should(Equal(&b))
	})
	t.Run("StringToPtr", func(t *testing.T) {
		g := NewGomegaWithT(t)
		var s string = "test1"
		g.Expect(StringPtr("test1")).Should(Equal(&s))
	})
}

func TestConvertList(t *testing.T) {
	t.Run("Marshal List", func(t *testing.T) {
		g := NewGomegaWithT(t)
		list := []string{"test1", "test2"}
		g.Expect(MarshalList(list)).Should(Equal(`["test1","test2"]`))
	})
	t.Run("Unmarshal List", func(t *testing.T) {
		g := NewGomegaWithT(t)
		list := []string{"test1", "test2"}
		g.Expect(UnmarshalList(`["test1","test2"]`)).Should(Equal(list))
	})
	t.Run("Unmarshal List - empty", func(t *testing.T) {
		g := NewGomegaWithT(t)
		list := []string{}
		g.Expect(UnmarshalList(``)).Should(Equal(list))
	})
	t.Run("Unmarshal List - failed", func(t *testing.T) {
		g := NewGomegaWithT(t)
		list := []string{}
		g.Expect(UnmarshalList(`s`)).Should(Equal(list))
	})
}
