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
|
require_relative 'struct_shared'
module Concurrent
RSpec.describe SettableStruct do
it_should_behave_like :struct
it_should_behave_like :mergeable_struct
let(:anonymous) { described_class.new(:name, :address, :zip) }
let!(:customer) { described_class.new('Customer', :name, :address, :zip) }
let(:joe) { customer.new('Joe Smith', '123 Maple, Anytown NC', 12345) }
let(:empty) { customer.new }
subject { anonymous.new }
context 'definition' do
it 'defines a setter for each member' do
members = [:Foo, :bar, 'baz']
structs = [
described_class.new(*members).new,
described_class.new('ClassForCheckingSetterDefinition', *members).new
]
structs.each do |struct|
members.each do |member|
func = "#{member}="
expect(struct).to respond_to func
method = struct.method(func)
expect(method.arity).to eq 1
end
end
end
end
context '#[member]=' do
it 'sets the value when given a valid symbol member' do
expect(empty[:name] = 'Jane').to eq 'Jane'
expect(subject[:name] = 'Jane').to eq 'Jane'
end
it 'sets the value when given a valid string member' do
expect(empty['name'] = 'Jane').to eq 'Jane'
expect(subject['name'] = 'Jane').to eq 'Jane'
end
it 'raises an exception when given a non-existent symbol member' do
expect{empty[:fooarbaz] = 'Jane'}.to raise_error(NameError)
expect{subject[:fooarbaz] = 'Jane'}.to raise_error(NameError)
end
it 'raises an exception when given a non-existent string member' do
expect{empty['fooarbaz'] = 'Jane'}.to raise_error(NameError)
expect{subject['fooarbaz'] = 'Jane'}.to raise_error(NameError)
end
it 'raises an exception when given a symbol member that has already been set' do
empty[:name] = 'John'
subject[:name] = 'John'
expect{empty[:name] = 'Jane'}.to raise_error(ImmutabilityError)
expect{subject[:name] = 'Jane'}.to raise_error(ImmutabilityError)
end
it 'raises an exception when given a string member that has already been set' do
empty['name'] = 'John'
subject['name'] = 'John'
expect{empty['name'] = 'Jane'}.to raise_error(ImmutabilityError)
expect{subject['name'] = 'Jane'}.to raise_error(ImmutabilityError)
end
end
context '#[index]=' do
it 'sets the value when given a valid index' do
expect(empty[0] = 'Jane').to eq 'Jane'
expect(subject[0] = 'Jane').to eq 'Jane'
end
it 'raises an exception when given an out-of-bound index' do
expect{empty[100] = 'Jane'}.to raise_error(IndexError)
expect{subject[100] = 'Jane'}.to raise_error(IndexError)
end
it 'raises an exception when given an index that has already been set' do
empty[0] = 'John'
subject[0] = 'John'
expect{empty[0] = 'Jane'}.to raise_error(ImmutabilityError)
expect{subject[0] = 'Jane'}.to raise_error(ImmutabilityError)
end
end
context 'synchronization' do
it 'protects #values' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.values
end
it 'protects #values_at' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.values_at(0)
end
it 'protects #[index]' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject[0]
end
it 'protects #[member]' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject[:name]
end
it 'protects getter methods' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.name
end
it 'protects #[index]=' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject[0] = :foo
end
it 'protects #[member]=' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject[:name] = :foo
end
it 'protects getter methods' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.name = :foo
end
it 'protects #to_s' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.to_s
end
it 'protects #inspect' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.inspect
end
it 'protects #to_h' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.to_h
end
it 'protects #merge' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.merge(subject.members.first => subject.values.first)
end
it 'protects #==' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject == joe
end
it 'protects #each' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.each{|value| nil }
end
it 'protects #each_pair' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.each_pair{|member, value| nil }
end
it 'protects #select' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.select{|value| false }
end
it 'protects #initialize_copy' do
expect(subject).to receive(:synchronize).at_least(:once).with(no_args).and_call_original
subject.clone
end
end
context 'copy' do
context '#dup' do
it 'retains settability of members' do
subject['name'] = 'John'
expect { subject.dup['name'] = 'Jane' }
.to raise_error(ImmutabilityError)
expect(subject['address'] = 'Earth').to eq('Earth')
end
end
context '#clone' do
it 'retains settability of members' do
subject['name'] = 'John'
expect { subject.clone['name'] = 'Jane' }
.to raise_error(ImmutabilityError)
expect(subject['address'] = 'Earth').to eq('Earth')
end
end
end
end
end
|