Skip to content

Commit 9f003b0

Browse files
authored
Merge pull request #12 from github/generic_arguments_test_helper
support generic arguments in the `chatop` test helper
2 parents c590b78 + 9d34d6b commit 9f003b0

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

lib/chatops/controller/test_case_helpers.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ def chat(message, user, room_id = "123")
2222
metadata["regex"] = Regexp.new("^#{metadata["regex"]}$", "i")
2323
metadata
2424
}
25-
matcher = matchers.find { |matcher| matcher["regex"].match(message) }
2625

27-
raise NoMatchingCommandRegex.new("No command matches '#{message}'") unless matcher
26+
named_params, command = extract_named_params(message)
2827

29-
match_data = matcher["regex"].match(message)
30-
jsonrpc_params = {}
28+
matcher = matchers.find { |matcher| matcher["regex"].match(command) }
29+
30+
raise NoMatchingCommandRegex.new("No command matches '#{command}'") unless matcher
31+
32+
match_data = matcher["regex"].match(command)
33+
jsonrpc_params = named_params.dup
3134
matcher["params"].each do |param|
3235
jsonrpc_params[param] = match_data[param.to_sym]
3336
end
@@ -47,4 +50,18 @@ def chatop_error
4750
json_response = JSON.load(response.body)
4851
json_response["error"]["message"]
4952
end
53+
54+
def extract_named_params(command_string)
55+
params = {}
56+
57+
while last_index = command_string.rindex(" --")
58+
arg = command_string[last_index..-1]
59+
matches = arg.match(/ --(\S+)(.*)/)
60+
params[matches[1]] = matches[2].strip
61+
command_string = command_string.slice(0, last_index)
62+
end
63+
64+
command_string = command_string.strip
65+
[params, command_string]
66+
end
5067
end

spec/lib/chatops/controller_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ def ensure_app_given
215215
expect(chatop_response).to eq "You can deploy foobar just fine."
216216
end
217217

218+
it "works with generic arguments" do
219+
chat "where can i deploy foobar --fruit apple --vegetable green celery", "bhuga"
220+
expect(request.params["action"]).to eq "wcid"
221+
expect(request.params["user"]).to eq "bhuga"
222+
expect(request.params["params"]["app"]).to eq "foobar"
223+
expect(request.params["params"]["fruit"]).to eq "apple"
224+
expect(request.params["params"]["vegetable"]).to eq "green celery"
225+
expect(chatop_response).to eq "You can deploy foobar just fine."
226+
end
227+
218228
it "anchors regexes" do
219229
expect {
220230
chat "too bad that this message doesn't start with where can i deploy foobar", "bhuga"

0 commit comments

Comments
 (0)