Skip to content

Commit

Permalink
test(core): add MethodMiddlewares::Commands::CastCaller specs
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Oct 18, 2022
1 parent 3ada9a8 commit f2e315e
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,34 @@ def call
case other
when ::Hash
cast_hash(other)
when Entities::Callers::Base
cast_caller(other)
end
end

private

##
# @return [ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base]
# @return [ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base, nil]
#
def cast_hash(hash)
return unless hash[:scope]
return unless hash[:entity]

case hash[:scope]
when :instance
Entities::Callers::Instance.new(entity: hash[:entity])
when :class
Entities::Callers::Class.new(entity: hash[:entity])
end
end

##
# @return [ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base, nil]
#
def cast_caller(caller)
caller.copy
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Entities
class Callers
class Base
include Support::AbstractMethod
include Support::Copyable

##
# @!attribute [r] entity
Expand Down Expand Up @@ -105,6 +106,25 @@ def resolve_super_method(method_name)

method.bind(entity)
end

##
# @param other [ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base, Object]
# @return [Boolean]
#
def ==(other)
return unless other.instance_of?(self.class)

return false if entity != other.entity

true
end

##
# @return [Hash]
#
def to_kwargs
{entity: entity}
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

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

let(:entity) { double }

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 hash" do
context "when `other` does NOT have `:entity` key" do
let(:other) { {} }

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

context "when `other` has `:entity` key" do
context "when `other` does NOT have `:scope` key" do
let(:other) { {entity: entity} }

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

context "when `other` has `:scope` key" do
context "when value by `:scope` key is NOT castable" do
let(:other) { {entity: entity, scope: 42} }

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

context "when value by `:scope` key is `:instance`" do
let(:other) { {entity: entity, scope: :instance} }

it "returns hash casted to caller" do
expect(casted).to eq(ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Instance.new(entity: entity))
end
end

context "when value by `:scope` key is `:class`" do
let(:other) { {entity: entity, scope: :class} }

it "returns hash casted to caller" do
expect(casted).to eq(ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Class.new(entity: entity))
end
end
end
end
end

context "when `other` is caller" do
let(:other) { described_class.call(other: {entity: entity, scope: :instance}) }

it "returns `caller` copy" do
expect(casted).to eq(other.copy)
end
end
end
end
end
# rubocop:enable RSpec/NestedGroups
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require "convenient_service"

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base do
include ConvenientService::RSpec::Matchers::DelegateTo

Expand All @@ -19,6 +20,7 @@
subject { described_class }

it { is_expected.to include_module(ConvenientService::Support::AbstractMethod) }
it { is_expected.to include_module(ConvenientService::Support::Copyable) }
end

example_group "attributes" do
Expand All @@ -39,17 +41,56 @@
it { is_expected.to have_abstract_method(:methods_middlewares_callers) }
end

# example_group "instance methods" do
# describe "#ancestors_greater_than_methods_middlewares_callers" do
# ##
# # NOTE: `#ancestors_greater_than_methods_middlewares_callers` is tested in `ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base` descendants.
# #
# end
#
# describe "#resolve_super_method" do
# ##
# # NOTE: `#resolve_super_method` is tested in `ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base` descendants.
# #
# end
# end
example_group "instance methods" do
# describe "#ancestors_greater_than_methods_middlewares_callers" do
# ##
# # NOTE: `#ancestors_greater_than_methods_middlewares_callers` is tested in `ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base` descendants.
# #
# end
#
# describe "#resolve_super_method" do
# ##
# # NOTE: `#resolve_super_method` is tested in `ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base` descendants.
# #
# end

describe "#to_kwargs" do
let(:kwargs) { {entity: entity} }

it "returns kwargs representation of caller" do
expect(caller.to_kwargs).to eq(kwargs)
end
end

example_group "comparison" do
describe "#==" do
let(:caller) { caller_class.new(entity: entity) }

context "when `other` has different class" do
let(:other) { 42 }

it "returns false" do
expect(caller == other).to be_nil
end
end

context "when `other` has different `entity`" do
let(:other) { described_class.new(entity: double) }

it "returns false" do
expect(caller == other).to eq(false)
end
end

context "when `other` has same attributes" do
let(:other) { caller_class.new(entity: entity) }

it "returns true" do
expect(caller == other).to eq(true)
end
end
end
end
end
end
# rubocop:enable RSpec/NestedGroups

0 comments on commit f2e315e

Please sign in to comment.