From 5c0918f990c44bb236ae9ef392c64d5e966998e7 Mon Sep 17 00:00:00 2001 From: Sebastian Crane Date: Wed, 9 Feb 2022 15:47:24 +0000 Subject: [PATCH] Add function with tests to dispatch IRC commands Signed-off-by: Sebastian Crane --- src/bot.clj | 30 ++++++++++++++++++++++++++++++ test/bot_test.clj | 14 ++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/bot.clj b/src/bot.clj index 8be286b..1bf2f35 100644 --- a/src/bot.clj +++ b/src/bot.clj @@ -40,3 +40,33 @@ " from the list of players for " game "!")) + +(defn split-message [message] + (let [message-parts (str/split message #"\s") + command (if-let [x (first message-parts)] (str/lower-case x) "") + game (second message-parts) + game-keyword (when game (keyword (str/lower-case game)))] + {:command command + :game game + :game-keyword game-keyword})) + +(defn dispatch-command [state sender message] + (let [{command :command + game :game + game-keyword :game-keyword} (split-message message)] + (when game + (condp = command + "!match" + (match-string @state game nil) + + "!add" + (do (swap! state game/add-player-of-game game-keyword sender) + (add-player-string nil game sender)) + + "!list" + (list-players-string @state game nil) + + "!remove" + (do (swap! state game/remove-player-of-game game-keyword sender) + (remove-player-string nil game sender)) + nil)))) diff --git a/test/bot_test.clj b/test/bot_test.clj index cf99ce5..1260a36 100644 --- a/test/bot_test.clj +++ b/test/bot_test.clj @@ -24,3 +24,17 @@ (deftest remove-player-string-test (is (= "Removed abc from the list of players for hypothetical-shooter!" (remove-player-string nil "hypothetical-shooter" "abc")))) + +(deftest split-message-test + (is (= {:command "!match" :game "Quasi-Rts" :game-keyword :quasi-rts} + (split-message "!match Quasi-Rts ")))) + +(deftest dispatch-command-test + (let [state-atom (atom test-state)] + (is (and + (= "Added 123 to the list of players for Imaginary-RPG!" + (dispatch-command state-atom "123" "!add Imaginary-RPG")) + (= {:games {:hypothetical-shooter #{"abc" "xyz" "123"} + :quasi-rts #{"abc" "123"} + :imaginary-rpg #{"xyz" "abc" "123"}}} + @state-atom)))))