summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/gitlab/policy_rule_boolean_spec.rb
blob: e6fb9ab9d57e2d0b529f94c3305a7fef568c8747 (plain)
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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/gitlab/policy_rule_boolean'

RSpec.describe RuboCop::Cop::Gitlab::PolicyRuleBoolean do
  include CopHelper

  subject(:cop) { described_class.new }

  it 'registers offense for &&' do
    expect_offense(<<~SOURCE)
      rule { conducts_electricity && batteries }.enable :light_bulb
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ && is not allowed within a rule block. Did you mean to use `&`?
    SOURCE
  end

  it 'registers offense for ||' do
    expect_offense(<<~SOURCE)
      rule { conducts_electricity || batteries }.enable :light_bulb
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ || is not allowed within a rule block. Did you mean to use `|`?
    SOURCE
  end

  it 'registers offense for if' do
    expect_offense(<<~SOURCE)
      rule { if conducts_electricity then can?(:magnetize) else batteries end }.enable :motor
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ if and ternary operators are not allowed within a rule block.
    SOURCE
  end

  it 'registers offense for ternary operator' do
    expect_offense(<<~SOURCE)
      rule { conducts_electricity ? can?(:magnetize) : batteries }.enable :motor
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ if and ternary operators are not allowed within a rule block.
    SOURCE
  end

  it 'registers no offense for &' do
    expect_no_offenses(<<~SOURCE)
      rule { conducts_electricity & batteries }.enable :light_bulb
    SOURCE
  end

  it 'registers no offense for |' do
    expect_no_offenses(<<~SOURCE)
      rule { conducts_electricity | batteries }.enable :light_bulb
    SOURCE
  end
end