diff options
author | Wim <wim@42.be> | 2018-12-12 23:57:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-12 23:57:17 +0100 |
commit | ccd55d2a2889349b8526dd55e29e827c12521f08 (patch) | |
tree | 448a595e577265de9e6d4f74e414ad0f4f2e5778 /gateway/gateway_test.go | |
parent | bfa9a83d316041769704c8b5da96d6f6fb68d732 (diff) | |
download | matterbridge-msglm-ccd55d2a2889349b8526dd55e29e827c12521f08.tar.gz matterbridge-msglm-ccd55d2a2889349b8526dd55e29e827c12521f08.tar.bz2 matterbridge-msglm-ccd55d2a2889349b8526dd55e29e827c12521f08.zip |
Refactor gateway (#648)
* Decrease complexity of handleMessage, handleReceive, handleFiles
* Move handlers to handlers.go
* Split ignoreMessage up in ignoreTextEmpty, ignoreNicks and IgnoreTexts
* Add ignoreEvent
* Add testcase for ignoreTextEmpty, ignoreNicks, ignoreTexts and ignoreEvent
Diffstat (limited to 'gateway/gateway_test.go')
-rw-r--r-- | gateway/gateway_test.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index 798b30c8..2e6d828c 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -387,3 +387,116 @@ func TestGetDestChannelAdvanced(t *testing.T) { } assert.Equal(t, map[string]int{"bridge3": 4, "bridge": 9, "announcements": 3, "bridge2": 4}, hits) } + +func TestIgnoreTextEmpty(t *testing.T) { + extraFile := make(map[string][]interface{}) + extraAttach := make(map[string][]interface{}) + extraFailure := make(map[string][]interface{}) + extraFile["file"] = append(extraFile["file"], config.FileInfo{}) + extraAttach["attachments"] = append(extraAttach["attachments"], []string{}) + extraFailure[config.EventFileFailureSize] = append(extraFailure[config.EventFileFailureSize], config.FileInfo{}) + + msgTests := map[string]struct { + input *config.Message + output bool + }{ + "usertyping": { + input: &config.Message{Event: config.EventUserTyping}, + output: false, + }, + "file attach": { + input: &config.Message{Extra: extraFile}, + output: false, + }, + "attachments": { + input: &config.Message{Extra: extraAttach}, + output: false, + }, + config.EventFileFailureSize: { + input: &config.Message{Extra: extraFailure}, + output: false, + }, + "nil extra": { + input: &config.Message{Extra: nil}, + output: true, + }, + "empty": { + input: &config.Message{}, + output: true, + }, + } + gw := &Gateway{} + for testname, testcase := range msgTests { + output := gw.ignoreTextEmpty(testcase.input) + assert.Equalf(t, testcase.output, output, "case '%s' failed", testname) + } + +} + +func TestIgnoreTexts(t *testing.T) { + msgTests := map[string]struct { + input *config.Message + re []string + output bool + }{ + "no regex": { + input: &config.Message{Text: "a text message"}, + re: []string{}, + output: false, + }, + "simple regex": { + input: &config.Message{Text: "a text message"}, + re: []string{"text"}, + output: true, + }, + "multiple regex fail": { + input: &config.Message{Text: "a text message"}, + re: []string{"abc", "123$"}, + output: false, + }, + "multiple regex pass": { + input: &config.Message{Text: "a text message"}, + re: []string{"lala", "sage$"}, + output: true, + }, + } + gw := &Gateway{} + for testname, testcase := range msgTests { + output := gw.ignoreTexts(testcase.input, testcase.re) + assert.Equalf(t, testcase.output, output, "case '%s' failed", testname) + } +} + +func TestIgnoreNicks(t *testing.T) { + msgTests := map[string]struct { + input *config.Message + re []string + output bool + }{ + "no entry": { + input: &config.Message{Username: "user", Text: "a text message"}, + re: []string{}, + output: false, + }, + "one entry": { + input: &config.Message{Username: "user", Text: "a text message"}, + re: []string{"user"}, + output: true, + }, + "multiple entries": { + input: &config.Message{Username: "user", Text: "a text message"}, + re: []string{"abc", "user"}, + output: true, + }, + "multiple entries fail": { + input: &config.Message{Username: "user", Text: "a text message"}, + re: []string{"abc", "def"}, + output: false, + }, + } + gw := &Gateway{} + for testname, testcase := range msgTests { + output := gw.ignoreNicks(testcase.input, testcase.re) + assert.Equalf(t, testcase.output, output, "case '%s' failed", testname) + } +} |