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