summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/expression/lexeme/string_spec.rb
blob: 2a6b90d127fc4fd9a8cb2beb2d8826c08cf55099 (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
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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Pipeline::Expression::Lexeme::String do
  describe '.build' do
    it 'creates a new instance of the token' do
      expect(described_class.build('"my string"'))
        .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 '.scan' do
    context 'when using double quotes' do
      it 'correctly identifies string token' do
        scanner = StringScanner.new('"some string"')

        token = described_class.scan(scanner)

        expect(token).not_to be_nil
        expect(token.build.evaluate).to eq 'some string'
      end
    end

    context 'when using single quotes' do
      it 'correctly identifies string token' do
        scanner = StringScanner.new("'some string 2'")

        token = described_class.scan(scanner)

        expect(token).not_to be_nil
        expect(token.build.evaluate).to eq 'some string 2'
      end
    end

    context 'when there are mixed quotes in the string' do
      it 'is a greedy scanner for double quotes' do
        scanner = StringScanner.new('"some string" "and another one"')

        token = described_class.scan(scanner)

        expect(token).not_to be_nil
        expect(token.build.evaluate).to eq 'some string'
      end

      it 'is a greedy scanner for single quotes' do
        scanner = StringScanner.new("'some string' 'and another one'")

        token = described_class.scan(scanner)

        expect(token).not_to be_nil
        expect(token.build.evaluate).to eq 'some string'
      end

      it 'allows to use single quotes inside double quotes' do
        scanner = StringScanner.new(%("some ' string"))

        token = described_class.scan(scanner)

        expect(token).not_to be_nil
        expect(token.build.evaluate).to eq "some ' string"
      end

      it 'allow to use double quotes inside single quotes' do
        scanner = StringScanner.new(%('some " string'))

        token = described_class.scan(scanner)

        expect(token).not_to be_nil
        expect(token.build.evaluate).to eq 'some " string'
      end

      it 'allows to use an empty string inside single quotes' do
        scanner = StringScanner.new(%(''))

        token = described_class.scan(scanner)

        expect(token.build.evaluate).to eq ''
      end

      it 'allow to use an empty string inside double quotes' do
        scanner = StringScanner.new(%(""))

        token = described_class.scan(scanner)

        expect(token.build.evaluate).to eq ''
      end
    end
  end

  describe '#evaluate' do
    it 'returns string value if it is present' do
      string = described_class.new('my string')

      expect(string.evaluate).to eq 'my string'
    end

    it 'returns an empty string if it is empty' do
      string = described_class.new('')

      expect(string.evaluate).to eq ''
    end
  end
end