Skip to content

Commit

Permalink
test(core): add CastMiddleware specs
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Oct 30, 2022
1 parent cf4285c commit 897b9ad
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ class << self
def cast(other)
Commands::CastMiddleware.call(other: other)
end

##
# @return [Boolean]
#
# @internal
# NOTE: `@param other [Module, ConvenientService::Support::Concern, ConvenientService::Core::Entities::Concerns::Entities::Middleware]`
# TODO: How to document `alias_method`?
#
# https://ruby-doc.org/core-2.7.0/Module.html#method-i-3C
#
alias_method :original_two_equals, :==

##
# @param other [Module, ConvenientService::Support::Concern, ConvenientService::Core::Entities::Concerns::Entities::Middleware]
# @return [Boolean]
#
# @internal
# https://ruby-doc.org/core-2.7.0/Module.html#method-i-3C
#
def ==(other)
return true if Entities::Middleware.original_two_equals(other)

return unless other.instance_of?(Class)
return unless other < Entities::Middleware

concern == other.concern
end
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module Commands
class CastMiddleware < Support::Command
##
# @!attribute [r] other
# @return [ConvenientService::Support::Concern, Module]
# @return [Module, ConvenientService::Support::Concern]
#
attr_reader :other

##
# @param other [ConvenientService::Support::Concern, Module]
# @param other [Module, ConvenientService::Support::Concern, ConvenientService::Core::Entities::Concerns::Entities::Middleware]
# @return [void]
#
def initialize(other:)
Expand All @@ -26,8 +26,9 @@ def initialize(other:)
# @return [ConvenientService::Core::Entities::Concerns::Entities::Middleware, nil]
#
def call
case other
when ::Module
if other.instance_of?(Class) && other < Entities::Middleware
cast_middleware(other)
elsif other.instance_of?(Module)
cast_module(other)
end
end
Expand All @@ -43,19 +44,18 @@ def call
# IMPORTANT: Must be kept in sync with `def concern` in `ConvenientService::Core::Entities::Concerns::Middleware`.
#
def cast_module(mod)
::Class.new(Entities::Middleware).tap do |klass|
klass.class_exec(mod) do |mod|
##
# @return [ConvenientService::Support::Concern, Module]
#
define_singleton_method(:concern) { mod }
middleware = ::Class.new(Entities::Middleware)

##
# @return [Boolean]
#
define_singleton_method(:==) { |other| concern == other.concern if other.instance_of?(self.class) }
end
end
##
# @return [Module, ConvenientService::Support::Concern]
#
middleware.define_singleton_method(:concern) { mod }

middleware
end

def cast_middleware(middleware)
middleware.dup
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Core::Entities::Concerns::Entities::Middleware::Commands::CastMiddleware do
example_group "class methhods" do
describe ".call" do
let(:casted) { described_class.call(other: other) }

context "when `other` is NOT castable" do
let(:other) { 42 }

it "returns `nil`" do
expect(casted).to be_nil
end
end

context "when `other` is Class" do
context "when Class is NOT `ConvenientService::Core::Entities::Concerns::Entities::Middleware` descendant" do
let(:other) { Class.new }

it "returns `nil`" do
expect(casted).to be_nil
end
end

context "when Class is `ConvenientService::Core::Entities::Concerns::Entities::Middleware` descendant" do
let(:other) do
::Class.new(ConvenientService::Core::Entities::Concerns::Entities::Middleware).tap do |klass|
klass.class_exec(Module.new) do |mod|
define_singleton_method(:concern) { mod }
end
end
end

it "returns middleware copy" do
expect(casted).to eq(other.dup)
end
end
end

context "when `other` is Module" do
let(:other) { Module.new }

let(:middleware) do
::Class.new(ConvenientService::Core::Entities::Concerns::Entities::Middleware).tap do |klass|
klass.class_exec(other) do |mod|
define_singleton_method(:concern) { mod }
end
end
end

it "returns other casted to middleware" do
expect(casted).to eq(middleware)
end
end
end
end
end
# rubocop:enable RSpec/NestedGroups

0 comments on commit 897b9ad

Please sign in to comment.