Description: add specs
Author: HIGUCHI Daisuke (VDR dai) <dai@debian.org>
Origin: upstream
Forwarded: not-needed
Last-Update: 2017-11-27

Index: ruby-nenv/spec/lib/nenv/builder_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv/builder_spec.rb
@@ -0,0 +1,27 @@
+require 'nenv/builder'
+
+RSpec.describe Nenv::Builder do
+  describe '#build' do
+    before do
+      allow(ENV).to receive(:[]).with('FOO')
+    end
+
+    it 'returns a class with the given methods' do
+      FooEnv = Nenv::Builder.build do
+        create_method(:foo?)
+      end
+      FooEnv.new.foo?
+    end
+
+    context 'with duplicate methods' do
+      it 'fails' do
+        expect do
+          FooEnv = Nenv::Builder.build do
+            create_method(:foo?)
+            create_method(:foo?)
+          end
+        end.to raise_error(Nenv::Environment::AlreadyExistsError)
+      end
+    end
+  end
+end
Index: ruby-nenv/spec/lib/nenv/environment/dumper_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv/environment/dumper_spec.rb
@@ -0,0 +1,34 @@
+require 'yaml'
+
+require 'nenv/environment/dumper'
+
+RSpec.describe Nenv::Environment::Dumper do
+  subject { described_class.setup.(value) }
+
+  context "with \"abc\"" do
+    let(:value) { 'abc' }
+    it { is_expected.to eq('abc') }
+  end
+
+  context 'with 123' do
+    let(:value) { 123 }
+    it { is_expected.to eq('123') }
+  end
+
+  context 'with nil' do
+    let(:value) { nil }
+    it { is_expected.to eq(nil) }
+  end
+
+  context 'with a block' do
+    subject do
+      described_class.setup { |data| YAML.dump(data) }.(value)
+    end
+
+    context 'with a yaml string' do
+      let(:value) { { foo: 3 } }
+      let(:yaml) { "---\n:foo: 3\n" }
+      it { is_expected.to eq(yaml) }
+    end
+  end
+end
Index: ruby-nenv/spec/lib/nenv/environment/loader_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv/environment/loader_spec.rb
@@ -0,0 +1,59 @@
+require 'yaml'
+require 'nenv/environment/loader'
+
+RSpec.describe Nenv::Environment::Loader do
+  context 'with no block' do
+    subject { described_class.setup(meth).(value) }
+
+    context 'with a normal method' do
+      let(:meth) { :foo }
+
+      context "with \"abc\"" do
+        let(:value) { 'abc' }
+        it { is_expected.to eq('abc') }
+      end
+    end
+
+    context 'with a bool method' do
+      let(:meth) { :foo? }
+
+      %w(1 true y yes TRUE YES foobar).each do |data|
+        context "with #{data.inspect}" do
+          let(:value) { data }
+          it { is_expected.to eq(true) }
+        end
+      end
+
+      %w(0 false n no FALSE NO).each do |data|
+        context "with #{data.inspect}" do
+          let(:value) { data }
+          it { is_expected.to eq(false) }
+        end
+      end
+
+      context 'with nil' do
+        let(:value) { nil }
+        it { is_expected.to eq(nil) }
+      end
+
+      context 'when empty string' do
+        let(:value) { '' }
+        it do
+          expect { subject }.to raise_error(
+            ArgumentError, /Can't convert empty string into Bool/
+          )
+        end
+      end
+    end
+  end
+
+  context 'with a block' do
+    subject do
+      described_class.setup(:foo) { |data| YAML.load(data) }.(value)
+    end
+    context 'with a yaml string' do
+      let(:value) { "--- foo\n...\n" }
+      it { is_expected.to eq('foo') }
+    end
+  end
+end
Index: ruby-nenv/spec/lib/nenv/environment_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv/environment_spec.rb
@@ -0,0 +1,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
Index: ruby-nenv/spec/lib/nenv_spec.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/lib/nenv_spec.rb
@@ -0,0 +1,84 @@
+require 'nenv'
+
+RSpec.describe Nenv do
+  let(:env) { instance_double(Hash) } # Hash is close enough
+  before { stub_const('ENV', env) }
+
+  describe 'Nenv() helper method' do
+    it 'reads from env' do
+      expect(ENV).to receive(:[]).with('GIT_BROWSER').and_return('chrome')
+      Nenv('git').browser
+    end
+
+    it 'return the value from env' do
+      allow(ENV).to receive(:[]).with('GIT_BROWSER').and_return('firefox')
+      expect(Nenv('git').browser).to eq('firefox')
+    end
+  end
+
+  describe 'Nenv() helper method with block' do
+    it 'reads from env' do
+      expect(ENV).to receive(:[]).with('GIT_BROWSER').and_return('chrome')
+      Nenv('git') do |git|
+        git.browser
+      end
+    end
+
+    it 'return the value from env' do
+      allow(ENV).to receive(:[]).with('GIT_BROWSER').and_return('firefox')
+      result = nil
+      Nenv('git') do |git|
+        result = git.browser
+      end
+      expect(result).to eq('firefox')
+    end
+  end
+
+  describe 'Nenv module' do
+    it 'reads from env' do
+      expect(ENV).to receive(:[]).with('CI').and_return('true')
+      Nenv.ci?
+    end
+
+    it 'return the value from env' do
+      allow(ENV).to receive(:[]).with('CI').and_return('false')
+      expect(Nenv.ci?).to be(false)
+    end
+
+    context 'with no method' do
+      it 'automatically creates the method' do
+        expect(ENV).to receive(:[]).with('FOO').and_return('true')
+        Nenv.foo?
+      end
+    end
+
+    context 'with existing method' do
+      before do
+        Nenv.instance.create_method(:foo?)
+      end
+
+      it 'reads from env' do
+        expect(ENV).to receive(:[]).with('FOO').and_return('true')
+        Nenv.foo?
+      end
+
+      it 'return the value from env' do
+        expect(ENV).to receive(:[]).with('FOO').and_return('true')
+        expect(Nenv.foo?).to be(true)
+      end
+    end
+  end
+
+  # Test added here to properly test if builder is required
+  describe 'Nenv builder' do
+    before do
+      allow(ENV).to receive(:[]).with('FOO').and_return('false')
+    end
+    it 'is required and works' do
+      FooEnv = Nenv::Builder.build do
+        create_method(:foo?)
+      end
+      FooEnv.new.foo?
+    end
+  end
+end
Index: ruby-nenv/spec/spec_helper.rb
===================================================================
--- /dev/null
+++ ruby-nenv/spec/spec_helper.rb
@@ -0,0 +1,49 @@
+require 'coveralls'
+Coveralls.wear!
+
+RSpec.configure do |config|
+  config.expect_with :rspec do |expectations|
+    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
+  end
+
+  config.mock_with :rspec do |mocks|
+    mocks.verify_partial_doubles = true
+  end
+
+  config.filter_run focus: ENV['CI'] != 'true'
+  config.run_all_when_everything_filtered = true
+
+  config.disable_monkey_patching!
+
+  # config.warnings = true
+
+  config.default_formatter = 'doc' if config.files_to_run.one?
+
+  # config.profile_examples = 10
+
+  config.order = :random
+
+  Kernel.srand config.seed
+
+  config.before do
+    allow(ENV).to receive(:[]) do |key|
+      fail "stub me: ENV[#{key.inspect}]"
+    end
+
+    allow(ENV).to receive(:[]=) do |key, value|
+      fail "stub me: ENV[#{key.inspect}] = #{value.inspect}"
+    end
+
+    allow(ENV).to receive(:[]).with('PRYRC').and_call_original
+    allow(ENV).to receive(:[]).with('DISABLE_PRY').and_call_original
+    allow(ENV).to receive(:[]).with('ANSICON').and_call_original
+    allow(ENV).to receive(:[]).with('TERM').and_call_original
+  end
+
+  config.after do
+    begin
+      Nenv.method(:reset).call
+    rescue NameError
+    end
+  end
+end
