extractor_spec.rb 6.89 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::SlashCommands::Extractor do
4 5 6 7 8 9 10 11 12 13 14 15
  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) }
16 17 18

  shared_examples 'command with no argument' do
    it 'extracts command' do
19
      msg, commands = extractor.extract_commands(original_msg)
20

Douwe Maan's avatar
Douwe Maan committed
21
      expect(commands).to eq [['reopen']]
22
      expect(msg).to eq final_msg
23 24 25 26 27
    end
  end

  shared_examples 'command with a single argument' do
    it 'extracts command' do
28
      msg, commands = extractor.extract_commands(original_msg)
29 30

      expect(commands).to eq [['assign', '@joe']]
31
      expect(msg).to eq final_msg
32 33 34 35 36
    end
  end

  shared_examples 'command with multiple arguments' do
    it 'extracts command' do
37
      msg, commands = extractor.extract_commands(original_msg)
38 39

      expect(commands).to eq [['labels', '~foo ~"bar baz" label']]
40
      expect(msg).to eq final_msg
41 42 43
    end
  end

44
  describe '#extract_commands' do
45 46 47
    describe 'command with no argument' do
      context 'at the start of content' do
        it_behaves_like 'command with no argument' do
Douwe Maan's avatar
Douwe Maan committed
48
          let(:original_msg) { "/reopen\nworld" }
49 50 51 52 53 54
          let(:final_msg) { "world" }
        end
      end

      context 'in the middle of content' do
        it_behaves_like 'command with no argument' do
Douwe Maan's avatar
Douwe Maan committed
55
          let(:original_msg) { "hello\n/reopen\nworld" }
56 57 58 59 60 61
          let(:final_msg) { "hello\nworld" }
        end
      end

      context 'in the middle of a line' do
        it 'does not extract command' do
Douwe Maan's avatar
Douwe Maan committed
62
          msg = "hello\nworld /reopen"
63
          msg, commands = extractor.extract_commands(msg)
64 65

          expect(commands).to be_empty
Douwe Maan's avatar
Douwe Maan committed
66
          expect(msg).to eq "hello\nworld /reopen"
67 68 69 70 71
        end
      end

      context 'at the end of content' do
        it_behaves_like 'command with no argument' do
Douwe Maan's avatar
Douwe Maan committed
72
          let(:original_msg) { "hello\n/reopen" }
73
          let(:final_msg) { "hello" }
74 75 76 77 78 79 80 81 82 83
        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
84 85 86 87 88 89 90 91

        it 'allows slash in command arguments' do
          msg = "/assign @joe / @jane\nworld"
          msg, commands = extractor.extract_commands(msg)

          expect(commands).to eq [['assign', '@joe / @jane']]
          expect(msg).to eq 'world'
        end
92 93 94 95 96 97 98 99 100 101 102 103
      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"
104
          msg, commands = extractor.extract_commands(msg)
105 106 107 108 109 110 111 112 113

          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" }
114
          let(:final_msg) { "hello" }
115 116 117 118 119 120
        end
      end

      context 'when argument is not separated with a space' do
        it 'does not extract command' do
          msg = "hello\n/assign@joe\nworld"
121
          msg, commands = extractor.extract_commands(msg)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

          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)
147
          msg, commands = extractor.extract_commands(msg)
148 149 150 151 152 153 154 155 156

          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) }
157
          let(:final_msg) { "hello" }
158 159 160 161 162 163
        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)
164
          msg, commands = extractor.extract_commands(msg)
165 166 167 168 169 170 171 172 173

          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)
174
      msg, commands = extractor.extract_commands(msg)
175 176 177 178 179 180

      expect(commands).to eq [['power', '@user.name %9.10 ~"bar baz.2"']]
      expect(msg).to eq "hello\nworld"
    end

    it 'extracts multiple commands' do
Douwe Maan's avatar
Douwe Maan committed
181
      msg = %(hello\n/power @user.name %9.10 ~"bar baz.2" label\nworld\n/reopen)
182
      msg, commands = extractor.extract_commands(msg)
183

Douwe Maan's avatar
Douwe Maan committed
184
      expect(commands).to eq [['power', '@user.name %9.10 ~"bar baz.2" label'], ['reopen']]
185
      expect(msg).to eq "hello\nworld"
186 187 188 189
    end

    it 'does not alter original content if no command is found' do
      msg = 'Fixes #123'
190
      msg, commands = extractor.extract_commands(msg)
191 192 193 194

      expect(commands).to be_empty
      expect(msg).to eq 'Fixes #123'
    end
195 196

    it 'does not extract commands inside a blockcode' do
197
      msg = "Hello\r\n```\r\nThis is some text\r\n/close\r\n/assign @user\r\n```\r\n\r\nWorld"
198
      expected = msg.delete("\r")
199
      msg, commands = extractor.extract_commands(msg)
200 201 202 203 204 205 206 207

      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")
208
      msg, commands = extractor.extract_commands(msg)
209 210 211 212 213 214

      expect(commands).to be_empty
      expect(msg).to eq expected
    end

    it 'does not extract commands inside a HTML tag' do
215
      msg = "Hello\r\n<div>\r\nThis is some text\r\n/close\r\n/assign @user\r\n</div>\r\n\r\nWorld"
216
      expected = msg.delete("\r")
217
      msg, commands = extractor.extract_commands(msg)
218 219 220 221

      expect(commands).to be_empty
      expect(msg).to eq expected
    end
222 223
  end
end