2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Utils::DeepSize do
|
|
|
|
let(:data) do
|
|
|
|
{
|
|
|
|
a: [1, 2, 3],
|
|
|
|
b: {
|
|
|
|
c: [4, 5],
|
|
|
|
d: [
|
|
|
|
{ e: [[6], [7]] }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:max_size) { 1.kilobyte }
|
|
|
|
let(:max_depth) { 10 }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
subject(:deep_size) { described_class.new(data, max_size: max_size, max_depth: max_depth) }
|
|
|
|
|
|
|
|
it { expect(described_class::DEFAULT_MAX_SIZE).to eq(1.megabyte) }
|
|
|
|
it { expect(described_class::DEFAULT_MAX_DEPTH).to eq(100) }
|
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
context 'when max_size is nil' do
|
|
|
|
let(:max_size) { nil }
|
|
|
|
|
|
|
|
it 'sets max_size to DEFAULT_MAX_SIZE' do
|
|
|
|
expect(subject.instance_variable_get(:@max_size)).to eq(described_class::DEFAULT_MAX_SIZE)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when max_depth is nil' do
|
|
|
|
let(:max_depth) { nil }
|
|
|
|
|
|
|
|
it 'sets max_depth to DEFAULT_MAX_DEPTH' do
|
|
|
|
expect(subject.instance_variable_get(:@max_depth)).to eq(described_class::DEFAULT_MAX_DEPTH)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#valid?' do
|
|
|
|
context 'when data within size and depth limits' do
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
context 'when data not within size limit' do
|
|
|
|
let(:max_size) { 200.bytes }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it { is_expected.not_to be_valid }
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when data not within depth limit' do
|
|
|
|
let(:max_depth) { 2 }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it { is_expected.not_to be_valid }
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
describe '.human_default_max_size' do
|
|
|
|
it 'returns 1 MB' do
|
|
|
|
expect(described_class.human_default_max_size).to eq('1 MB')
|
|
|
|
end
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|