Skip to content

Commit

Permalink
Write specs
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherRegularDude committed Dec 14, 2024
1 parent 21128f4 commit 72d2929
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 68 deletions.
11 changes: 9 additions & 2 deletions lib/resol.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# frozen_string_literal: true

require "smart_core/initializer"

require_relative "resol/version"
require_relative "resol/return_engine"
require_relative "resol/configuration"
require_relative "resol/service"

module Resol
extend self

def config
Configuration
end

def configure
yield config
end
end
63 changes: 26 additions & 37 deletions lib/resol/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
# frozen_string_literal: true

module Resol
class Configuration
DEFAULT_RETURN_ENGINE = ReturnEngine::Catch

class << self
def configure
SmartCore::Initializer::Configuration.configure do |c|
self.smartcore_config = c
yield self
self.smartcore_config = nil
end
end

def return_engine
@return_engine || DEFAULT_RETURN_ENGINE
end

def return_engine=(engine)
@return_engine = engine
end

private

attr_accessor :smartcore_config

def method_missing(meth, *, &)
# rubocop:disable Style/SafeNavigation
if smartcore_config && smartcore_config.respond_to?(meth)
# rubocop:enable Style/SafeNavigation
smartcore_config.__send__(meth, *, &)
else
super
end
end

def respond_to_missing?(meth, include_private)
smartcore_config.respond_to?(meth, include_private)
end
module Configuration
extend self

DEFAULTS = { return_engine: Resol::ReturnEngine::Catch }.freeze

DEFAULTS.each_key do |attr_name|
define_method(attr_name) { values[attr_name] }
define_method(:"#{attr_name}=") { |value| values[attr_name] = value }
end

def smart_config
return nil if smart_not_loaded?

SmartCore::Initializer::Configuration.config
end

def to_h = values.dup

private

def smart_not_loaded?
!defined?(SmartCore::Initializer::Configuration)
end

def values
@values ||= DEFAULTS.dup
end
end
end
2 changes: 1 addition & 1 deletion resol.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Gem::Specification.new do |spec|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.include?("spec") }
spec.require_paths = ["lib"]

spec.add_dependency "dry-initializer", "~> 3.1"
spec.add_dependency "dry-initializer", "~> 3.1"
spec.add_dependency "smart_initializer", "~> 0.7"
end
41 changes: 13 additions & 28 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
# frozen_string_literal: true

RSpec.describe Resol::Configuration do
around do |example|
example.call
let(:cfg_values) { described_class.instance_variable_get(:@values) }

described_class.configure do |c|
c.auto_cast = true
c.return_engine = described_class::DEFAULT_RETURN_ENGINE
end
end
it "properly configures" do
expect(described_class.return_engine).to eq(described_class::DEFAULTS[:return_engine])
expect(described_class.smart_config).to eq(SmartCore::Initializer::Configuration.config)

it "delegates configuration" do
described_class.configure do |c|
c.auto_cast = false
c.return_engine = Resol::ReturnEngine::Return
end
described_class.return_engine = "kek"

expect(SmartCore::Initializer::Configuration.config[:auto_cast]).to eq(false)
expect(described_class.return_engine).to eq(Resol::ReturnEngine::Return)
expect(described_class.return_engine).to eq("kek")
expect(described_class.to_h.equal?(cfg_values)).to eq(false)
end

context "when undefined method is called" do
let(:called_block) do
proc do
described_class.configure do |c|
c.not_exist = true
end
end
end

it "raises error" do
expect(&called_block).to raise_error(NoMethodError)
specify do
expect { described_class.kekpek }.to raise_error(NoMethodError)
end
end

context "with undefined method" do
it "respond_to? returns false" do
expect(described_class.respond_to?(:not_exist)).to eq(false)
end
context "when smartcore not loaded" do
before { allow(described_class).to receive(:smart_not_loaded?).and_return(true) }

specify { expect(described_class.smart_config).to eq(nil) }
end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
require "resol"
require "pry"

require "smart_core/initializer"
require "dry/initializer"

class SmartService < Resol::Service
use_initializer! :smartcore
end
Expand All @@ -36,4 +39,10 @@ class SmartService < Resol::Service

config.order = :random
Kernel.srand config.seed

config.around do |ex|
old_settings = Resol::Configuration.to_h
ex.call
Resol::Configuration.instance_variable_set(:@values, old_settings)
end
end

0 comments on commit 72d2929

Please sign in to comment.