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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
# frozen_string_literal: true
require 'spec_helper'
module Grape
module Util
describe InheritableSetting do
before do
described_class.reset_global!
end
let(:parent) do
described_class.new.tap do |settings|
settings.global[:global_thing] = :global_foo_bar
settings.namespace[:namespace_thing] = :namespace_foo_bar
settings.namespace_inheritable[:namespace_inheritable_thing] = :namespace_inheritable_foo_bar
settings.namespace_stackable[:namespace_stackable_thing] = :namespace_stackable_foo_bar
settings.namespace_reverse_stackable[:namespace_reverse_stackable_thing] = :namespace_reverse_stackable_foo_bar
settings.route[:route_thing] = :route_foo_bar
end
end
let(:other_parent) do
described_class.new.tap do |settings|
settings.namespace[:namespace_thing] = :namespace_foo_bar_other
settings.namespace_inheritable[:namespace_inheritable_thing] = :namespace_inheritable_foo_bar_other
settings.namespace_stackable[:namespace_stackable_thing] = :namespace_stackable_foo_bar_other
settings.namespace_reverse_stackable[:namespace_reverse_stackable_thing] = :namespace_reverse_stackable_foo_bar_other
settings.route[:route_thing] = :route_foo_bar_other
end
end
before do
subject.inherit_from parent
end
describe '#global' do
it 'sets a global value' do
subject.global[:some_thing] = :foo_bar
expect(subject.global[:some_thing]).to eq :foo_bar
subject.global[:some_thing] = :foo_bar_next
expect(subject.global[:some_thing]).to eq :foo_bar_next
end
it 'sets the global inherited values' do
expect(subject.global[:global_thing]).to eq :global_foo_bar
end
it 'overrides global values' do
subject.global[:global_thing] = :global_new_foo_bar
expect(parent.global[:global_thing]).to eq :global_new_foo_bar
end
it 'handles different parents' do
subject.global[:global_thing] = :global_new_foo_bar
subject.inherit_from other_parent
expect(parent.global[:global_thing]).to eq :global_new_foo_bar
expect(other_parent.global[:global_thing]).to eq :global_new_foo_bar
end
end
describe '#api_class' do
it 'is specific to the class' do
subject.api_class[:some_thing] = :foo_bar
parent.api_class[:some_thing] = :some_thing
expect(subject.api_class[:some_thing]).to eq :foo_bar
expect(parent.api_class[:some_thing]).to eq :some_thing
end
end
describe '#namespace' do
it 'sets a value until the end of a namespace' do
subject.namespace[:some_thing] = :foo_bar
expect(subject.namespace[:some_thing]).to eq :foo_bar
end
it 'uses new values when a new namespace starts' do
subject.namespace[:namespace_thing] = :new_namespace_foo_bar
expect(subject.namespace[:namespace_thing]).to eq :new_namespace_foo_bar
expect(parent.namespace[:namespace_thing]).to eq :namespace_foo_bar
end
end
describe '#namespace_inheritable' do
it 'works with inheritable values' do
expect(subject.namespace_inheritable[:namespace_inheritable_thing]).to eq :namespace_inheritable_foo_bar
end
it 'handles different parents' do
expect(subject.namespace_inheritable[:namespace_inheritable_thing]).to eq :namespace_inheritable_foo_bar
subject.inherit_from other_parent
expect(subject.namespace_inheritable[:namespace_inheritable_thing]).to eq :namespace_inheritable_foo_bar_other
subject.inherit_from parent
expect(subject.namespace_inheritable[:namespace_inheritable_thing]).to eq :namespace_inheritable_foo_bar
subject.inherit_from other_parent
subject.namespace_inheritable[:namespace_inheritable_thing] = :my_thing
expect(subject.namespace_inheritable[:namespace_inheritable_thing]).to eq :my_thing
subject.inherit_from parent
expect(subject.namespace_inheritable[:namespace_inheritable_thing]).to eq :my_thing
end
end
describe '#namespace_stackable' do
it 'works with stackable values' do
expect(subject.namespace_stackable[:namespace_stackable_thing]).to eq [:namespace_stackable_foo_bar]
subject.inherit_from other_parent
expect(subject.namespace_stackable[:namespace_stackable_thing]).to eq [:namespace_stackable_foo_bar_other]
end
end
describe '#namespace_reverse_stackable' do
it 'works with reverse stackable values' do
expect(subject.namespace_reverse_stackable[:namespace_reverse_stackable_thing]).to eq [:namespace_reverse_stackable_foo_bar]
subject.inherit_from other_parent
expect(subject.namespace_reverse_stackable[:namespace_reverse_stackable_thing]).to eq [:namespace_reverse_stackable_foo_bar_other]
end
end
describe '#route' do
it 'sets a value until the next route' do
subject.route[:some_thing] = :foo_bar
expect(subject.route[:some_thing]).to eq :foo_bar
subject.route_end
expect(subject.route[:some_thing]).to be_nil
end
it 'works with route values' do
expect(subject.route[:route_thing]).to eq :route_foo_bar
end
end
describe '#api_class' do
it 'is specific to the class' do
subject.api_class[:some_thing] = :foo_bar
expect(subject.api_class[:some_thing]).to eq :foo_bar
end
end
describe '#inherit_from' do
it 'notifies clones' do
new_settings = subject.point_in_time_copy
expect(new_settings).to receive(:inherit_from).with(other_parent)
subject.inherit_from other_parent
end
end
describe '#point_in_time_copy' do
let!(:cloned_obj) { subject.point_in_time_copy }
it 'resets point_in_time_copies' do
expect(cloned_obj.point_in_time_copies).to be_empty
end
it 'decouples namespace values' do
subject.namespace[:namespace_thing] = :namespace_foo_bar
cloned_obj.namespace[:namespace_thing] = :new_namespace_foo_bar
expect(subject.namespace[:namespace_thing]).to eq :namespace_foo_bar
end
it 'decouples namespace inheritable values' do
expect(cloned_obj.namespace_inheritable[:namespace_inheritable_thing]).to eq :namespace_inheritable_foo_bar
subject.namespace_inheritable[:namespace_inheritable_thing] = :my_thing
expect(subject.namespace_inheritable[:namespace_inheritable_thing]).to eq :my_thing
expect(cloned_obj.namespace_inheritable[:namespace_inheritable_thing]).to eq :namespace_inheritable_foo_bar
cloned_obj.namespace_inheritable[:namespace_inheritable_thing] = :my_cloned_thing
expect(cloned_obj.namespace_inheritable[:namespace_inheritable_thing]).to eq :my_cloned_thing
expect(subject.namespace_inheritable[:namespace_inheritable_thing]).to eq :my_thing
end
it 'decouples namespace stackable values' do
expect(cloned_obj.namespace_stackable[:namespace_stackable_thing]).to eq [:namespace_stackable_foo_bar]
subject.namespace_stackable[:namespace_stackable_thing] = :other_thing
expect(subject.namespace_stackable[:namespace_stackable_thing]).to eq %i[namespace_stackable_foo_bar other_thing]
expect(cloned_obj.namespace_stackable[:namespace_stackable_thing]).to eq [:namespace_stackable_foo_bar]
end
it 'decouples namespace reverse stackable values' do
expect(cloned_obj.namespace_reverse_stackable[:namespace_reverse_stackable_thing]).to eq [:namespace_reverse_stackable_foo_bar]
subject.namespace_reverse_stackable[:namespace_reverse_stackable_thing] = :other_thing
expect(subject.namespace_reverse_stackable[:namespace_reverse_stackable_thing]).to eq %i[other_thing namespace_reverse_stackable_foo_bar]
expect(cloned_obj.namespace_reverse_stackable[:namespace_reverse_stackable_thing]).to eq [:namespace_reverse_stackable_foo_bar]
end
it 'decouples route values' do
expect(cloned_obj.route[:route_thing]).to eq :route_foo_bar
subject.route[:route_thing] = :new_route_foo_bar
expect(cloned_obj.route[:route_thing]).to eq :route_foo_bar
end
it 'adds itself to original as clone' do
expect(subject.point_in_time_copies).to include(cloned_obj)
end
end
describe '#to_hash' do
it 'return all settings as a hash' do
subject.global[:global_thing] = :global_foo_bar
subject.namespace[:namespace_thing] = :namespace_foo_bar
subject.namespace_inheritable[:namespace_inheritable_thing] = :namespace_inheritable_foo_bar
subject.namespace_stackable[:namespace_stackable_thing] = [:namespace_stackable_foo_bar]
subject.namespace_reverse_stackable[:namespace_reverse_stackable_thing] = [:namespace_reverse_stackable_foo_bar]
subject.route[:route_thing] = :route_foo_bar
expect(subject.to_hash).to include(global: { global_thing: :global_foo_bar })
expect(subject.to_hash).to include(namespace: { namespace_thing: :namespace_foo_bar })
expect(subject.to_hash).to include(namespace_inheritable: {
namespace_inheritable_thing: :namespace_inheritable_foo_bar
})
expect(subject.to_hash).to include(namespace_stackable: { namespace_stackable_thing: [:namespace_stackable_foo_bar, [:namespace_stackable_foo_bar]] })
expect(subject.to_hash).to include(namespace_reverse_stackable:
{ namespace_reverse_stackable_thing: [[:namespace_reverse_stackable_foo_bar], :namespace_reverse_stackable_foo_bar] })
expect(subject.to_hash).to include(route: { route_thing: :route_foo_bar })
end
end
end
end
end
|