<commit_before>package slack

import (
	"regexp"
	"testing"

	log "github.com/Sirupsen/logrus"
)

var shutdownMessage *Message = NewMessage("shutdown", "general")

func shutdownHandler(_ *Bot, _ map[string]interface{}) (*Message, Status) {
	return shutdownMessage, Shutdown
}

func TestListen(t *testing.T) {
	log.SetLevel(1) // TODO: find the actual name
	var tests = []struct {
		pattern         string
		eventText       string
		expectedMessage *Message
		expectedStatus  Status
	}{
		{"hello", "hello", shutdownMessage, Shutdown},
		{"hello", "goodbye", nil, Continue},
		{"hel*", "he", shutdownMessage, Shutdown},
		{"hel*", "hellllll", shutdownMessage, Shutdown},
		{"hel*", "halp", nil, Continue},
	}

	for _, test := range tests {
		bot := NewBot("token")
		bot.Listen(test.pattern, shutdownHandler)
		handler := bot.Handlers["message"][0]
		event := map[string]interface{}{"text": test.eventText}
		actualMessage, actualStatus := handler(nil, event)
		if test.expectedMessage == nil {
			if actualMessage != nil {
				t.Errorf("Error. Expected nil. Got %v.", actualMessage)
			}
		} else if actualMessage == nil {
			t.Errorf("Error. Expected %v. Got nil.", test.expectedMessage)
		} else {
			compareMessages(test.expectedMessage.toMap(), actualMessage.toMap(), t)
		}
		if test.expectedStatus != actualStatus {
			t.Errorf("Error. Expected %i. Got %i", test.expectedStatus, actualStatus)
		}
	}
}

func TestListenRegexp(t *testing.T) {
	log.SetLevel(1) // TODO: see above<commit_msg>add test case for events with no text<commit_after>package slack

import (
	"regexp"
	"testing"

	log "github.com/Sirupsen/logrus"
)

var shutdownMessage *Message = NewMessage("shutdown", "general")

func shutdownHandler(_ *Bot, _ map[string]interface{}) (*Message, Status) {
	return shutdownMessage, Shutdown
}

func TestListen(t *testing.T) {
	log.SetLevel(1) // TODO: find the actual name
	var tests = []struct {
		pattern         string
		eventText       string
		expectedMessage *Message
		expectedStatus  Status
	}{
		{"hello", "hello", shutdownMessage, Shutdown},
		{"hello", "goodbye", nil, Continue},
		{"hel*", "he", shutdownMessage, Shutdown},
		{"hel*", "hellllll", shutdownMessage, Shutdown},
		{"hel*", "halp", nil, Continue},
	}

	for _, test := range tests {
		bot := NewBot("token")
		bot.Listen(test.pattern, shutdownHandler)
		handler := bot.Handlers["message"][0]
		event := map[string]interface{}{"text": test.eventText}
		actualMessage, actualStatus := handler(nil, event)
		if test.expectedMessage == nil {
			if actualMessage != nil {
				t.Errorf("Error. Expected nil. Got %v.", actualMessage)
			}
		} else if actualMessage == nil {
			t.Errorf("Error. Expected %v. Got nil.", test.expectedMessage)
		} else {
			compareMessages(test.expectedMessage.toMap(), actualMessage.toMap(), t)
		}
		if test.expectedStatus != actualStatus {
			t.Errorf("Error. Expected %i. Got %i", test.expectedStatus, actualStatus)
		}
	}
}

func TestListenNoEventText(t *testing.T) {
	log.SetLevel(1) // TODO: see above

	bot := NewBot("token")
	bot.Listen("hi", shutdownHandler)
	handler := bot.Handlers["message"][0]
	event := map[string]interface{}{}
	actualMessage, actualStatus := handler(nil, event)
	if actualMessage != nil {
		t.Errorf("Error. Expected nil. Got %v.", actualMessage)
	}
	if Continue != actualStatus {
		t.Errorf("Error. Expected %i. Got %i", Continue, actualStatus)
	}
}

func TestListenRegexp(t *testing.T) {
	log.SetLevel(1) // TODO: see above
{'fraction_non_alphanumeric': 0.15402365157196424, 'fraction_numerical': 0.0023074704355350445, 'mean_word_length': 4.319018404907975, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '55500', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1119, 'n_words': 286}