2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
require 'fast_spec_helper'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Config::Entry::Simplifiable do
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '.strategy' do
|
|
|
|
let(:entry) do
|
|
|
|
Class.new(described_class) do
|
|
|
|
strategy :Something, if: -> { 'condition' }
|
|
|
|
strategy :DifferentOne, if: -> { 'condition' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defines entry strategies' do
|
|
|
|
expect(entry.strategies.size).to eq 2
|
|
|
|
expect(entry.strategies.map(&:name))
|
|
|
|
.to eq %i[Something DifferentOne]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'setting strategy by a condition' do
|
|
|
|
let(:first) { double('first strategy') }
|
|
|
|
let(:second) { double('second strategy') }
|
|
|
|
let(:unknown) { double('unknown strategy') }
|
|
|
|
|
|
|
|
before do
|
2019-12-21 20:55:43 +05:30
|
|
|
entry::Something = first
|
|
|
|
entry::DifferentOne = second
|
|
|
|
entry::UnknownStrategy = unknown
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when first strategy should be used' do
|
|
|
|
let(:entry) do
|
|
|
|
Class.new(described_class) do
|
|
|
|
strategy :Something, if: -> (arg) { arg == 'something' }
|
|
|
|
strategy :DifferentOne, if: -> (*) { false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'attemps to load a first strategy' do
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(first).to receive(:new).with('something')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
entry.new('something')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when second strategy should be used' do
|
|
|
|
let(:entry) do
|
|
|
|
Class.new(described_class) do
|
|
|
|
strategy :Something, if: -> (arg) { arg == 'something' }
|
|
|
|
strategy :DifferentOne, if: -> (arg) { arg == 'test' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'attemps to load a second strategy' do
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(second).to receive(:new).with('test')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
entry.new('test')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when neither one is a valid strategy' do
|
|
|
|
let(:entry) do
|
|
|
|
Class.new(described_class) do
|
|
|
|
strategy :Something, if: -> (*) { false }
|
|
|
|
strategy :DifferentOne, if: -> (*) { false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'instantiates an unknown strategy' do
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(unknown).to receive(:new).with('test')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
entry.new('test')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a unknown strategy class is not defined' do
|
|
|
|
let(:entry) do
|
|
|
|
Class.new(described_class) do
|
|
|
|
strategy :String, if: -> (*) { true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when being initialized' do
|
|
|
|
expect { entry.new('something') }
|
|
|
|
.to raise_error ArgumentError, /UndefinedStrategy not available!/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|