Skip to content

Commit

Permalink
Merge pull request #411 from DataDog/refactor/add_configuration_spec
Browse files Browse the repository at this point in the history
Add configuration examples to RSpec
  • Loading branch information
delner committed May 8, 2018
2 parents bdd81b6 + f729961 commit 0b5daa3
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 494 deletions.
114 changes: 114 additions & 0 deletions spec/ddtrace/configurable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Configurable do
shared_examples_for 'a configurable constant' do
describe '#option' do
let(:name) { :foo }
let(:options) { {} }
let(:block) { nil }
before(:each) { configurable.send(:option, name, options, &block) }

context 'given a default option' do
let(:options) { { default: default_value } }
let(:default_value) { :bar }
it { expect(configurable.get_option(name)).to eq(default_value) }
end

context 'given a custom setter' do
let(:name) { :shout }
before(:each) { configurable.set_option(name, 'loud') }

context 'option' do
let(:options) { { setter: ->(v) { v.upcase } } }
it { expect(configurable.get_option(name)).to eq('LOUD') }
end

context 'block' do
let(:block) { Proc.new { |value| "#{value.upcase}!" } }
it { expect(configurable.get_option(name)).to eq('LOUD!') }
end
end
end

describe '#get_option' do
subject(:result) { configurable.get_option(name) }
let(:name) { :foo }
let(:options) { {} }

it { expect(configurable).to respond_to(:get_option) }

context 'when the option doesn\'t exist' do
it { expect { result }.to raise_error(Datadog::InvalidOptionError) }
end
end

describe '#set_option' do
let(:name) { :foo }
let(:options) { {} }
let(:value) { :bar }

before(:each) do
configurable.send(:option, name, options)
configurable.set_option(name, value)
end

it { expect(configurable).to respond_to(:set_option) }

context 'when a default has been defined' do
let(:options) { { default: default_value } }
let(:default_value) { :bar }
let(:value) { 'baz!' }
it { expect(configurable.get_option(name)).to eq(value) }

context 'and the value set is \'false\'' do
let(:default_value) { true }
let(:value) { false }
it { expect(configurable.get_option(name)).to eq(value) }
end
end

context 'when the option doesn\'t exist' do
subject(:result) { configurable.set_option(:bad_option, value) }
it { expect { result }.to raise_error(Datadog::InvalidOptionError) }
end
end

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

before(:each) do
configurable.send(:option, :x, default: 1)
configurable.send(:option, :y, default: 2)
configurable.set_option(:y, 100)
end

it { is_expected.to eq({ x: 1, y: 100 }) }
end

describe '#sorted_options' do
subject(:sorted_options) { configurable.sorted_options }

before(:each) do
configurable.send(:option, :foo, depends_on: [:bar])
configurable.send(:option, :bar, depends_on: [:baz])
configurable.send(:option, :baz)
end

it { is_expected.to eq([:baz, :bar, :foo]) }
end
end

describe 'implemented' do
describe 'class' do
subject(:configurable) { Class.new { include(Datadog::Configurable) } }
it_behaves_like 'a configurable constant'
end

describe 'module' do
subject(:configurable) { Module.new { include(Datadog::Configurable) } }
it_behaves_like 'a configurable constant'
end
end
end
56 changes: 56 additions & 0 deletions spec/ddtrace/configuration/pin_setup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Configuration::PinSetup do
let(:target) { Object.new }

before(:each) do
Datadog::Pin.new('original-service', app: 'original-app').onto(target)
end

describe '#call' do
before(:each) { described_class.new(target, options).call }

context 'given options' do
let(:options) do
{
service_name: 'my-service',
app: 'my-app',
app_type: :cache,
tracer: tracer,
tags: { env: :prod },
distributed_tracing: true
}
end

let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) }

it do
expect(target.datadog_pin.service).to eq('my-service')
expect(target.datadog_pin.app).to eq('my-app')
expect(target.datadog_pin.tags).to eq({ env: :prod })
expect(target.datadog_pin.config).to eq({ distributed_tracing: true })
expect(target.datadog_pin.tracer).to eq(tracer)
end
end

context 'missing options' do
let(:options) { { app: 'custom-app' } }

it do
expect(target.datadog_pin.app).to eq('custom-app')
expect(target.datadog_pin.service).to eq('original-service')
end
end
end

describe 'Datadog#configure' do
before(:each) { Datadog.configure(target, service_name: :foo, extra: :bar) }

it do
expect(target.datadog_pin.service).to eq(:foo)
expect(target.datadog_pin.config).to eq({ extra: :bar })
end
end
end
43 changes: 43 additions & 0 deletions spec/ddtrace/configuration/proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Configuration::Proxy do
subject(:proxy) { described_class.new(configurable_module) }

let(:configurable_module) do
Module.new do
include Datadog::Configurable
option :x, default: :a
option :y, default: :b
end
end

describe '#[]' do
before(:each) do
proxy[:x] = 1
proxy[:y] = 2
end

it do
expect(proxy[:x]).to eq(1)
expect(proxy[:y]).to eq(2)
end
end

describe '#to_h' do
subject(:hash) { proxy.to_h }
it { is_expected.to eq({ x: :a, y: :b }) }
end

describe '#to_hash' do
subject(:hash) { proxy.to_hash }
it { is_expected.to eq({ x: :a, y: :b }) }
end

describe '#merge' do
subject(:result) { proxy.merge(hash) }
let(:hash) { { z: :c } }
it { is_expected.to eq({ x: :a, y: :b, z: :c }) }
end
end
21 changes: 21 additions & 0 deletions spec/ddtrace/configuration/resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

require 'ddtrace'

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

describe '#call' do
subject(:order) { resolver.call }

context 'given a set of dependencies' do
let(:graph) { { 1 => [2], 2 => [3, 4], 3 => [], 4 => [3], 5 => [1] } }
it { expect(order).to eq([3, 4, 2, 1, 5]) }
end

context 'given cyclic dependencies' do
let(:graph) { { 1 => [2], 2 => [1] } }
it { expect { order }.to raise_error(TSort::Cyclic) }
end
end
end
Loading

0 comments on commit 0b5daa3

Please sign in to comment.