general_test.go 877 B

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