Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package testassist
 
import (
    "encoding/json"
    "testing"
 
    "github.com/golang/protobuf/proto"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
)
 
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)
        }
    }
}