Skip to content

Commit

Permalink
Added: Contrib::Configuration specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jun 25, 2018
1 parent bf7b51d commit ea75490
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 0 deletions.
58 changes: 58 additions & 0 deletions spec/ddtrace/contrib/configuration/option_definition_set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Configuration::OptionDefinitionSet do
subject(:set) { described_class.new }

it { is_expected.to be_a_kind_of(Hash) }

shared_context 'dependent option set' do
before(:each) do
set[:foo] = instance_double(
Datadog::Contrib::Configuration::OptionDefinition,
depends_on: [:bar]
)

set[:bar] = instance_double(
Datadog::Contrib::Configuration::OptionDefinition,
depends_on: [:baz]
)

set[:baz] = instance_double(
Datadog::Contrib::Configuration::OptionDefinition,
depends_on: []
)
end
end

describe '#dependency_order' do
subject(:dependency_order) { set.dependency_order }

context 'when invoked' do
let(:resolver) { instance_double(Datadog::Configuration::Resolver) }

it do
expect(Datadog::Configuration::Resolver).to receive(:new)
.with(a_kind_of(Hash))
.and_return(resolver)
expect(resolver).to receive(:call)
dependency_order
end
end

context 'when given some options' do
include_context 'dependent option set'
it { is_expected.to eq([:baz, :bar, :foo]) }
end
end

describe '#dependency_graph' do
subject(:dependency_graph) { set.dependency_graph }

context 'when set contains options' do
include_context 'dependent option set'
it { is_expected.to eq(foo: [:bar], bar: [:baz], baz: []) }
end
end
end
104 changes: 104 additions & 0 deletions spec/ddtrace/contrib/configuration/option_definition_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Configuration::OptionDefinition do
subject(:definition) { described_class.new(name, meta, &block) }

let(:name) { :enabled }
let(:meta) { {} }
let(:block) { nil }

describe '#default' do
subject(:default) { definition.default }

context 'when not initialized with a value' do
it { is_expected.to be nil }
end

context 'when initialized with a value' do
let(:meta) { { default: default_value } }
let(:default_value) { double('default') }
it { is_expected.to be default_value }
end
end

describe '#depends_on' do
subject(:default) { definition.depends_on }

context 'when not initialized with a value' do
it { is_expected.to eq([]) }
end

context 'when initialized with a value' do
let(:meta) { { depends_on: depends_on_value } }
let(:depends_on_value) { double('depends_on') }
it { is_expected.to be depends_on_value }
end
end

describe '#lazy' do
subject(:lazy) { definition.lazy }

context 'when not initialized with a value' do
it { is_expected.to be false }
end

context 'when initialized with a value' do
let(:meta) { { lazy: lazy_value } }
let(:lazy_value) { double('lazy') }
it { is_expected.to be lazy_value }
end
end

describe '#name' do
subject(:result) { definition.name }

context 'when given a String' do
let(:name) { 'enabled' }
it { is_expected.to be name.to_sym }
end

context 'when given a Symbol' do
let(:name) { :enabled }
it { is_expected.to be name }
end
end

describe '#setter' do
subject(:setter) { definition.setter }

context 'when given a value' do
let(:meta) { { setter: setter_value } }
let(:setter_value) { double('setter') }
it { is_expected.to be setter_value }
end

context 'when initialized with a block' do
let(:block) { proc {} }
it { is_expected.to be block }
end

context 'when not initialized' do
it { is_expected.to be described_class::IDENTITY }
end
end

describe '#default_value' do
subject(:result) { definition.default_value }
let(:meta) { { default: default } }
let(:default) { double('default') }

context 'when lazy is true' do
let(:meta) { super().merge(lazy: true) }
let(:default_value) { double('default_value') }
before(:each) { expect(default).to receive(:call).and_return(default_value) }
it { is_expected.to be default_value }
end

context 'when lazy is false' do
let(:meta) { super().merge(lazy: false) }
it { is_expected.to be default }
end
end
end
9 changes: 9 additions & 0 deletions spec/ddtrace/contrib/configuration/option_set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Configuration::OptionSet do
subject(:set) { described_class.new }

it { is_expected.to be_a_kind_of(Hash) }
end
68 changes: 68 additions & 0 deletions spec/ddtrace/contrib/configuration/option_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Configuration::Option do
subject(:option) { described_class.new(definition, context) }
let(:definition) do
instance_double(
Datadog::Contrib::Configuration::OptionDefinition,
default_value: default_value,
setter: setter
)
end
let(:default_value) { double('default value') }
let(:setter) { proc { setter_value } }
let(:setter_value) { double('setter_value') }
let(:context) { double('configuration object') }

describe '#initialize' do
it { expect(option.definition).to be(definition) }
end

describe '#set' do
subject(:set) { option.set(value) }
let(:value) { double('value') }

before(:each) { expect(context).to receive(:instance_exec).with(value, &setter) }

it { is_expected.to be(setter_value) }
end

describe '#get' do
subject(:get) { option.get }

context 'when #set' do
context 'hasn\'t been called' do
it { is_expected.to be(default_value) }
end

context 'has been called' do
let(:value) { double('value') }

before(:each) do
allow(context).to receive(:instance_exec).with(value, &setter)
option.set(value)
end

it { is_expected.to be(setter_value) }
end
end
end

describe '#reset' do
subject(:reset) { option.reset }

context 'when a value has been set' do
let(:value) { double('value') }

before(:each) do
allow(context).to receive(:instance_exec).with(value, &setter)
allow(context).to receive(:instance_exec).with(default_value, &setter).and_return(default_value)
option.set(value)
end

it { is_expected.to be(default_value) }
end
end
end
139 changes: 139 additions & 0 deletions spec/ddtrace/contrib/configuration/options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Configuration::Options do
describe 'implemented' do
subject(:options_class) do
Class.new.tap do |klass|
klass.send(:include, described_class)
end
end

describe 'class behavior' do
describe '#options' do
subject(:options) { options_class.options }

context 'for a class directly implementing Options' do
it { is_expected.to be_a_kind_of(Datadog::Contrib::Configuration::OptionDefinitionSet) }
end

context 'on class inheriting from a class implementing Options' do
let(:parent_class) do
Class.new.tap do |klass|
klass.send(:include, described_class)
end
end
let(:options_class) { Class.new(parent_class) }

context 'which defines some options' do
before(:each) { parent_class.send(:option, :foo) }

it { is_expected.to be_a_kind_of(Datadog::Contrib::Configuration::OptionDefinitionSet) }
it { is_expected.to_not be(parent_class.options) }
it { is_expected.to include(:foo) }
end
end
end

describe '#option' do
subject(:option) { options_class.send(:option, name, meta, &block) }

let(:name) { :foo }
let(:meta) { {} }
let(:block) { proc {} }

it 'creates an option definition' do
is_expected.to be_a_kind_of(Datadog::Contrib::Configuration::OptionDefinition)
expect(options_class.options).to include(name)
expect(options_class.new).to respond_to(name)
expect(options_class.new).to respond_to("#{name}=")
end
end
end

describe 'instance behavior' do
subject(:options_object) { options_class.new }

describe '#options' do
subject(:options) { options_object.options }
it { is_expected.to be_a_kind_of(Datadog::Contrib::Configuration::OptionSet) }
end

describe '#set_option' do
subject(:set_option) { options_object.set_option(name, value) }
let(:name) { :foo }
let(:value) { double('value') }

context 'when the option is defined' do
before(:each) { options_class.send(:option, name) }
it { expect { set_option }.to change { options_object.send(name) }.from(nil).to(value) }
end

context 'when the option is not defined' do
it { expect { set_option }.to raise_error(described_class::InvalidOptionError) }
end
end

describe '#get_option' do
subject(:get_option) { options_object.get_option(name) }
let(:name) { :foo }

context 'when the option is defined' do
before(:each) { options_class.send(:option, name, meta) }
let(:meta) { {} }

context 'and a value is set' do
let(:value) { double('value') }
before(:each) { options_object.set_option(name, value) }
it { is_expected.to be(value) }
end

context 'and a value is not set' do
let(:meta) { super().merge(default: default_value) }
let(:default_value) { double('default_value') }
it { is_expected.to be(default_value) }
end
end

context 'when the option is not defined' do
it { expect { get_option }.to raise_error(described_class::InvalidOptionError) }
end
end

describe '#to_h' do
subject(:hash) { options_object.to_h }

context 'when no options are defined' do
it { is_expected.to eq({}) }
end

context 'when options are set' do
before(:each) do
options_class.send(:option, :foo)
options_object.set_option(:foo, :bar)
end

it { is_expected.to eq(foo: :bar) }
end
end

describe '#reset_options!' do
subject(:reset_options) { options_object.reset_options! }

context 'when an option is defined' do
let(:option) { options_object.options[:foo] }

before(:each) do
options_class.send(:option, :foo, default: :bar)
options_object.set_option(:foo, :baz)
end

it 'resets the option to its default value' do
expect { reset_options }.to change { options_object.get_option(:foo) }.from(:baz).to(:bar)
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions spec/ddtrace/contrib/configuration/resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Configuration::Resolver do
subject(:resolver) { described_class.new }

describe '#resolve' do
subject(:resolve) { resolver.resolve(name) }
let(:name) { double('name') }
it { is_expected.to be name }
end
end
Loading

0 comments on commit ea75490

Please sign in to comment.