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
|
require 'yaml'
require 'nenv/environment'
RSpec.describe Nenv::Environment do
class MockEnv < Hash # a hash is close enough
def []=(k, v)
fail TypeError, "no implicit conversion of #{k.class} into String" unless k.respond_to? :to_str
fail TypeError, "no implicit conversion of #{v.class} into String" unless v.respond_to? :to_str
super(k.to_str, v.to_str)
end
end
before { stub_const('ENV', MockEnv.new) }
subject { instance }
shared_examples 'accessor methods' do
describe 'predicate method' do
before { subject.create_method(:foo?) }
it 'responds to it' do
expect(subject).to respond_to(:foo?)
end
context 'when the method already exists' do
let(:error) { described_class::AlreadyExistsError }
let(:message) { 'Method :foo? already exists' }
specify do
expect do
subject.create_method(:foo?)
end.to raise_error(error, message)
end
end
context 'with value stored in ENV' do
before { ENV[sample_key] = value }
describe 'when value is truthy' do
let(:value) { 'true' }
it 'should return true' do
expect(subject.foo?).to eq true
end
end
describe 'when value is falsey' do
let(:value) { '0' }
it 'should return false' do
expect(subject.foo?).to eq false
end
end
end
end
describe 'reader method' do
context 'when added' do
before { subject.create_method(:foo) }
it 'responds to it' do
expect(subject).to respond_to(:foo)
end
context 'when the method already exists' do
let(:error) { described_class::AlreadyExistsError }
let(:message) { 'Method :foo already exists' }
specify do
expect do
subject.create_method(:foo)
end.to raise_error(error, message)
end
end
end
context 'with value stored in ENV' do
before { ENV[sample_key] = value }
context 'with no block' do
before { instance.create_method(:foo) }
let(:value) { '123' }
it 'returns marshalled stored value' do
expect(subject.foo).to eq '123'
end
end
context 'with block' do
before { instance.create_method(:foo) { |data| YAML.load(data) } }
let(:value) { "---\n:foo: 5\n" }
it 'returns unmarshalled stored value' do
expect(subject.foo).to eq(foo: 5)
end
end
end
end
describe 'writer method' do
context 'when added' do
before { subject.create_method(:foo=) }
it 'responds to it' do
expect(subject).to respond_to(:foo=)
end
context 'when the method already exists' do
let(:error) { described_class::AlreadyExistsError }
let(:message) { 'Method :foo= already exists' }
specify do
expect do
subject.create_method(:foo=)
end.to raise_error(error, message)
end
end
end
describe 'env variable' do
after { expect(ENV[sample_key]).to eq result }
context 'with no block' do
before { subject.create_method(:foo=) }
let(:result) { '123' }
it 'stores a converted to string value' do
subject.foo = 123
end
end
context 'with block' do
before { subject.create_method(:foo=) { |data| YAML.dump(data) } }
let(:result) { "---\n:foo: 5\n" }
it 'stores a marshaled value' do
subject.foo = { foo: 5 }
end
end
end
end
end
context 'with no namespace' do
let(:instance) { described_class.new }
let(:sample_key) { 'FOO' }
include_examples 'accessor methods'
end
context 'with any namespace' do
let(:namespace) { 'bar' }
let(:sample_key) { 'BAR_FOO' }
let(:instance) { described_class.new(namespace) }
include_examples 'accessor methods'
context 'with a method containing underscores' do
before { instance.create_method(:foo_baz) }
it 'reads the correct variable' do
ENV['BAR_FOO_BAZ'] = '123'
expect(subject.foo_baz).to eq '123'
end
end
end
end
|