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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Config::Entry::Validators::NestedArrayHelpers do
let(:config_struct) do
Struct.new(:value, keyword_init: true) do
include ActiveModel::Validations
extend Gitlab::Config::Entry::Validators::NestedArrayHelpers
validates_each :value do |record, attr, value|
unless validate_nested_array(value, 2) { |v| v.is_a?(Integer) }
record.errors.add(attr, "is invalid")
end
end
end
end
describe '#validate_nested_array' do
let(:config) { config_struct.new(value: value) }
subject(:errors) { config.errors }
before do
config.valid?
end
context 'with valid values' do
context 'with arrays of integers' do
let(:value) { [10, 11] }
it { is_expected.to be_empty }
end
context 'with nested arrays of integers' do
let(:value) { [10, [11, 12]] }
it { is_expected.to be_empty }
end
end
context 'with invalid values' do
subject(:error_messages) { errors.messages }
context 'with single integers' do
let(:value) { 10 }
it { is_expected.to eq({ value: ['is invalid'] }) }
end
context 'when it is nested over the limit' do
let(:value) { [10, [11, [12]]] }
it { is_expected.to eq({ value: ['is invalid'] }) }
end
context 'when a value in the array is not valid' do
let(:value) { [10, 11.5] }
it { is_expected.to eq({ value: ['is invalid'] }) }
end
context 'when a value in the nested array is not valid' do
let(:value) { [10, [11, 12.5]] }
it { is_expected.to eq({ value: ['is invalid'] }) }
end
end
end
end
|