package container

import (
	"fmt"
	"testing"
)

func TestLogBrokerBuilder_New(t *testing.T) {

	methodName := "String"
	logBrokerBuilder := NewLogBrokerBuilder("", methodName, "")
	if logBrokerBuilder == nil {
		t.Fatalf("创建失败!")
	}
}

func TestNewLogBrokerBuilder_New_EmptyMethod(t *testing.T) {
	defer func() {
		if err := recover(); err != "方法名不能为空!" {
			t.Fatalf("意外错误:%s", err)
		}
	}()

	methodName := ""
	_ = NewLogBrokerBuilder("", methodName, "")
	t.Fatalf("意外的没有错误!")
}

func TestNewLogBrokerBuilder_Check(t *testing.T) {

	methodName := "String"
	method, ok := testServiceType.MethodByName(methodName)
	if !ok {
		t.Fatalf("意外的没找到方法:%s", methodName)
	}
	builder := NewLogBrokerBuilder("", methodName, "")
	err := builder.Check(method.Type)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestNewLogBrokerBuilder_Check_NullMethodType(t *testing.T) {

	methodName := "String"
	builder := NewLogBrokerBuilder("", methodName, "")
	err := builder.Check(nil)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("方法类型不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestNewLogBrokerBuilder_Build_NullCaller(t *testing.T) {

	methodName := "String"
	builder := NewLogBrokerBuilder("", methodName, "")
	_, err := builder.Build(nil, nil)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("调用器不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}
