Skip to content

Commit

Permalink
Merge pull request #3 from rezwyi/rspec_3
Browse files Browse the repository at this point in the history
Migrate test specs to RSpec3
  • Loading branch information
rezwyi committed Jan 19, 2018
2 parents 3039e54 + 88061c1 commit 57cbe53
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 67 deletions.
23 changes: 14 additions & 9 deletions Gemfile.lock
Expand Up @@ -19,14 +19,19 @@ GEM
hashie (3.5.7)
json (2.1.0)
rake (10.5.0)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.8)
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.6)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.0)
semantic (1.6.0)
simplecov (0.14.1)
docile (~> 1.1.0)
Expand All @@ -46,7 +51,7 @@ DEPENDENCIES
coveralls
elasticonf!
rake (~> 10.0)
rspec (~> 2.14.0)
rspec (~> 3.0)
yard (~> 0.8)

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion elasticonf.gemspec
Expand Up @@ -23,6 +23,6 @@ Gem::Specification.new do |s|
s.add_dependency 'semantic', '> 0'

s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'rspec', '~> 2.14.0'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'yard', '~> 0.8'
end
13 changes: 9 additions & 4 deletions lib/elasticonf.rb
Expand Up @@ -6,6 +6,9 @@
require 'elasticonf/loader'

module Elasticonf
class Error < StandardError; end
class LoadError < Error; end

module_function

def root
Expand All @@ -25,7 +28,7 @@ def load!(env = nil)
config.env = env if env

unless File.exists?(config_file)
raise "Config file #{config_file} not found. Cannot continue"
raise LoadError, "Config file #{config_file} not found. Cannot continue"
end

loader = Loader[YAML.load_file(config_file)]
Expand All @@ -41,9 +44,11 @@ def load!(env = nil)
end

if Kernel.const_defined?(config.const_name)
config.raise_if_already_initialized_constant ?
raise("Cannot set constant #{config.const_name} because it is already initialized") :
Kernel.send(:remove_const, config.const_name)
if config.raise_if_already_initialized_constant
raise LoadError, "Cannot set constant #{config.const_name} because it is already initialized"
else
Kernel.send :remove_const, config.const_name
end
end

Kernel.const_set config.const_name, loader
Expand Down
77 changes: 44 additions & 33 deletions spec/lib/elasticonf/config_spec.rb
Expand Up @@ -12,121 +12,132 @@
subject.reset_config!
end

its(:env) { should eql('development') }
its(:config_file) { should eql('settings') }
its(:const_name) { should eql('Settings') }
its(:raise_if_already_initialized_constant) { should be_true }
it 'should return default values' do
expect(subject.env).to eql('development')
expect(subject.config_file).to eql('settings')
expect(subject.const_name).to eql('Settings')
expect(subject.const_name).to eql('Settings')
expect(subject.raise_if_already_initialized_constant).to be_truthy
end

it 'should raise an error' do
expect { subject.config_root }.to raise_error
expect { subject.config_root }.to raise_error(ArgumentError)
end
end

describe '#env' do
its(:env) { should eql('development') }
it 'should return default value' do
expect(subject.env).to eql('development')
end

it 'should return some value' do
it 'should set new env' do
expect { subject.env = :test }.to change(subject, :env).to('test')
end

context 'when wrong argument given' do
it 'should raise an error' do
expect { subject.env = -> {} }.to raise_error
expect { subject.env = -> {} }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.env = {} }.to raise_error
expect { subject.env = {} }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.env = [] }.to raise_error
expect { subject.env = [] }.to raise_error(ArgumentError)
end
end
end

describe '#config_root' do
it 'should raise an error' do
expect { subject.config_root }.to raise_error
expect { subject.config_root }.to raise_error(ArgumentError)
end

it 'should return some value' do
it 'should set new config_root value' do
subject.config_root = '/config'
expect(subject.config_root).to eql(Pathname.new('/config'))
end

context 'when wrong argument given' do
it 'should raise an error' do
expect { subject.config_root = -> {} }.to raise_error
expect { subject.config_root = -> {} }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.config_root = {} }.to raise_error
expect { subject.config_root = {} }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.config_root = [] }.to raise_error
expect { subject.config_root = [] }.to raise_error(ArgumentError)
end
end
end

describe '#config_file' do
its(:config_file) { should eql('settings') }
it 'should return default value' do
expect(subject.config_file).to eql('settings')
end

it 'should return some value' do
it 'should set new config_file value' do
expect {
subject.config_file = 'application'
}.to change(subject, :config_file).to('application')
end

context 'when wrong argument given' do
it 'should raise an error' do
expect { subject.config_file = -> {} }.to raise_error
expect { subject.config_file = -> {} }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.config_file = {} }.to raise_error
expect { subject.config_file = {} }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.config_file = [] }.to raise_error
expect { subject.config_file = [] }.to raise_error(ArgumentError)
end
end
end

describe '#const_name' do
its(:const_name) { should eql('Settings') }
it 'should return default value' do
expect(subject.const_name).to eql('Settings')
end

it 'should return some value' do
it 'should set new const_name value' do
expect {
subject.const_name = 'AppSettings'
}.to change(subject, :const_name).to('AppSettings')
end

it 'should return some value' do
it 'should set new const_name value' do
expect {
subject.const_name = :'AppSettings'
}.to change(subject, :const_name).to('AppSettings')
end

context 'when wrong argument given' do
it 'should raise an error' do
expect { subject.const_name = -> {} }.to raise_error
expect { subject.const_name = -> {} }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.const_name = {} }.to raise_error
expect { subject.const_name = {} }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.const_name = [] }.to raise_error
expect { subject.const_name = [] }.to raise_error(ArgumentError)
end
end
end

describe '#raise_if_already_initialized_constant' do
its(:raise_if_already_initialized_constant) { should be_true }
it 'should return default value' do
expect(subject.raise_if_already_initialized_constant).to be_truthy
end

it 'should change to false' do
it 'should change raise_if_already_initialized_constant to false' do
expect {
subject.raise_if_already_initialized_constant = false
}.to change(subject, :raise_if_already_initialized_constant).to(false)
Expand All @@ -136,31 +147,31 @@
it 'should raise an error' do
expect {
subject.raise_if_already_initialized_constant = 'some_string'
}.to raise_error
}.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect {
subject.raise_if_already_initialized_constant = :some_symbol
}.to raise_error
}.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect {
subject.raise_if_already_initialized_constant = -> {}
}.to raise_error
}.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect {
subject.raise_if_already_initialized_constant = {}
}.to raise_error
}.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect {
subject.raise_if_already_initialized_constant = []
}.to raise_error
}.to raise_error(ArgumentError)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/elasticonf/loader_spec.rb
Expand Up @@ -7,7 +7,7 @@
end

it 'should be inherited from Hashie::Mash' do
subject.should be_kind_of(Hashie::Mash)
expect(subject).to be_kind_of(Hashie::Mash)
end

describe '#get' do
Expand All @@ -25,19 +25,19 @@

context 'when wrong argument given' do
it 'should raise an error' do
expect { subject.get }.to raise_error
expect { subject.get }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.get({}) }.to raise_error
expect { subject.get({}) }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.get([]) }.to raise_error
expect { subject.get([]) }.to raise_error(ArgumentError)
end

it 'should raise an error' do
expect { subject.get(false) }.to raise_error
expect { subject.get(false) }.to raise_error(ArgumentError)
end
end
end
Expand Down

0 comments on commit 57cbe53

Please sign in to comment.