2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-25 05:25:37 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-11-29 06:44:48 -05:00
|
|
|
describe Gitlab::Config::Entry::Simplifiable do
|
2017-08-25 05:25:37 -04:00
|
|
|
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-10-15 23:06:12 -04:00
|
|
|
entry::Something = first
|
|
|
|
entry::DifferentOne = second
|
|
|
|
entry::UnknownStrategy = unknown
|
2017-08-25 05:25:37 -04:00
|
|
|
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
|
|
|
|
expect(first).to receive(:new).with('something', anything)
|
|
|
|
|
|
|
|
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
|
|
|
|
expect(second).to receive(:new).with('test', anything)
|
|
|
|
|
|
|
|
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
|
|
|
|
expect(unknown).to receive(:new).with('test', anything)
|
|
|
|
|
|
|
|
entry.new('test')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-30 07:20:55 -04:00
|
|
|
|
|
|
|
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
|
2017-08-25 05:25:37 -04:00
|
|
|
end
|