diff --git a/commands/commands.go b/commands/commands.go index cf85c11..992e7d9 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -28,6 +28,8 @@ type Levels struct { Version int Ping int Last int + Rand int + Find int } var ( @@ -37,7 +39,6 @@ var ( // CommandsList list of commandElement type CommandsList struct { List *list.List - Db db.ZbotDatabase } type commandElement struct { @@ -49,8 +50,6 @@ type commandElement struct { // Chain add a command and the required level to use it to the list of command func (cmdList *CommandsList) Chain(cmdDefinition string, cmd interface{}, level int) *CommandsList { - cmd.(zbotCommand).SetDB(cmdList.Db) - newCommand := &commandElement{ command: cmd.(zbotCommand), requiredLevel: level, @@ -80,7 +79,6 @@ func (cmdList *CommandsList) Run(cmd string, msg string, user user.User) string // zbotCommand interface to be implemented by each command type zbotCommand interface { - SetDB(db db.ZbotDatabase) ProcessText(text string, username user.User) (string, error) } diff --git a/commands/commands_test.go b/commands/commands_test.go index acca3ce..3bfd34f 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -102,17 +102,13 @@ var fake3Command = &FakeCommand2{cmd: "fakecommand3"} var cmdList = &CommandsList{ List: list.New(), - Db: &db.MockZbotDatabase{ - Term: "foo", - Meaning: "bar", - }, } func TestChainAndRun(t *testing.T) { cmdList.Chain("fakecommand1", fake1Command, 0) cmdList.Chain("fakecommand2", fake2Command, 0) - cmdList.Chain("fakecommand3", fake2Command, 0) + cmdList.Chain("fakecommand3", fake3Command, 0) assert.Equal(t, 3, cmdList.List.Len(), "Chain add elements in the list") assert.Equal(t, "Fake 2", cmdList.Run("fakecommand2", "!fakecommand2", userTest)) diff --git a/commands/find.go b/commands/find.go index 80635b0..2f33938 100644 --- a/commands/find.go +++ b/commands/find.go @@ -1,6 +1,7 @@ package command import ( + "errors" "fmt" "regexp" "strings" @@ -10,29 +11,24 @@ import ( "github.com/ssalvatori/zbot-telegram-go/user" ) +//FindCommand defintion type FindCommand struct { - Next HandlerCommand - Db db.ZbotDatabase - Levels Levels + Db db.ZbotDatabase } -func (handler *FindCommand) ProcessText(text string, user user.User) string { +//ProcessText run command +func (handler *FindCommand) ProcessText(text string, user user.User) (string, error) { commandPattern := regexp.MustCompile(`^!find\s(\S*)`) - result := "" if commandPattern.MatchString(text) { term := commandPattern.FindStringSubmatch(text) results, err := handler.Db.Find(term[1]) if err != nil { - log.Error(fmt.Errorf("Error learn %v", err)) - return "" - } - result = fmt.Sprintf("%s", strings.Join(getTerms(results), " ")) - } else { - if handler.Next != nil { - result = handler.Next.ProcessText(text, user) + log.Error(err) + return "", err } + return fmt.Sprintf("%s", strings.Join(getTerms(results), " ")), nil } - return result + return "", errors.New("text doesn't match") } diff --git a/commands/find_test.go b/commands/find_test.go index bec147c..91fd2c2 100644 --- a/commands/find_test.go +++ b/commands/find_test.go @@ -16,18 +16,32 @@ func TestFindCommandOK(t *testing.T) { Meaning: "bar", } - assert.Equal(t, "bar", findCommand.ProcessText("!find foo", userTest), "Last Command") + var result string + + result, _ = findCommand.ProcessText("!find foo", userTest) + assert.Equal(t, "bar", result, "Last Command") findCommand.Db = &db.MockZbotDatabase{ Not_found: true, } - assert.Equal(t, "", findCommand.ProcessText("!find lalal", userTest), "Last Command") + result, _ = findCommand.ProcessText("!find lalal", userTest) + assert.Equal(t, "", result, "Last Command") + +} +func TestFindCommandNotMatch(t *testing.T) { + + result, _ := findCommand.ProcessText("!find6", userTest) + assert.Equal(t, "", result, "Empty output doesn't match") + + _, err := findCommand.ProcessText("!find6", userTest) + assert.Equal(t, "text doesn't match", err.Error(), "Error output doesn't match") +} + +func TestFindCommandError(t *testing.T) { findCommand.Db = &db.MockZbotDatabase{ - Error: true, + Rand_def: db.DefinitionItem{Term: "foo", Meaning: "bar"}, + Error: true, } - assert.Equal(t, "", findCommand.ProcessText("!find lala", userTest), "Error DB") - - assert.Equal(t, "", findCommand.ProcessText("!find", userTest), "Last no next command") - findCommand.Next = &FakeCommand{} - assert.Equal(t, "Fake OK", findCommand.ProcessText("?? ", userTest), "Last next command") + _, err := findCommand.ProcessText("!find lala", userTest) + assert.Equal(t, "mock", err.Error(), "Db error") } diff --git a/commands/ping.go b/commands/ping.go index 7f8da2a..cd3b625 100644 --- a/commands/ping.go +++ b/commands/ping.go @@ -10,10 +10,13 @@ import ( // PingCommand command definition type PingCommand struct { + Db db.ZbotDatabase } //SetDb set db connection if the module need it -func (handler *PingCommand) SetDb(db db.ZbotDatabase) {} +func (handler *PingCommand) SetDb(db db.ZbotDatabase) { + handler.Db = db +} // ProcessText run command func (handler *PingCommand) ProcessText(text string, user user.User) (string, error) { diff --git a/commands/version.go b/commands/version.go index 018fff9..30da701 100644 --- a/commands/version.go +++ b/commands/version.go @@ -5,22 +5,16 @@ import ( "fmt" "regexp" - "github.com/ssalvatori/zbot-telegram-go/db" - "github.com/ssalvatori/zbot-telegram-go/user" ) //VersionCommand configuration for version type VersionCommand struct { - //Next HandlerCommand Version string GitHash string BuildTime string } -//SetDb set db connection if the module need it -func (handler *VersionCommand) SetDb(db db.ZbotDatabase) {} - //ProcessText run command func (handler *VersionCommand) ProcessText(text string, user user.User) (string, error) { diff --git a/zbot/zbot.go b/zbot/zbot.go index 78b425b..bddeb08 100644 --- a/zbot/zbot.go +++ b/zbot/zbot.go @@ -53,6 +53,8 @@ var levelsConfig = command.Levels{ Version: 0, Ping: 0, Last: 0, + Rand: 0, + Find: 0, } //Execute run Zbot @@ -146,34 +148,31 @@ func processing(db db.ZbotDatabase, msg tb.Message) string { } /* - // TODO: how to clean this code - //commands := &command.PingCommand{} - //versionCommand := &command.VersionCommand{Version: version, BuildTime: buildTime, GitHash: gitHash} - //statsCommand := &command.StatsCommand{Db: db, Levels: levelsConfig} - randCommand := &command.RandCommand{Db: db, Levels: levelsConfig} - //topCommand := &command.TopCommand{Db: db, Levels: levelsConfig} - lastCommand := &command.LastCommand{Db: db, Levels: levelsConfig} getCommand := &command.GetCommand{Db: db, Levels: levelsConfig} - findCommand := &command.FindCommand{Db: db, Levels: levelsConfig} searchCommand := &command.SearchCommand{Db: db, Levels: levelsConfig} learnCommand := &command.LearnCommand{Db: db, Levels: levelsConfig} levelCommand := &command.LevelCommand{Db: db, Levels: levelsConfig} ignoreCommand := &command.IgnoreCommand{Db: db, Levels: levelsConfig} lockCommand := &command.LockCommand{Db: db, Levels: levelsConfig} appendCommand := &command.AppendCommand{Db: db, Levels: levelsConfig} - whoCommand := &command.WhoCommand{Db: db, Levels: levelsConfig} forgetCommand := &command.ForgetCommand{Db: db, Levels: levelsConfig} */ commandsList := &command.CommandsList{ List: list.New(), - Db: db, } - commandsList.Chain("ping", command.PingCommand{}, levelsConfig.Ping) - commandsList.Chain("version", command.VersionCommand{Version: version, BuildTime: buildTime}, levelsConfig.Version) - commandsList.Chain("top", command.TopCommand{}, levelsConfig.Top) - commandsList.Chain("stats", command.StatsCommand{}, levelsConfig.Stats) - commandsList.Chain("last", command.LastCommand{}, levelsConfig.Last) + commandsList.Chain("ping", &command.PingCommand{Db: db}, levelsConfig.Ping) + commandsList.Chain("version", &command.VersionCommand{ + GitHash: gitHash, + Version: version, + BuildTime: buildTime, + }, levelsConfig.Version) + commandsList.Chain("top", &command.TopCommand{Db: db}, levelsConfig.Top) + commandsList.Chain("stats", &command.StatsCommand{Db: db}, levelsConfig.Stats) + commandsList.Chain("last", &command.LastCommand{Db: db}, levelsConfig.Last) + commandsList.Chain("rand", &command.RandCommand{Db: db}, levelsConfig.Rand) + commandsList.Chain("who", &command.WhoCommand{Db: db}, levelsConfig.Who) + commandsList.Chain("find", &command.FindCommand{Db: db}, levelsConfig.Find) /* TODO: check error handler @@ -185,20 +184,12 @@ func processing(db db.ZbotDatabase, msg tb.Message) string { PathModules: ModulesPath, } - commands.Next = versionCommand - versionCommand.Next = statsCommand - statsCommand.Next = randCommand - randCommand.Next = topCommand - topCommand.Next = lastCommand - lastCommand.Next = getCommand getCommand.Next = findCommand - findCommand.Next = searchCommand searchCommand.Next = learnCommand learnCommand.Next = levelCommand levelCommand.Next = lockCommand lockCommand.Next = appendCommand appendCommand.Next = whoCommand - whoCommand.Next = forgetCommand forgetCommand.Next = ignoreCommand ignoreCommand.Next = externalCommand */