package container

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

func TestServiceMethodHandler_New(t *testing.T) {

	caller := func(_ []reflect.Value) []reflect.Value {
		return nil
	}
	serviceMethodHandler, err := NewServiceMethodHandler(caller)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	if serviceMethodHandler == nil {
		t.Fatalf("创建失败!")
	}
	if !CallerEqual(serviceMethodHandler.caller, caller) {
		t.Fatalf("调用器设置错误!")
	}
}

func TestServiceMethodHandler_New_NullCaller(t *testing.T) {
	_, err := NewServiceMethodHandler(nil)
	if err == nil {
		t.Fatalf("意外的没有错误!")
	}
	if err.Error() != fmt.Sprintf("调用器不能为空!") {
		t.Fatalf("意外错误:%s", err.Error())
	}
}

func TestServiceMethodHandler_Caller(t *testing.T) {
	caller := func([]reflect.Value) []reflect.Value {
		return nil
	}
	methodHandler, err := NewServiceMethodHandler(caller)
	if err != nil {
		t.Fatalf("意外错误:%s", err.Error())
	}
	caller1 := func([]reflect.Value) []reflect.Value {
		return nil
	}
	methodHandler.caller = caller1
	if !CallerEqual(methodHandler.Caller(), caller1) {
		t.Fatalf("获取调用器错误!")
	}
}
