summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/ansi2json/line_spec.rb
blob: 8b1cd812a709cafbb67e1a04b1724162efddf506 (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
111
112
113
114
115
116
117
118
119
120
121
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Ansi2json::Line do
  let(:offset) { 0 }
  let(:style) { Gitlab::Ci::Ansi2json::Style.new }

  subject { described_class.new(offset: offset, style: style) }

  describe '#<<' do
    it 'appends new data to the current segment' do
      expect { subject << 'test 1' }.to change { subject.current_segment.text }
      expect(subject.current_segment.text).to eq('test 1')

      expect { subject << ', test 2' }.to change { subject.current_segment.text }
      expect(subject.current_segment.text).to eq('test 1, test 2')
    end
  end

  describe '#style' do
    context 'when style is passed to the initializer' do
      let(:style) { double }

      it 'returns the same style' do
        expect(subject.style).to eq(style)
      end
    end

    context 'when style is not passed to the initializer' do
      it 'returns the default style' do
        expect(subject.style.set?).to be_falsey
      end
    end
  end

  describe '#update_style' do
    let(:expected_style) do
      Gitlab::Ci::Ansi2json::Style.new(
        fg: 'term-fg-l-yellow',
        bg: 'term-bg-blue',
        mask: 1)
    end

    it 'sets the style' do
      subject.update_style(%w[1 33 44])

      expect(subject.style).to eq(expected_style)
    end
  end

  describe '#add_section' do
    it 'appends a new section to the list' do
      subject.add_section('section_1')
      subject.add_section('section_2')

      expect(subject.sections).to eq(%w[section_1 section_2])
    end
  end

  describe '#set_as_section_header' do
    it 'change the section_header to true' do
      expect { subject.set_as_section_header }
        .to change { subject.section_header }
        .to be_truthy
    end
  end

  describe '#set_section_duration' do
    it 'sets and formats the section_duration' do
      subject.set_section_duration(75)

      expect(subject.section_duration).to eq('01:15')
    end
  end

  describe '#flush_current_segment!' do
    context 'when current segment is not empty' do
      before do
        subject << 'some data'
      end

      it 'adds the segment to the list' do
        expect { subject.flush_current_segment! }.to change { subject.segments.count }.by(1)

        expect(subject.segments.map { |s| s[:text] }).to eq(['some data'])
      end

      it 'updates the current segment pointer propagating the style' do
        previous_segment = subject.current_segment

        subject.flush_current_segment!

        expect(subject.current_segment).not_to eq(previous_segment)
        expect(subject.current_segment.style).to eq(previous_segment.style)
      end
    end

    context 'when current segment is empty' do
      it 'does not add any segments to the list' do
        expect { subject.flush_current_segment! }.not_to change { subject.segments.count }
      end

      it 'does not change the current segment' do
        expect { subject.flush_current_segment! }.not_to change { subject.current_segment }
      end
    end
  end

  describe '#to_h' do
    before do
      subject << 'some data'
      subject.update_style(['1'])
    end

    context 'when sections are present' do
      before do
        subject.add_section('section_1')
        subject.add_section('section_2')
      end

      context 'when section header is set' do
        before do
          subject.set_as_section_header
        end

        it 'serializes the attributes set' do
          result = {
            offset: 0,
            content: [{ text: 'some data', style: 'term-bold' }],
            section: 'section_2',
            section_header: true
          }

          expect(subject.to_h).to eq(result)
        end
      end

      context 'when section duration is set' do
        before do
          subject.set_section_duration(75)
        end

        it 'serializes the attributes set' do
          result = {
            offset: 0,
            content: [{ text: 'some data', style: 'term-bold' }],
            section: 'section_2',
            section_duration: '01:15'
          }

          expect(subject.to_h).to eq(result)
        end
      end
    end

    context 'when there are no sections' do
      it 'serializes the attributes set' do
        result = {
          offset: 0,
          content: [{ text: 'some data', style: 'term-bold' }]
        }

        expect(subject.to_h).to eq(result)
      end
    end
  end
end