Skip to content

Commit

Permalink
[InstallationOptions] Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Dec 30, 2015
1 parent f460047 commit fedede8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/cocoapods/installer/installation_options.rb
Expand Up @@ -19,7 +19,7 @@ class InstallationOptions
def self.from_podfile(podfile)
name, options = podfile.installation_method
unless name.downcase == 'cocoapods'
raise Informative "Currently need to specify a `cocoapods` install, you chose `#{name}`."
raise Informative, "Currently need to specify a `cocoapods` install, you chose `#{name}`."
end
new(options)
end
Expand Down Expand Up @@ -68,10 +68,10 @@ def self.all_options
#
# @raise [Informative] if `options` contains any unknown keys.
#
def initialize(options)
def initialize(options = {})
options = ActiveSupport::HashWithIndifferentAccess.new(options)
unknown_keys = options.keys - self.class.all_options.map(&:to_s)
raise Informative, "Unknown installation options: #{unknown_keys.to_sentence}" unless unknown_keys.empty?
raise Informative, "Unknown installation options: #{unknown_keys.to_sentence}." unless unknown_keys.empty?
self.class.defaults.each do |key, default|
value = options.fetch(key, default)
send("#{key}=", value)
Expand All @@ -91,6 +91,16 @@ def to_h(include_defaults: true)
end
end

def ==(other)
other.is_a?(self.class) && to_h == other.to_h
end

alias_method :eql, :==

def hash
to_h.hash
end

option :clean, true
option :deduplicate_targets, true
option :deterministic_uuids, true
Expand Down
76 changes: 76 additions & 0 deletions spec/unit/installer/installation_options_spec.rb
@@ -0,0 +1,76 @@
require File.expand_path('../../../spec_helper', __FILE__)

module Pod
describe Installer::InstallationOptions do
describe 'registered options' do
{
'clean' => true,
'deduplicate_targets' => true,
'deterministic_uuids' => true,
'integrate_targets' => true,
'lock_pod_sources' => true,
}.each do |option, default|
it "includes `#{option}` defaulting to `#{default}`" do
Installer::InstallationOptions.defaults.fetch(option).should == default
Installer::InstallationOptions.new.send(option).should == default
end
end
end

describe '.from_podfile' do
it 'raises for a non-cocoapods install' do
podfile = Podfile.new { install! 'foo', :key => 'value' }
exception = should.raise(Informative) { Installer::InstallationOptions.from_podfile(podfile) }
exception.message.should.include 'Currently need to specify a `cocoapods` install, you chose `foo`.'
end

it 'parses the name in a case-insensitive manner' do
podfile = Podfile.new { install! 'CoCoApOdS' }
should.not.raise(Informative) { Installer::InstallationOptions.from_podfile(podfile) }
end

it 'uses the installation method options to create the options' do
options = { :integrate_targets => false }
podfile = Podfile.new { install! 'cocoapods', options }
installation_options = Installer::InstallationOptions.from_podfile(podfile)
installation_options.should == Installer::InstallationOptions.new(options)
end
end

describe '#initialize' do
it 'uses all defaults when no options are specified' do
Installer::InstallationOptions.new.to_h(:include_defaults => false).should.be.empty
end

it 'sets the values as specified in the options' do
installation_options = Installer::InstallationOptions.new(:deterministic_uuids => false)
installation_options.deterministic_uuids.should.be.false
end

it 'raises when unknown keys are encountered' do
exception = should.raise(Informative) { Installer::InstallationOptions.new(:a => 'a', :b => 'b', :c => 'c') }
exception.message.should.include 'Unknown installation options: a, b, and c.'
end
end

describe '#to_h' do
it 'includes all options by default' do
installation_options = Installer::InstallationOptions.new(:deterministic_uuids => false)
installation_options.to_h.should == {
'clean' => true,
'deduplicate_targets' => true,
'deterministic_uuids' => false,
'integrate_targets' => true,
'lock_pod_sources' => true,
}
end

it 'removes default values when specified' do
installation_options = Installer::InstallationOptions.new(:deterministic_uuids => false)
installation_options.to_h(:include_defaults => false).should == {
'deterministic_uuids' => false,
}
end
end
end
end

0 comments on commit fedede8

Please sign in to comment.