Skip to content

Commit

Permalink
feat(has_negated_result): introduce #negated_result
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Jan 20, 2024
1 parent 25bb3a4 commit 6890cef
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/convenient_service/service/configs/minimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module Minimal
use ConvenientService::Plugins::Common::HasConstructorWithoutInitialize::Concern

use ConvenientService::Plugins::Service::HasResult::Concern
use ConvenientService::Plugins::Service::HasNegatedResult::Concern
use ConvenientService::Plugins::Service::HasJSendResult::Concern
use ConvenientService::Plugins::Service::CanHaveSteps::Concern
end
Expand Down
2 changes: 2 additions & 0 deletions lib/convenient_service/service/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# NOTE: Order matters.
#
require_relative "plugins/has_result"
require_relative "plugins/has_negated_result"

require_relative "plugins/has_j_send_result"

require_relative "plugins/can_recalculate_result"
Expand Down
4 changes: 4 additions & 0 deletions lib/convenient_service/service/plugins/has_negated_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

require_relative "has_negated_result/concern"
require_relative "has_negated_result/exceptions"
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module HasNegatedResult
module Concern
include Support::Concern

class_methods do
##
# @api public
# @raise [ConvenientService::Service::Plugins::HasNegatedResult::Exceptions::NegatedResultIsNotOverridden]
#
def negated_result(...)
new(...).negated_result
end
end

instance_methods do
##
# @api public
# @raise [ConvenientService::Service::Plugins::HasNegatedResult::Exceptions::NegatedResultIsNotOverridden]
#
def negated_result
::ConvenientService.raise Exceptions::NegatedResultIsNotOverridden.new(service: self)
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module HasNegatedResult
module Exceptions
class NegatedResultIsNotOverridden < ::ConvenientService::Exception
def initialize_with_kwargs(service:)
message = <<~TEXT
Negated result method (#negated_result) of `#{service.class}` is NOT overridden.
TEXT

initialize(message)
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Plugins::Common::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Concern,
ConvenientService::Service::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Concern,
ConvenientService::Service::Plugins::CanHaveSteps::Concern,
ConvenientService::Common::Plugins::CachesConstructorArguments::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Plugins::Common::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Concern,
ConvenientService::Service::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Concern,
ConvenientService::Service::Plugins::CanHaveSteps::Concern,
ConvenientService::Common::Plugins::CachesConstructorArguments::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Plugins::Common::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Concern,
ConvenientService::Service::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Concern,
ConvenientService::Service::Plugins::CanHaveSteps::Concern,
ConvenientService::Common::Plugins::CachesConstructorArguments::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Plugins::Common::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Concern,
ConvenientService::Service::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Concern,
ConvenientService::Service::Plugins::CanHaveSteps::Concern,
ConvenientService::Common::Plugins::CachesConstructorArguments::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
# https://github.com/marian13/convenient_service/discussions/43
#
it "applies its `included` block only once" do
expect(service_class.concerns.to_a.size).to eq(8)
expect(service_class.concerns.to_a.size).to eq(9)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Plugins::Common::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Concern,
ConvenientService::Service::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Concern,
ConvenientService::Service::Plugins::CanHaveSteps::Concern
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Plugins::Common::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Concern,
ConvenientService::Service::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Concern,
ConvenientService::Service::Plugins::CanHaveSteps::Concern,
ConvenientService::Common::Plugins::CachesConstructorArguments::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Plugins::Common::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Concern,
ConvenientService::Service::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Concern,
ConvenientService::Service::Plugins::CanHaveSteps::Concern,
ConvenientService::Common::Plugins::CachesConstructorArguments::Concern,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

# rubocop:disable RSpec/MultipleMemoizedHelpers
RSpec.describe ConvenientService::Service::Plugins::HasNegatedResult::Concern do
include ConvenientService::RSpec::Helpers::IgnoringException

include ConvenientService::RSpec::Matchers::DelegateTo

let(:service_class) do
Class.new do
include ConvenientService::Service::Configs::Minimal
end
end

let(:service_instance) { service_class.new }
let(:negated_result) { service_instance.negated_result }

let(:args) { [:foo] }
let(:kwargs) { {foo: :bar} }
let(:block) { proc { :foo } }

example_group "modules" do
include ConvenientService::RSpec::Matchers::IncludeModule
include ConvenientService::RSpec::PrimitiveMatchers::ExtendModule

subject { described_class }

it { is_expected.to include_module(ConvenientService::Support::Concern) }

context "when included" do
subject { service_class }

let(:service_class) do
Class.new.tap do |klass|
klass.class_exec(described_class) do |mod|
include mod
end
end
end

it { is_expected.to include_module(described_class::InstanceMethods) }
it { is_expected.to extend_module(described_class::ClassMethods) }
end
end

example_group "instance methods" do
describe "#negated_result" do
let(:exception_message) do
<<~TEXT
Negated result method (#negated_result) of `#{service_class}` is NOT overridden.
TEXT
end

it "raises `ConvenientService::Service::Plugins::HasNegatedResult::Exceptions::NegatedResultIsNotOverridden`" do
expect { negated_result }
.to raise_error(ConvenientService::Service::Plugins::HasNegatedResult::Exceptions::NegatedResultIsNotOverridden)
.with_message(exception_message)
end

specify do
expect { ignoring_exception(ConvenientService::Service::Plugins::HasNegatedResult::Exceptions::NegatedResultIsNotOverridden) { negated_result } }
.to delegate_to(ConvenientService, :raise)
end
end
end

example_group "class methods" do
include ConvenientService::RSpec::Helpers::IgnoringException
include ConvenientService::RSpec::Matchers::DelegateTo

let(:service_class) do
Class.new do
include ConvenientService::Service::Configs::Minimal

def negated_result
failure
end
end
end

describe ".negated_result" do
specify do
expect { service_class.negated_result(*args, **kwargs, &block) }
.to delegate_to(service_class, :new)
.with_arguments(*args, **kwargs, &block)
end

specify do
allow(service_class).to receive(:new).and_return(service_instance)

expect { service_class.negated_result(*args, **kwargs, &block) }
.to delegate_to(service_instance, :negated_result)
.and_return_its_value
end
end
end
end
# rubocop:enable RSpec/MultipleMemoizedHelpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

RSpec.describe ConvenientService::Service::Plugins::HasNegatedResult::Exceptions do
include ConvenientService::RSpec::PrimitiveMatchers::BeDescendantOf

specify { expect(described_class::NegatedResultIsNotOverridden).to be_descendant_of(ConvenientService::Exception) }
end

0 comments on commit 6890cef

Please sign in to comment.