File: license_template_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (62 lines) | stat: -rw-r--r-- 1,423 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe LicenseTemplate do
  describe '#content' do
    it 'calls a proc exactly once if provided' do
      content_proc = -> { 'bar' }
      expect(content_proc).to receive(:call).once.and_call_original

      lazy = build_template(content_proc)

      expect(lazy.content).to eq('bar')

      # Subsequent calls should not call proc again
      expect(lazy.content).to eq('bar')
    end

    it 'returns a string if provided' do
      lazy = build_template('bar')

      expect(lazy.content).to eq('bar')
    end
  end

  describe '#resolve!' do
    let(:content) do
      <<~TEXT
      Pretend License

      [project]

      Copyright (c) [year] [fullname]
      TEXT
    end

    let(:expected) do
      <<~TEXT
      Pretend License

      Foo Project

      Copyright (c) 1985 Nick Thomas
      TEXT
    end

    let(:template) { build_template(content) }

    it 'updates placeholders in a copy of the template content' do
      expect(template.content.object_id).to eq(content.object_id)

      template.resolve!(project_name: "Foo Project", fullname: "Nick Thomas", year: "1985")

      expect(template.content).to eq(expected)
      expect(template.content.object_id).not_to eq(content.object_id)
    end
  end

  def build_template(content)
    described_class.new(key: 'foo', name: 'foo', project: nil, category: :Other, content: content)
  end
end