File: when_node_spec.rb

package info (click to toggle)
ruby-rubocop-ast 0.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 892 kB
  • sloc: ruby: 10,886; makefile: 4
file content (120 lines) | stat: -rw-r--r-- 2,730 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

RSpec.describe RuboCop::AST::WhenNode do
  let(:when_node) { parse_source(source).ast.children[1] }

  describe '.new' do
    let(:source) do
      ['case',
       'when :foo then bar',
       'end'].join("\n")
    end

    it { expect(when_node.is_a?(described_class)).to be(true) }
  end

  describe '#conditions' do
    context 'with a single condition' do
      let(:source) do
        ['case',
         'when :foo then bar',
         'end'].join("\n")
      end

      it { expect(when_node.conditions.size).to eq(1) }
      it { expect(when_node.conditions).to all(be_literal) }
    end

    context 'with a multiple conditions' do
      let(:source) do
        ['case',
         'when :foo, :bar, :baz then bar',
         'end'].join("\n")
      end

      it { expect(when_node.conditions.size).to eq(3) }
      it { expect(when_node.conditions).to all(be_literal) }
    end
  end

  describe '#each_condition' do
    let(:source) do
      ['case',
       'when :foo, :bar, :baz then bar',
       'end'].join("\n")
    end

    context 'when not passed a block' do
      it { expect(when_node.each_condition.is_a?(Enumerator)).to be(true) }
    end

    context 'when passed a block' do
      it 'yields all the conditions' do
        expect { |b| when_node.each_condition(&b) }
          .to yield_successive_args(*when_node.conditions)
      end
    end
  end

  describe '#then?' do
    context 'with a then keyword' do
      let(:source) do
        ['case',
         'when :foo then bar',
         'end'].join("\n")
      end

      it { expect(when_node.then?).to be_truthy }
    end

    context 'without a then keyword' do
      let(:source) do
        ['case',
         'when :foo',
         '  bar',
         'end'].join("\n")
      end

      it { expect(when_node.then?).to be_falsey }
    end
  end

  describe '#body' do
    context 'with a then keyword' do
      let(:source) do
        ['case',
         'when :foo then :bar',
         'end'].join("\n")
      end

      it { expect(when_node.body.sym_type?).to be(true) }
    end

    context 'without a then keyword' do
      let(:source) do
        ['case',
         'when :foo',
         '  [:bar, :baz]',
         'end'].join("\n")
      end

      it { expect(when_node.body.array_type?).to be(true) }
    end
  end

  describe '#branch_index' do
    let(:source) do
      ['case',
       'when :foo then 1',
       'when :bar then 2',
       'when :baz then 3',
       'end'].join("\n")
    end

    let(:whens) { parse_source(source).ast.children[1...-1] }

    it { expect(whens[0].branch_index).to eq(0) }
    it { expect(whens[1].branch_index).to eq(1) }
    it { expect(whens[2].branch_index).to eq(2) }
  end
end