2016-06-30 11:34:19 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::SlashCommands::Extractor do
|
2016-08-13 12:58:51 -04:00
|
|
|
let(:definitions) do
|
|
|
|
Class.new do
|
|
|
|
include Gitlab::SlashCommands::Dsl
|
|
|
|
|
|
|
|
command(:reopen, :open) { }
|
|
|
|
command(:assign) { }
|
|
|
|
command(:labels) { }
|
|
|
|
command(:power) { }
|
|
|
|
end.command_definitions
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:extractor) { described_class.new(definitions) }
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
shared_examples 'command with no argument' do
|
|
|
|
it 'extracts command' do
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(original_msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-17 19:58:52 -04:00
|
|
|
expect(commands).to eq [['reopen']]
|
2016-08-13 12:58:51 -04:00
|
|
|
expect(msg).to eq final_msg
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'command with a single argument' do
|
|
|
|
it 'extracts command' do
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(original_msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to eq [['assign', '@joe']]
|
2016-08-13 12:58:51 -04:00
|
|
|
expect(msg).to eq final_msg
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'command with multiple arguments' do
|
|
|
|
it 'extracts command' do
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(original_msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to eq [['labels', '~foo ~"bar baz" label']]
|
2016-08-13 12:58:51 -04:00
|
|
|
expect(msg).to eq final_msg
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
describe '#extract_commands' do
|
2016-06-30 11:34:19 -04:00
|
|
|
describe 'command with no argument' do
|
|
|
|
context 'at the start of content' do
|
|
|
|
it_behaves_like 'command with no argument' do
|
2016-08-17 19:58:52 -04:00
|
|
|
let(:original_msg) { "/reopen\nworld" }
|
2016-06-30 11:34:19 -04:00
|
|
|
let(:final_msg) { "world" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in the middle of content' do
|
|
|
|
it_behaves_like 'command with no argument' do
|
2016-08-17 19:58:52 -04:00
|
|
|
let(:original_msg) { "hello\n/reopen\nworld" }
|
2016-06-30 11:34:19 -04:00
|
|
|
let(:final_msg) { "hello\nworld" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in the middle of a line' do
|
|
|
|
it 'does not extract command' do
|
2016-08-17 19:58:52 -04:00
|
|
|
msg = "hello\nworld /reopen"
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
2016-08-17 19:58:52 -04:00
|
|
|
expect(msg).to eq "hello\nworld /reopen"
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'at the end of content' do
|
|
|
|
it_behaves_like 'command with no argument' do
|
2016-08-17 19:58:52 -04:00
|
|
|
let(:original_msg) { "hello\n/reopen" }
|
2016-08-13 12:58:51 -04:00
|
|
|
let(:final_msg) { "hello" }
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'command with a single argument' do
|
|
|
|
context 'at the start of content' do
|
|
|
|
it_behaves_like 'command with a single argument' do
|
|
|
|
let(:original_msg) { "/assign @joe\nworld" }
|
|
|
|
let(:final_msg) { "world" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in the middle of content' do
|
|
|
|
it_behaves_like 'command with a single argument' do
|
|
|
|
let(:original_msg) { "hello\n/assign @joe\nworld" }
|
|
|
|
let(:final_msg) { "hello\nworld" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in the middle of a line' do
|
|
|
|
it 'does not extract command' do
|
|
|
|
msg = "hello\nworld /assign @joe"
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
|
|
|
expect(msg).to eq "hello\nworld /assign @joe"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'at the end of content' do
|
|
|
|
it_behaves_like 'command with a single argument' do
|
|
|
|
let(:original_msg) { "hello\n/assign @joe" }
|
2016-08-13 12:58:51 -04:00
|
|
|
let(:final_msg) { "hello" }
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when argument is not separated with a space' do
|
|
|
|
it 'does not extract command' do
|
|
|
|
msg = "hello\n/assign@joe\nworld"
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
|
|
|
expect(msg).to eq "hello\n/assign@joe\nworld"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'command with multiple arguments' do
|
|
|
|
context 'at the start of content' do
|
|
|
|
it_behaves_like 'command with multiple arguments' do
|
|
|
|
let(:original_msg) { %(/labels ~foo ~"bar baz" label\nworld) }
|
|
|
|
let(:final_msg) { "world" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in the middle of content' do
|
|
|
|
it_behaves_like 'command with multiple arguments' do
|
|
|
|
let(:original_msg) { %(hello\n/labels ~foo ~"bar baz" label\nworld) }
|
|
|
|
let(:final_msg) { "hello\nworld" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in the middle of a line' do
|
|
|
|
it 'does not extract command' do
|
|
|
|
msg = %(hello\nworld /labels ~foo ~"bar baz" label)
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
|
|
|
expect(msg).to eq %(hello\nworld /labels ~foo ~"bar baz" label)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'at the end of content' do
|
|
|
|
it_behaves_like 'command with multiple arguments' do
|
|
|
|
let(:original_msg) { %(hello\n/labels ~foo ~"bar baz" label) }
|
2016-08-13 12:58:51 -04:00
|
|
|
let(:final_msg) { "hello" }
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when argument is not separated with a space' do
|
|
|
|
it 'does not extract command' do
|
|
|
|
msg = %(hello\n/labels~foo ~"bar baz" label\nworld)
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
|
|
|
expect(msg).to eq %(hello\n/labels~foo ~"bar baz" label\nworld)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'extracts command with multiple arguments and various prefixes' do
|
|
|
|
msg = %(hello\n/power @user.name %9.10 ~"bar baz.2"\nworld)
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to eq [['power', '@user.name %9.10 ~"bar baz.2"']]
|
|
|
|
expect(msg).to eq "hello\nworld"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'extracts multiple commands' do
|
2016-08-17 19:58:52 -04:00
|
|
|
msg = %(hello\n/power @user.name %9.10 ~"bar baz.2" label\nworld\n/reopen)
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-17 19:58:52 -04:00
|
|
|
expect(commands).to eq [['power', '@user.name %9.10 ~"bar baz.2" label'], ['reopen']]
|
2016-08-13 12:58:51 -04:00
|
|
|
expect(msg).to eq "hello\nworld"
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not alter original content if no command is found' do
|
|
|
|
msg = 'Fixes #123'
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
|
|
|
expect(msg).to eq 'Fixes #123'
|
|
|
|
end
|
2016-08-09 16:47:29 -04:00
|
|
|
|
|
|
|
it 'does not extract commands inside a blockcode' do
|
2016-08-10 08:12:09 -04:00
|
|
|
msg = "Hello\r\n```\r\nThis is some text\r\n/close\r\n/assign @user\r\n```\r\n\r\nWorld"
|
2016-08-09 16:47:29 -04:00
|
|
|
expected = msg.delete("\r")
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-08-09 16:47:29 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
|
|
|
expect(msg).to eq expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not extract commands inside a blockquote' do
|
|
|
|
msg = "Hello\r\n>>>\r\nThis is some text\r\n/close\r\n/assign @user\r\n>>>\r\n\r\nWorld"
|
|
|
|
expected = msg.delete("\r")
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-08-09 16:47:29 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
|
|
|
expect(msg).to eq expected
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not extract commands inside a HTML tag' do
|
2016-08-10 08:12:09 -04:00
|
|
|
msg = "Hello\r\n<div>\r\nThis is some text\r\n/close\r\n/assign @user\r\n</div>\r\n\r\nWorld"
|
2016-08-09 16:47:29 -04:00
|
|
|
expected = msg.delete("\r")
|
2016-08-13 12:58:51 -04:00
|
|
|
msg, commands = extractor.extract_commands(msg)
|
2016-08-09 16:47:29 -04:00
|
|
|
|
|
|
|
expect(commands).to be_empty
|
|
|
|
expect(msg).to eq expected
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
end
|