2021-06-19 08:28:20 -04:00
|
|
|
package testassist
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-06-19 08:04:50 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
|
2019-02-10 13:04:11 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
2021-06-19 08:28:20 -04:00
|
|
|
func LoadJSON(creator func() cfgcommon.Buildable) func(string) (proto.Message, error) {
|
2019-02-10 13:04:11 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-19 08:28:20 -04:00
|
|
|
func RunMultiTestCase(t *testing.T, testCases []TestCase) {
|
2019-02-10 13:04:11 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|