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
102
103
104
105
106
107
108
109
110
|
require 'spec_helper'
describe Gitlab::SlashCommands::Dsl do
before :all do
DummyClass = Class.new do
include Gitlab::SlashCommands::Dsl
desc 'A command with no args'
command :no_args, :none do
"Hello World!"
end
desc 'A command returning a value'
command :returning do
return 42
end
params 'The first argument'
command :one_arg, :once, :first do |arg1|
arg1
end
desc 'A command with two args'
params 'The first argument', 'The second argument'
command :two_args do |arg1, arg2|
[arg1, arg2]
end
command :wildcard do |*args|
args
end
end
end
let(:dummy) { DummyClass.new }
describe '.command_definitions' do
it 'returns an array with commands definitions' do
expected = [
{ name: :no_args, aliases: [:none], description: 'A command with no args', params: [] },
{ name: :returning, aliases: [], description: 'A command returning a value', params: [] },
{ name: :one_arg, aliases: [:once, :first], description: '', params: ['The first argument'] },
{ name: :two_args, aliases: [], description: 'A command with two args', params: ['The first argument', 'The second argument'] },
{ name: :wildcard, aliases: [], description: '', params: [] }
]
expect(DummyClass.command_definitions).to eq expected
end
end
describe '.command_names' do
it 'returns an array with commands definitions' do
expect(DummyClass.command_names).to eq [
:no_args, :none, :returning, :one_arg,
:once, :first, :two_args, :wildcard
]
end
end
describe 'command with no args' do
context 'called with no args' do
it 'succeeds' do
expect(dummy.__send__(:no_args)).to eq 'Hello World!'
end
end
end
describe 'command with an explicit return' do
context 'called with no args' do
it 'succeeds' do
expect(dummy.__send__(:returning)).to eq 42
end
end
end
describe 'command with one arg' do
context 'called with one arg' do
it 'succeeds' do
expect(dummy.__send__(:one_arg, 42)).to eq 42
end
end
end
describe 'command with two args' do
context 'called with two args' do
it 'succeeds' do
expect(dummy.__send__(:two_args, 42, 'foo')).to eq [42, 'foo']
end
end
end
describe 'command with wildcard' do
context 'called with no args' do
it 'succeeds' do
expect(dummy.__send__(:wildcard)).to eq []
end
end
context 'called with one arg' do
it 'succeeds' do
expect(dummy.__send__(:wildcard, 42)).to eq [42]
end
end
context 'called with two args' do
it 'succeeds' do
expect(dummy.__send__(:wildcard, 42, 'foo')).to eq [42, 'foo']
end
end
end
end
|