github.com/imannamdari/v2ray-core/v5@v5.0.5/infra/conf/cfgcommon/testassist/general.go (about)

     1  package testassist
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/golang/protobuf/proto"
     8  
     9  	"github.com/imannamdari/v2ray-core/v5/common"
    10  	"github.com/imannamdari/v2ray-core/v5/infra/conf/cfgcommon"
    11  )
    12  
    13  func LoadJSON(creator func() cfgcommon.Buildable) func(string) (proto.Message, error) {
    14  	return func(s string) (proto.Message, error) {
    15  		instance := creator()
    16  		if err := json.Unmarshal([]byte(s), instance); err != nil {
    17  			return nil, err
    18  		}
    19  		return instance.Build()
    20  	}
    21  }
    22  
    23  type TestCase struct {
    24  	Input  string
    25  	Parser func(string) (proto.Message, error)
    26  	Output proto.Message
    27  }
    28  
    29  func RunMultiTestCase(t *testing.T, testCases []TestCase) {
    30  	for _, testCase := range testCases {
    31  		actual, err := testCase.Parser(testCase.Input)
    32  		common.Must(err)
    33  		if !proto.Equal(actual, testCase.Output) {
    34  			t.Fatalf("Failed in test case:\n%s\nActual:\n%v\nExpected:\n%v", testCase.Input, actual, testCase.Output)
    35  		}
    36  	}
    37  }