package values

import (
	"errors"
	"fmt"
	"reflect"
	"testing"
)

var val = New()

func Test_value_STRING(t *testing.T) {
	if err := val.SetString("key", "value"); err != nil {
		t.Fatal(err)
	}

	if err := val.SetString("key", "value"); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetString("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != "value" {
		t.Fatal("the value of the keys does not match")
	}

	if err := val.UpString("key", "update"); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetString("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != "update" {
		t.Fatal("the value of the keys does not match")
	}

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_BOOL(t *testing.T) {
	if err := val.SetBool("key", true); err != nil {
		t.Fatal(err)
	}

	if err := val.SetBool("key", true); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetBool("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != true {
		t.Fatal("the value of the keys does not match")
	}

	if err := val.UpBool("key", false); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetBool("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != false {
		t.Fatal("the value of the keys does not match")
	}

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_INT(t *testing.T) {
	if err := val.SetInt("key", 1); err != nil {
		t.Fatal(err)
	}

	if err := val.SetInt("key", 1); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetInt("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 1 {
		t.Fatal("the value of the keys does not match")
	}

	if err := val.UpInt("key", 9); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetInt("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 9 {
		t.Fatal("the value of the keys does not match")
	}

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_INT64(t *testing.T) {
	if err := val.SetInt64("key", 1); err != nil {
		t.Fatal(err)
	}

	if err := val.SetInt64("key", 1); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetInt64("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 1 {
		t.Fatal("the value of the keys does not match")
	}

	if err := val.UpInt64("key", 9); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetInt64("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 9 {
		t.Fatal("the value of the keys does not match")
	}

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_FLOAT32(t *testing.T) {
	if err := val.SetFloat32("key", 1.8); err != nil {
		t.Fatal(err)
	}

	if err := val.SetFloat32("key", 1.8); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetFloat32("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 1.8 {
		t.Fatal("the value of the keys does not match")
	}

	if err := val.UpFloat32("key", 9.76); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetFloat32("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 9.76 {
		t.Fatal("the value of the keys does not match")
	}

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_FLOAT64(t *testing.T) {
	if err := val.SetFloat64("key", 1); err != nil {
		t.Fatal(err)
	}

	if err := val.SetFloat64("key", 1); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetFloat64("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 1 {
		t.Fatal("the value of the keys does not match")
	}

	if err := val.UpFloat64("key", 9.76); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetFloat64("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 9.76 {
		t.Fatal("the value of the keys does not match")
	}

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_BYTES(t *testing.T) {
	raw := []byte("qqqqqqqqqqqqqqqq")
	if err := val.SetBytes("key", raw); err != nil {
		t.Fatal(err)
	}

	if err := val.SetBytes("key", raw); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetBytes("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if !reflect.DeepEqual(v, raw) {
		t.Fatal("the value of the keys does not match")
	}

	raw2 := []byte("qqqqqqqqqqqqqqqq")
	if err := val.UpBytes("key", raw2); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetBytes("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if !reflect.DeepEqual(v, raw2) {
		t.Fatal("the value of the keys does not match")
	}

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}
}

func Test_value_INTERFACE(t *testing.T) {
	type MyType struct {
		Q string
		W string
	}

	raw := &MyType{"wq", "tr"}
	if err := val.SetInterface("key", raw); err != nil {
		t.Fatal(err)
	}

	if err := val.SetInterface("key", raw); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}
	v := &MyType{}

	exist, err := val.GetInterface("key", v)
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if !reflect.DeepEqual(v, raw) {
		t.Fatal("the value of the keys does not match")
	}

	raw2 := &MyType{"wq4212", "tsdfsder"}
	if err := val.UpInterface("key", raw2); err != nil {
		t.Fatal(err)
	}

	exist, err = val.GetInterface("key", v)
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if !reflect.DeepEqual(v, raw2) {
		t.Fatal("the value of the keys does not match")
	}

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}
}

func Test_value_ITERATOR(t *testing.T) {

	var (
		valInt     = int(1)
		valInt64   = int64(2)
		valFloat32 = float32(123.23)
		valFloat64 = float64(42342.123)
		valString  = string("dsfsdfsdfsdf")
		valBool    = bool(true)
	)

	err := val.SetInt("int", valInt)
	if err != nil {
		t.Error(err)
	}
	err = val.SetInt64("int64", valInt64)
	if err != nil {
		t.Error(err)
	}
	err = val.SetFloat32("float32", valFloat32)
	if err != nil {
		t.Error(err)
	}
	err = val.SetFloat64("float64", valFloat64)
	if err != nil {
		t.Error(err)
	}
	err = val.SetBool("bool", valBool)
	if err != nil {
		t.Error(err)
	}
	err = val.SetString("string", valString)
	if err != nil {
		t.Error(err)
	}
	marshal, err := val.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	i := 0
ITERATOR:
	if i > 0 {
		return
	}
	err = val.Iterator(func(key string, val *ValueTypes, err error) error {
		switch key {
		case "int":
			if val.Type != Int {
				return errors.New(key + " type error")
			}

			if val.Value != valInt {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valInt).String()))
			}
		case "int64":
			if val.Type != Int64 {
				return errors.New(key + " type error")
			}
			if val.Value != valInt64 {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valInt64).String()))
			}
		case "float32":
			if val.Type != Float32 {
				return errors.New(key + " type error")
			}
			if val.Value != valFloat32 {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valFloat32).String()))
			}
		case "float64":
			if val.Type != Float64 {
				return errors.New(key + " type error")
			}
			if val.Value != valFloat64 {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valFloat64).String()))
			}
		case "bool":
			if val.Type != Bool {
				return errors.New(key + " type error")
			}
			if val.Value != valBool {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valBool).String()))
			}
		case "string":
			if val.Type != String {
				return errors.New(key + " type error")
			}
			if val.Value != valString {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valString).String()))
			}

		default:
			return errors.New(key + "unsupported type")
		}
		return nil
	})
	if err != nil {
		t.Error(err)
	}

	err = val.UnmarshalJSON(marshal)
	if err != nil {
		t.Error(err)
	}
	i++
	goto ITERATOR
}
package values

import (
	"errors"
	"fmt"
	"reflect"
	"testing"
)

var val = New()

func Test_value_STRING(t *testing.T) {
	if err := val.SetString("key", "value"); err != nil {
		t.Fatal(err)
	}

	if err := val.SetString("key", "value"); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetString("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != "value" {
		t.Fatal("the value of the keys does not match")
	}

	////////////////update
	if err := val.UpString("key", "update"); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetString("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != "update" {
		t.Fatal("the value of the keys does not match")
	}
	//////////////endupdate

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_BOOL(t *testing.T) {
	if err := val.SetBool("key", true); err != nil {
		t.Fatal(err)
	}

	if err := val.SetBool("key", true); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetBool("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != true {
		t.Fatal("the value of the keys does not match")
	}

	////////////////update
	if err := val.UpBool("key", false); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetBool("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != false {
		t.Fatal("the value of the keys does not match")
	}
	//////////////endupdate

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_INT(t *testing.T) {
	if err := val.SetInt("key", 1); err != nil {
		t.Fatal(err)
	}

	if err := val.SetInt("key", 1); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetInt("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 1 {
		t.Fatal("the value of the keys does not match")
	}

	////////////////update
	if err := val.UpInt("key", 9); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetInt("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 9 {
		t.Fatal("the value of the keys does not match")
	}
	//////////////endupdate

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_INT64(t *testing.T) {
	if err := val.SetInt64("key", 1); err != nil {
		t.Fatal(err)
	}

	if err := val.SetInt64("key", 1); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetInt64("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 1 {
		t.Fatal("the value of the keys does not match")
	}

	////////////////update
	if err := val.UpInt64("key", 9); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetInt64("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 9 {
		t.Fatal("the value of the keys does not match")
	}
	//////////////endupdate

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_FLOAT32(t *testing.T) {
	if err := val.SetFloat32("key", 1.8); err != nil {
		t.Fatal(err)
	}

	if err := val.SetFloat32("key", 1.8); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetFloat32("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 1.8 {
		t.Fatal("the value of the keys does not match")
	}

	////////////////update
	if err := val.UpFloat32("key", 9.76); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetFloat32("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 9.76 {
		t.Fatal("the value of the keys does not match")
	}
	//////////////endupdate

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_FLOAT64(t *testing.T) {
	if err := val.SetFloat64("key", 1); err != nil {
		t.Fatal(err)
	}

	if err := val.SetFloat64("key", 1); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetFloat64("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 1 {
		t.Fatal("the value of the keys does not match")
	}

	////////////////update
	if err := val.UpFloat64("key", 9.76); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetFloat64("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if v != 9.76 {
		t.Fatal("the value of the keys does not match")
	}
	//////////////endupdate

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}

}

func Test_value_BYTES(t *testing.T) {
	raw := []byte("qqqqqqqqqqqqqqqq")
	if err := val.SetBytes("key", raw); err != nil {
		t.Fatal(err)
	}

	if err := val.SetBytes("key", raw); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}

	v, exist, err := val.GetBytes("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if !reflect.DeepEqual(v, raw) {
		t.Fatal("the value of the keys does not match")
	}

	////////////////update
	raw2 := []byte("qqqqqqqqqqqqqqqq")
	if err := val.UpBytes("key", raw2); err != nil {
		t.Fatal(err)
	}
	v, exist, err = val.GetBytes("key")
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if !reflect.DeepEqual(v, raw2) {
		t.Fatal("the value of the keys does not match")
	}
	//////////////endupdate

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}
}

func Test_value_INTERFACE(t *testing.T) {
	type MyType struct {
		Q string
		W string
	}

	raw := &MyType{"wq", "tr"}
	if err := val.SetInterface("key", raw); err != nil {
		t.Fatal(err)
	}

	if err := val.SetInterface("key", raw); err == nil {
		t.Fatal("the check for the existence of a key did not work")
	}
	v := &MyType{}

	exist, err := val.GetInterface("key", v)
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if !reflect.DeepEqual(v, raw) {
		t.Fatal("the value of the keys does not match")
	}

	////////////////update
	raw2 := &MyType{"wq4212", "tsdfsder"}
	if err := val.UpInterface("key", raw2); err != nil {
		t.Fatal(err)
	}

	exist, err = val.GetInterface("key", v)
	if err != nil {
		t.Fatal(err)
	}
	if !exist {
		t.Fatal("the key that we wrote down earlier was not found")
	}

	if !reflect.DeepEqual(v, raw2) {
		t.Fatal("the value of the keys does not match")
	}
	//////////////endupdate

	isDelete, err := val.Delete("key")
	if err != nil {
		t.Fatal(err)
	}

	if !isDelete {
		t.Fatal("did not find the installed key")
	}
}

func Test_value_ITERATOR(t *testing.T) {

	var (
		valInt     = int(1)
		valInt64   = int64(2)
		valFloat32 = float32(123.23)
		valFloat64 = float64(42342.123)
		valString  = string("dsfsdfsdfsdf")
		valBool    = bool(true)
	)

	err := val.SetInt("int", valInt)
	if err != nil {
		t.Error(err)
	}
	err = val.SetInt64("int64", valInt64)
	if err != nil {
		t.Error(err)
	}
	err = val.SetFloat32("float32", valFloat32)
	if err != nil {
		t.Error(err)
	}
	err = val.SetFloat64("float64", valFloat64)
	if err != nil {
		t.Error(err)
	}
	err = val.SetBool("bool", valBool)
	if err != nil {
		t.Error(err)
	}
	err = val.SetString("string", valString)
	if err != nil {
		t.Error(err)
	}
	marshal, err := val.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	i := 0
ITERATOR:
	if i > 0 {
		return
	}
	err = val.Iterator(func(key string, val *ValueTypes, err error) error {
		switch key {
		case "int":
			if val.Type != Int {
				return errors.New(key + " type error")
			}

			if val.Value != valInt {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valInt).String()))
			}
		case "int64":
			if val.Type != Int64 {
				return errors.New(key + " type error")
			}
			if val.Value != valInt64 {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valInt64).String()))
			}
		case "float32":
			if val.Type != Float32 {
				return errors.New(key + " type error")
			}
			if val.Value != valFloat32 {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valFloat32).String()))
			}
		case "float64":
			if val.Type != Float64 {
				return errors.New(key + " type error")
			}
			if val.Value != valFloat64 {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valFloat64).String()))
			}
		case "bool":
			if val.Type != Bool {
				return errors.New(key + " type error")
			}
			if val.Value != valBool {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valBool).String()))
			}
		case "string":
			if val.Type != String {
				return errors.New(key + " type error")
			}
			if val.Value != valString {
				return errors.New(fmt.Sprintln("falling types", key, reflect.TypeOf(val.Value).String(), reflect.TypeOf(valString).String()))
			}

		default:
			return errors.New(key + "unsupported type")
		}
		return nil
	})
	if err != nil {
		t.Error(err)
	}

	err = val.UnmarshalJSON(marshal)
	if err != nil {
		t.Error(err)
	}
	i++
	goto ITERATOR
}
