From e5171b60a91e5d212ef93725d46f977d24be007f Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 25 Jul 2022 02:14:34 +0700 Subject: lib/xmlrpc: rewrite the test using test.Data --- lib/xmlrpc/request_test.go | 196 ++++++++++----------------------- lib/xmlrpc/testdata/marshal_test.txt | 13 +++ lib/xmlrpc/testdata/unmarshal_test.txt | 74 +++++++++++++ 3 files changed, 145 insertions(+), 138 deletions(-) create mode 100644 lib/xmlrpc/testdata/marshal_test.txt create mode 100644 lib/xmlrpc/testdata/unmarshal_test.txt diff --git a/lib/xmlrpc/request_test.go b/lib/xmlrpc/request_test.go index 2617a63e..51befb73 100644 --- a/lib/xmlrpc/request_test.go +++ b/lib/xmlrpc/request_test.go @@ -5,7 +5,6 @@ package xmlrpc import ( - "encoding/xml" "testing" "github.com/shuLhan/share/lib/test" @@ -17,22 +16,16 @@ type testStruct struct { } func TestRequest_MarshalText(t *testing.T) { - cases := []struct { + type testCase struct { methodName string params []interface{} - exp string - }{{ + } + + var cases = []testCase{{ methodName: "method.name", params: []interface{}{ "param-string", }, - exp: xml.Header + "method.name" + - "" + - "" + - "param-string" + - "" + - "" + - "", }, { methodName: "test.struct", params: []interface{}{ @@ -41,154 +34,81 @@ func TestRequest_MarshalText(t *testing.T) { Y: true, }, }, - exp: xml.Header + "test.struct" + - "" + - "" + - "X1" + - "" + - "" + - "Ytrue" + - "" + - "" + - "", }, { methodName: "test.array", params: []interface{}{ []string{"a", "b"}, }, - exp: xml.Header + "test.array" + - "" + - "a" + - "b" + - "" + - "", }} - for _, c := range cases { - req, err := NewRequest(c.methodName, c.params) + var ( + c testCase + req Request + tdata *test.Data + got []byte + exp []byte + err error + ) + + tdata, err = test.LoadData("testdata/marshal_test.txt") + if err != nil { + t.Fatal(err) + } + + for _, c = range cases { + req, err = NewRequest(c.methodName, c.params) if err != nil { t.Fatal(err) } - got, err := req.MarshalText() + got, err = req.MarshalText() if err != nil { t.Fatal(err) } - test.Assert(t, "Pack", c.exp, string(got)) + exp = tdata.Output[c.methodName] + test.Assert(t, "Pack", string(exp), string(got)) } } func TestRequest_UnmarshalText(t *testing.T) { - cases := []struct { - desc string - in string - exp *Request - expError string - }{{ - desc: "Multiple param", - in: ` - - method.name - - - - - param-string - - - - - - - 1 - - - - - `, - exp: &Request{ - MethodName: "method.name", - Params: []*Value{{ - Kind: String, - In: "param-string", - }, { - Kind: Integer, - In: int32(1), - }}, - }, - }, { - desc: "Param as struct", - in: ` - - test.struct - - - - - - X - 1 - - - Y - true - - - - - - `, - exp: &Request{ - MethodName: "test.struct", - Params: []*Value{{ - Kind: Struct, - StructMembers: map[string]*Value{ - "X": &Value{ - Kind: Integer, - In: int32(1), - }, - "Y": &Value{ - Kind: Boolean, - In: true, - }, - }, - }}, - }, - }, { - desc: "Param as array", - in: ` - test.array - - a - b - - `, - exp: &Request{ - MethodName: "test.array", - Params: []*Value{{ - Kind: Array, - ArrayValues: []*Value{{ - Kind: String, - In: "a", - }, { - Kind: String, - In: "b", - }}, - }}, - }, - }} + var ( + tdata *test.Data + name string + xmlInput []byte + err error + ) + + tdata, err = test.LoadData("testdata/unmarshal_test.txt") + if err != nil { + t.Fatal(err) + } - for _, c := range cases { - t.Logf(c.desc) + for name, xmlInput = range tdata.Input { + t.Run(name, func(t *testing.T) { + var ( + req *Request + exp string + got string + xmlb []byte + err error + ) - got := &Request{} - err := got.UnmarshalText([]byte(c.in)) - if err != nil { - test.Assert(t, "Unmarshal", c.expError, err.Error()) - continue - } + exp = string(tdata.Output[name]) + + req = &Request{} + err = req.UnmarshalText(xmlInput) + if err != nil { + got = err.Error() + } else { + xmlb, err = req.MarshalText() + if err != nil { + t.Fatal(err) + } + got = string(xmlb) + } - test.Assert(t, "Unmarshal", c.exp, got) + test.Assert(t, name, exp, got) + }) } } diff --git a/lib/xmlrpc/testdata/marshal_test.txt b/lib/xmlrpc/testdata/marshal_test.txt new file mode 100644 index 00000000..9ad3d896 --- /dev/null +++ b/lib/xmlrpc/testdata/marshal_test.txt @@ -0,0 +1,13 @@ +Test marshaling. + +<<< method.name + +method.nameparam-string + +<<< test.struct + +test.structX1Ytrue + +<<< test.array + +test.arrayab diff --git a/lib/xmlrpc/testdata/unmarshal_test.txt b/lib/xmlrpc/testdata/unmarshal_test.txt new file mode 100644 index 00000000..0b4535d2 --- /dev/null +++ b/lib/xmlrpc/testdata/unmarshal_test.txt @@ -0,0 +1,74 @@ +Test unmarshaling. + +>>> Multiple param + + + method.name + + + + + param-string + + + + + + + 1 + + + + + + +<<< Multiple param + +method.nameparam-string1 + +>>> Param as struct + + + test.struct + + + + + + X + 1 + + + Y + true + + + + + + + +<<< Param as struct + +test.structX1Ytrue + +>>> Param as array + +test.array + + + + + + a + b + + + + + + + +<<< Param as array + +test.arrayab -- cgit v1.3