diff options
author | xengineering <me@xengineering.eu> | 2023-05-25 18:57:36 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-25 18:57:36 +0200 |
commit | bfffca845be49cc2a113602cd994a2f430b95f58 (patch) | |
tree | de099f7e474228d75d62fde5105b16aa4d856071 | |
parent | 646ee622bbe21092325a0b9fd29a8e2de258af47 (diff) | |
download | limox-bfffca845be49cc2a113602cd994a2f430b95f58.tar limox-bfffca845be49cc2a113602cd994a2f430b95f58.tar.zst limox-bfffca845be49cc2a113602cd994a2f430b95f58.zip |
Add unit test for xmpp.username() and fix it
The second unit test detected a bug - they are worth the effort!
-rw-r--r-- | xmpp/jid.go | 12 | ||||
-rw-r--r-- | xmpp/jid_test.go | 17 |
2 files changed, 19 insertions, 10 deletions
diff --git a/xmpp/jid.go b/xmpp/jid.go index 672e934..90c1509 100644 --- a/xmpp/jid.go +++ b/xmpp/jid.go @@ -26,18 +26,10 @@ func username(jid string) string { list := []rune(jid) for i, v := range list { - if v == '/' { - list = list[:i] - break - } - } - - for i, v := range list { if v == '@' { - list = list[:i] - break + return string(list[:i]) } } - return string(list) + return "" } diff --git a/xmpp/jid_test.go b/xmpp/jid_test.go index b900b9c..6f8b9fd 100644 --- a/xmpp/jid_test.go +++ b/xmpp/jid_test.go @@ -19,3 +19,20 @@ func TestDomainpart(t *testing.T) { } } } + +func TestUsername(t *testing.T) { + var data = map[string]string{ + "user@example.org/ressource": "user", + "example.org/ressource": "", + "user@example.org": "user", + "df%§df?LÖ@example.org": "df%§df?LÖ", + "example.org": "", + "test.eu": "", + } + + for k, v := range(data) { + if username(k) != v { + t.Fatalf("Username of JID '%s' is not '%s'!", k, v) + } + } +} |