2016-06-17 03:33:32 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-11-14 04:31:45 -05:00
|
|
|
describe Gitlab::Ci::Config::Entry::Validatable do
|
2017-08-24 08:54:27 -04:00
|
|
|
let(:entry) do
|
|
|
|
Class.new(Gitlab::Ci::Config::Entry::Node) do
|
|
|
|
include Gitlab::Ci::Config::Entry::Validatable
|
|
|
|
end
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.validator' do
|
|
|
|
before do
|
2016-11-14 04:31:45 -05:00
|
|
|
entry.class_eval do
|
2016-06-17 03:33:32 -04:00
|
|
|
attr_accessor :test_attribute
|
|
|
|
|
|
|
|
validations do
|
|
|
|
validates :test_attribute, presence: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns validator' do
|
2016-11-14 04:31:45 -05:00
|
|
|
expect(entry.validator.superclass)
|
|
|
|
.to be Gitlab::Ci::Config::Entry::Validator
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
|
2016-08-09 05:29:32 -04:00
|
|
|
it 'returns only one validator to mitigate leaks' do
|
2016-11-14 04:31:45 -05:00
|
|
|
expect { entry.validator }.not_to change { entry.validator }
|
2016-08-09 05:29:32 -04:00
|
|
|
end
|
|
|
|
|
2016-11-14 04:31:45 -05:00
|
|
|
context 'when validating entry instance' do
|
2017-08-24 08:54:27 -04:00
|
|
|
let(:entry_instance) { entry.new('something') }
|
2016-06-17 03:33:32 -04:00
|
|
|
|
|
|
|
context 'when attribute is valid' do
|
|
|
|
before do
|
2016-11-14 04:31:45 -05:00
|
|
|
entry_instance.test_attribute = 'valid'
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'instance of validator is valid' do
|
2016-11-14 04:31:45 -05:00
|
|
|
expect(entry.validator.new(entry_instance)).to be_valid
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when attribute is not valid' do
|
|
|
|
before do
|
2016-11-14 04:31:45 -05:00
|
|
|
entry_instance.test_attribute = nil
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'instance of validator is invalid' do
|
2016-11-14 04:31:45 -05:00
|
|
|
expect(entry.validator.new(entry_instance)).to be_invalid
|
2016-06-17 03:33:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|