mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-09-01 17:24:15 -04:00
38 lines
907 B
Go
38 lines
907 B
Go
package testassist
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
|
|
"testing"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
)
|
|
|
|
func LoadJSON(creator func() cfgcommon.Buildable) func(string) (proto.Message, error) {
|
|
return func(s string) (proto.Message, error) {
|
|
instance := creator()
|
|
if err := json.Unmarshal([]byte(s), instance); err != nil {
|
|
return nil, err
|
|
}
|
|
return instance.Build()
|
|
}
|
|
}
|
|
|
|
type TestCase struct {
|
|
Input string
|
|
Parser func(string) (proto.Message, error)
|
|
Output proto.Message
|
|
}
|
|
|
|
func RunMultiTestCase(t *testing.T, testCases []TestCase) {
|
|
for _, testCase := range testCases {
|
|
actual, err := testCase.Parser(testCase.Input)
|
|
common.Must(err)
|
|
if !proto.Equal(actual, testCase.Output) {
|
|
t.Fatalf("Failed in test case:\n%s\nActual:\n%v\nExpected:\n%v", testCase.Input, actual, testCase.Output)
|
|
}
|
|
}
|
|
}
|