|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +RSpec.describe Adyen::HashWithAccessors do |
| 4 | + shared_examples :hash_with_accessors do |
| 5 | + subject do |
| 6 | + h = described_class.new |
| 7 | + h[key] = 1 |
| 8 | + h |
| 9 | + end |
| 10 | + |
| 11 | + it 'returns values of a hashes' do |
| 12 | + expect(subject.arbitrary_accessor).to be 1 |
| 13 | + end |
| 14 | + |
| 15 | + it 'can assign existing values' do |
| 16 | + subject.arbitrary_accessor = 2 |
| 17 | + expect(subject.arbitrary_accessor).to be 2 |
| 18 | + expect(subject[key]).to be 2 |
| 19 | + end |
| 20 | + |
| 21 | + it 'complains if there are arguments for the accessor' do |
| 22 | + expect { subject.arbitrary_accessor(1) }.to raise_error(ArgumentError, 'wrong number of arguments (given 1, expected 0)') |
| 23 | + expect { subject.arbitrary_accessor(1, 2) }.to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 0)') |
| 24 | + end |
| 25 | + |
| 26 | + it 'complains if there are arguments for the accessor =' do |
| 27 | + # using send because i'm not sure how to do this wrong with normal ruby setter calling. |
| 28 | + # just here for completeness |
| 29 | + expect { subject.send(:arbitrary_accessor=) }.to raise_error(ArgumentError, 'wrong number of arguments (given 0, expected 1)') |
| 30 | + expect { subject.send(:arbitrary_accessor=, 1, 2) }.to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') |
| 31 | + end |
| 32 | + |
| 33 | + it 'responds to the accessor' do |
| 34 | + expect(subject).to respond_to(:arbitrary_accessor) |
| 35 | + expect(subject).to respond_to(:arbitrary_accessor=) |
| 36 | + expect(subject).to_not respond_to(:another_accessor) |
| 37 | + expect(subject).to_not respond_to(:another_accessor=) |
| 38 | + end |
| 39 | + |
| 40 | + it "raises when the key doesn't exist" do |
| 41 | + expect { subject.another_accessor }.to raise_error(NoMethodError) |
| 42 | + expect { subject.another_accessor = 1 }.to raise_error(NoMethodError) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context 'with a string key' do |
| 47 | + let(:key) { 'arbitrary_accessor' } |
| 48 | + |
| 49 | + it_behaves_like :hash_with_accessors |
| 50 | + end |
| 51 | + |
| 52 | + context 'with a symbol key' do |
| 53 | + let(:key) { :arbitrary_accessor } |
| 54 | + |
| 55 | + it_behaves_like :hash_with_accessors |
| 56 | + end |
| 57 | + |
| 58 | + context 'with a conflicting key' do |
| 59 | + subject do |
| 60 | + h = described_class.new |
| 61 | + h['keys'] = 'not the keys' |
| 62 | + h |
| 63 | + end |
| 64 | + |
| 65 | + it "does original thing if there'd be a conflict" do |
| 66 | + expect(subject.keys).to eq ['keys'] # the default behaviour |
| 67 | + expect(subject['keys']).to eq 'not the keys' |
| 68 | + end |
| 69 | + |
| 70 | + it 'still does the writer thing even if the reader is defined' do |
| 71 | + subject.keys = 'written keys' |
| 72 | + expect(subject['keys']).to eq 'written keys' |
| 73 | + expect(subject.keys).to eq ['keys'] |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context 'with some other method missing defined' do |
| 78 | + # this test setup is kind of janky, |
| 79 | + # but we want to confirm super is set up correctly |
| 80 | + # We could do a lot more house-keeping if we weren't sure Hash doesn't |
| 81 | + # define its own method_missing and respond_to_missing? by default, |
| 82 | + # and there was any particular reason to clean up properly and remove our |
| 83 | + # called_super method from Hash. |
| 84 | + |
| 85 | + before(:all) do |
| 86 | + class Hash |
| 87 | + def called_super(*args) |
| 88 | + end |
| 89 | + |
| 90 | + def method_missing(*args) |
| 91 | + called_super(:method_missing, *args) |
| 92 | + super |
| 93 | + end |
| 94 | + |
| 95 | + def respond_to_missing?(*args) |
| 96 | + called_super(:respond_to_missing?, *args) |
| 97 | + super |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + subject do |
| 103 | + h = described_class.new |
| 104 | + h[:my_accessor] = 1 |
| 105 | + h |
| 106 | + end |
| 107 | + |
| 108 | + it 'can fall back to another respond_to_missing?' do |
| 109 | + expect(subject).to_not receive(:called_super).with(:respond_to_missing?, :my_accessor, false) |
| 110 | + expect(subject).to respond_to(:my_accessor) |
| 111 | + expect(subject).to receive(:called_super).with(:respond_to_missing?, :literally_anything, false) |
| 112 | + expect(subject.respond_to?(:literally_anything)).to be false |
| 113 | + end |
| 114 | + |
| 115 | + it 'can fall back to another method_missing' do |
| 116 | + expect(subject).to_not receive(:called_super).with(:method_missing, :my_accessor) |
| 117 | + expect(subject.my_accessor).to be 1 |
| 118 | + expect(subject).to receive(:called_super).with(:method_missing, :something_else) |
| 119 | + expect { subject.something_else }.to raise_error(NoMethodError) |
| 120 | + end |
| 121 | + |
| 122 | + end |
| 123 | + |
| 124 | + it "doesn't modify all hashes" do |
| 125 | + expect { {a: 1}.a }.to raise_error(NoMethodError) |
| 126 | + end |
| 127 | +end |
0 commit comments