Welcome to mirror list, hosted at ThFree Co, Russian Federation.

variable_spec.rb « lexeme « expression « pipeline « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e10ca686baeb1195768eeba2940d22715c1d523 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Pipeline::Expression::Lexeme::Variable do
  describe '.build' do
    it 'creates a new instance of the token' do
      expect(described_class.build('$VARIABLE'))
        .to be_a(described_class)
    end
  end

  describe '.type' do
    it 'is a value lexeme' do
      expect(described_class.type).to eq :value
    end
  end

  describe '#evaluate' do
    let(:lexeme) { described_class.new('VARIABLE') }

    it 'returns variable value if it is defined' do
      expect(lexeme.evaluate(VARIABLE: 'my variable'))
        .to eq 'my variable'
    end

    it 'allows to use a string as a variable key too' do
      expect(lexeme.evaluate('VARIABLE' => 'my variable'))
        .to eq 'my variable'
    end

    it 'returns nil if it is not defined' do
      expect(lexeme.evaluate('OTHER' => 'variable')).to be_nil
      expect(lexeme.evaluate(OTHER: 'variable')).to be_nil
    end

    it 'returns an empty string if it is empty' do
      expect(lexeme.evaluate('VARIABLE' => '')).to eq ''
      expect(lexeme.evaluate(VARIABLE: '')).to eq ''
    end

    it 'does not call with_indifferent_access unnecessarily' do
      variables_hash = { VARIABLE: 'my variable' }.with_indifferent_access

      expect(variables_hash).not_to receive(:with_indifferent_access)
      expect(lexeme.evaluate(variables_hash)).to eq 'my variable'
    end
  end
end