/*
 * Copyright 2022 CloudWeGo Authors
 *
 * 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.
 */

package codes

import (
	"strconv"
	"testing"

	"github.com/cloudwego/kitex/internal/test"
)

func TestUnmarshalJSON(t *testing.T) {
	code := new(Code)

	// input is "null"
	err := code.UnmarshalJSON([]byte("null"))
	test.Assert(t, err == nil, err)

	// input is a valid number
	err = code.UnmarshalJSON([]byte("1"))
	test.Assert(t, err == nil, err)

	// input is an invalid number
	err = code.UnmarshalJSON([]byte("200"))
	test.Assert(t, err != nil, err)

	// input is a valid string
	err = code.UnmarshalJSON([]byte("\"DATA_LOSS\""))
	test.Assert(t, err == nil, err)

	// input is an invalid string
	err = code.UnmarshalJSON([]byte("WRONG_INPUT"))
	test.Assert(t, err != nil, err)

	// code is null
	code = nil
	err = code.UnmarshalJSON([]byte("\"DATA_LOSS\""))
	test.Assert(t, err != nil, err)
}

func TestCodeString(t *testing.T) {
	c := OK
	test.Assert(t, c.String() == "OK")
	c = _maxCode
	test.Assert(t, c.String() == "Code("+strconv.FormatInt(int64(c), 10)+")")
}
