/*
 * Copyright 2021 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 perrors

import (
	"errors"
	"testing"

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

func TestNewProtocolError(t *testing.T) {
	// 1. check extends
	err1 := errors.New("test error")
	err2 := NewProtocolError(err1)
	test.Assert(t, errors.Is(err2, err1))

	err3 := NewProtocolError(err2)
	test.Assert(t, errors.Is(err3, err1))

	// 2. check args
	test.Assert(t, err3.Error() == err1.Error())
	test.Assert(t, err3.(interface{ String() string }).String() == err1.Error())
	test.Assert(t, err3.(ProtocolError).TypeId() == err2.(ProtocolError).TypeId())
	test.Assert(t, !errors.Is(err3, InvalidDataLength))

	// 3. check nil
	err4 := NewProtocolError(nil)
	test.Assert(t, err4 == nil)
}

func TestNewProtocolErrorWithType(t *testing.T) {
	// check fail
	msg := "test error"
	err1 := errors.New(msg)
	test.Assert(t, !IsProtocolError(err1))

	typeID := InvalidData
	err2 := NewProtocolErrorWithType(typeID, msg)
	test.Assert(t, !errors.Is(err2, InvalidDataLength))

	// check success
	test.Assert(t, err2.Error() == msg)
	test.Assert(t, IsProtocolError(err2))
	test.Assert(t, err2.TypeId() == typeID)

	err3 := NewProtocolErrorWithType(typeID, err2.Error())
	test.Assert(t, err3.Error() == msg)
	test.Assert(t, IsProtocolError(err3))
	test.Assert(t, err3.TypeId() == typeID)
}

func TestNewProtocolErrorWithMsg(t *testing.T) {
	// check fail
	msg := "test error"
	err1 := errors.New(msg)
	test.Assert(t, !IsProtocolError(err1))

	err2 := NewProtocolErrorWithMsg(msg)
	test.Assert(t, !errors.Is(err2, InvalidDataLength))

	// check success
	test.Assert(t, err2.Error() == msg)
	test.Assert(t, IsProtocolError(err2))

	err3 := NewProtocolErrorWithMsg(msg)
	test.Assert(t, err3.Error() == msg)
}

func TestNewProtocolErrorWithErrMsg(t *testing.T) {
	// check fail
	msg := "test error"
	err1 := errors.New(msg)
	test.Assert(t, !IsProtocolError(err1))
	err2 := NewProtocolErrorWithErrMsg(err1, msg)
	test.Assert(t, !errors.Is(err2, InvalidDataLength))

	// check success
	test.Assert(t, err2.Error() == msg)
	test.Assert(t, IsProtocolError(err2))
	err3 := NewProtocolErrorWithErrMsg(err2, msg)
	test.Assert(t, err3.Error() == msg)

	// check nil
	err4 := NewProtocolErrorWithErrMsg(nil, msg)
	test.Assert(t, err4 == nil)
	test.Assert(t, !IsProtocolError(err4))
}

func TestIsProtocolError(t *testing.T) {
	// check fail
	err1 := errors.New("test error")
	test.Assert(t, !IsProtocolError(err1))

	// check success
	err2 := NewProtocolError(err1)
	test.Assert(t, IsProtocolError(err2))
	err3 := NewProtocolErrorWithType(InvalidData, "test error")
	test.Assert(t, IsProtocolError(err3))
	err4 := NewProtocolErrorWithMsg("test error")
	test.Assert(t, IsProtocolError(err4))
	err5 := NewProtocolError(err4)
	test.Assert(t, IsProtocolError(err5))

	// check nil
	test.Assert(t, !IsProtocolError(nil))
	err6 := NewProtocolError(nil)
	test.Assert(t, !IsProtocolError(err6))
}
