From 11564b8c322d48ee927b54f6571f728750987abc Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 29 Nov 2016 19:22:15 +1300 Subject: [PATCH] test: add MyINFO parsing test cases --- UserInfo.go | 18 ---------- UserInfo_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 UserInfo_test.go diff --git a/UserInfo.go b/UserInfo.go index efdba5c..36231f6 100644 --- a/UserInfo.go +++ b/UserInfo.go @@ -62,24 +62,6 @@ func init() { rx_myinfo = regexp.MustCompile(HEAD + `([^<]*)<(.+?) V:([^,]+),M:(.),H:([0-9]+)/([0-9]+)/([0-9]+),S:([0-9]+)>` + FOOT) rx_myinfo_notag = regexp.MustCompile(HEAD + `([^$]*)` + FOOT) // Fallback for no tag - - /* - sample := "" - // sample = "$ALL Betty description$ $0.01\x01$xyz@xyz.com$53054999578$" - // sample = "$ALL ivysaur80 $P$10A$$0$" - // sample = "$ALL SHINY_IVYSAUR $ $0.005Q$$0$" - // sample = "$ALL mappu desccccc$ $p$$0$" - - u := UserInfo{} - err := u.fromMyINFO(sample) - if err != nil { - fmt.Println(err.Error()) - } else { - fmt.Printf("%+v\n", u) - } - - panic("") - */ } func NewUserInfo(username string) *UserInfo { diff --git a/UserInfo_test.go b/UserInfo_test.go new file mode 100644 index 0000000..72b4c9a --- /dev/null +++ b/UserInfo_test.go @@ -0,0 +1,88 @@ +package libnmdc + +import ( + "testing" +) + +type myInfoTestPair struct { + in string + expect UserInfo +} + +func TestMyINFOParse(t *testing.T) { + + cases := []myInfoTestPair{ + + myInfoTestPair{ + in: "$ALL Bxxxy description$ $0.01\x01$xyz@example.com$53054999578$", + expect: UserInfo{ + Nick: "Bxxxy", + Description: "description", + ClientTag: "ApexDC++", + ClientVersion: "1.4.3", + Email: "xyz@example.com", + ShareSize: 53054999578, + ConnectionMode: CONNECTIONMODE_PASSIVE, + Flag: FLAG_NORMAL, + Slots: 1, + Speed: "0.0", + HubsUnregistered: 9, + HubsRegistered: 0, + HubsOperator: 2, + }, + }, + myInfoTestPair{ + in: "$ALL ixxxxxxx0 $P$10A$$0$", + expect: UserInfo{ + Nick: "ixxxxxxx0", + ClientVersion: "0", // Auto-inserted by the parser for short-format MyINFO strings + ConnectionMode: CONNECTIONMODE_PASSIVE, + Flag: UserFlag(rune('A')), + Speed: "1", + }, + }, + myInfoTestPair{ + in: "$ALL SXXXX_XXXXXXR $ $0.005Q$$0$", + expect: UserInfo{ + Nick: "SXXXX_XXXXXXR", + ClientTag: "ncdc", + ClientVersion: "1.19.1-12-g5561", + ConnectionMode: CONNECTIONMODE_PASSIVE, + Flag: UserFlag(rune('Q')), + Slots: 10, + Speed: "0.00", + HubsUnregistered: 1, + }, + }, + myInfoTestPair{ + in: "$ALL mxxxu desccccc$ $p$$0$", + expect: UserInfo{ + Nick: "mxxxu", + Description: "desccccc", + ClientTag: "HexChat", + ClientVersion: "2.12.1", + ConnectionMode: CONNECTIONMODE_PASSIVE, + Flag: UserFlag(rune('p')), + HubsUnregistered: 1, + Slots: 0, + }, + }, + } + + for _, v := range cases { + + got := UserInfo{} + err := got.fromMyINFO(v.in) + + if err != nil { + t.Errorf("MyINFO parse warning (%s)", err.Error()) + continue + } + + if got != v.expect { + t.Errorf("MyINFO parse failure\nExpected:\n%+v\nGot:\n%+v\n", v.expect, got) + continue + } + } + +}