-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_robot.ex
101 lines (78 loc) · 2.36 KB
/
test_robot.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
defmodule Clover.Test.TestRobot do
@moduledoc """
A `Clover.Robot` implementation for testing
"""
use Clover.Robot
alias Clover.Robot.Brain
def init(_arg, data) do
{:ok, data}
end
def handle_connected(connection_state, data) do
log(:debug, "connected", inspect: connection_state)
{:ok, data}
end
respond ~r/^pid$/, :pid_script
respond ~r/^bad return$/, :bad_return_script
respond ~r/^bad return$/, :bad_return_rescue
respond ~r/^what number/, :store_number_script
respond ~r/^crash$/, :crash_script
respond ~r/^echo\s+(?<text>.*)$/, :echo_script
respond ~r/ping/, :ping_script
# A module-based script
script(Clover.Test.Hexadecimal)
respond ~r/^what time is it/i, message, _match, _data do
say(message, "4:30")
end
respond ~r/^type\s+(?<text>.*)$/, :type_script
respond ~r/^quicktype\s+(?<text>.*)$/, :quick_type_script
overhear ~r/\bhello|hi|good morning\b/i, :greeting_script
overhear ~r/^what day is it/i, message, _match, _data do
say(message, "Every day is like Sunday")
end
# Scripts
def ping_script(message, _match, _data) do
say(message, "pong")
end
def pid_script(message, _match, _data) do
say(message, inspect(self()))
end
def greeting_script(message, _match, _data) do
say(message, "hi")
end
def crash_script(_message, _match, _data) do
raise "CRASH!"
end
# Returns an invalid value
def bad_return_script(message, _match, _data) do
message
|> Map.put(:action, :invalid_action)
|> Map.put(:text, "oops!")
end
def bad_return_rescue(message, _match, _data) do
say(message, "rescued bad return")
end
def store_number_script(message, _match, _data) do
number = Brain.get(message, :number) || 0
Brain.put(message, :number, number + 1)
say(message, "you are number #{number + 1}")
end
def echo_script(message, %{named_captures: %{"text" => text}}, data) do
{say(message, text), data}
end
def type_script(message, %{named_captures: %{"text" => text}}, _data) do
[
message |> typing(),
message |> say(text, delay: 1500)
]
end
def quick_type_script(message, %{named_captures: %{"text" => text}}, _data) do
[
message |> typing(),
message |> say(text, delay: 10)
]
end
defp log(level, message, opts) do
alias Clover.Util.Logger
Logger.log(level, "test robot", message, opts)
end
end