Skip to content

Commit

Permalink
test(has_instance_proxy): add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Jan 13, 2024
1 parent 6be8d3b commit e9026ee
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,65 @@ module HasInstanceProxy
module Commands
class CreateInstanceProxyClass < Support::Command
##
# @!attribute [r] namespace
# @!attribute [r] target_class
# @return [Class]
#
attr_reader :namespace
attr_reader :target_class

##
# @param namespace [Class]
# @param target_class [Class]
# @return [void]
#
# @internal
# TODO: Direct Specs.
#
def initialize(namespace:)
@namespace = namespace
def initialize(target_class:)
@target_class = target_class
end

##
# @return [void]
#
# @internal
# TODO: Direct Specs.
#
def call
klass = ::Class.new(Entities::InstanceProxy)

##
# @example Result for feature.
#
# klass = ConvenientService::Common::Plugins::HasInstanceProxy::Commands::CreateInstanceProxyClass.call(
# namespace: SomeFeature
# target_class: SomeFeature
# )
#
# ##
# # `klass` is something like:
# #
# # class InstanceProxy < ConvenientService::Service::Plugins::HasInstanceProxy::Entities::InstanceProxy
# # class << self
# # def namespace
# # def target_class
# # ##
# # # NOTE: Returns `namespace` passed to `CreateInstanceProxyClass`.
# # # NOTE: Returns `target_class` passed to `CreateInstanceProxyClass`.
# # #
# # namespace
# # target_class
# # end
# #
# # def ==(other)
# # return unless other.respond_to?(:namespace)
# # return unless other.respond_to?(:target_class)
# #
# # self.namespace == other.namespace
# # self.target_class == other.target_class
# # end
# # end
# # end
#
klass.class_exec(namespace) do |namespace|
define_singleton_method(:namespace) { namespace }
klass.class_exec(target_class) do |target_class|
define_singleton_method(:target_class) { target_class }

##
# @internal
# TODO: Try `self.namespace == other.namespace if self < ::ConvenientService::Common::Plugins::HasInstanceProxy::Entities::InstanceProxy`.
# TODO: Try `self.target_class == other.target_class if self < ::ConvenientService::Common::Plugins::HasInstanceProxy::Entities::InstanceProxy`.
#
define_singleton_method(:==) { |other| self.namespace == other.namespace if other.respond_to?(:namespace) }
define_singleton_method(:==) { |other| self.target_class == other.target_class if other.respond_to?(:target_class) }

##
# TODO: `inspect`.
#
# define_singleton_method(:inspect) { "#{namespace}InstanceProxy" }
# define_singleton_method(:inspect) { "#{target_class}InstanceProxy" }
end

klass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Concern
# @return [Class] Can be any type.
#
def instance_proxy_class
@instance_proxy_class ||= Commands::CreateInstanceProxyClass[namespace: self]
@instance_proxy_class ||= Commands::CreateInstanceProxyClass[target_class: self]
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,75 @@
##
# TODO: Specs.
##
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Common::Plugins::HasInstanceProxy::Commands::CreateInstanceProxyClass do
include ConvenientService::RSpec::Matchers::DelegateTo

example_group "class methods" do
describe ".call" do
include ConvenientService::RSpec::PrimitiveMatchers::BeDescendantOf

subject(:command_result) { described_class.call(target_class: target_class) }

let(:target_class) { Class.new }

it "returns `Class` instance" do
expect(command_result).to be_instance_of(Class)
end

example_group "instance_proxy_class" do
let(:instance_proxy_class) { described_class.call(target_class: target_class) }

example_group "inheritance" do
include ConvenientService::RSpec::PrimitiveMatchers::BeDescendantOf

subject { instance_proxy_class }

it { is_expected.to be_descendant_of(ConvenientService::Common::Plugins::HasInstanceProxy::Entities::InstanceProxy) }
end

example_group "class methods" do
describe ".target_class" do
it "returns `target_class` passed to `ConvenientService::Common::Plugins::HasInstanceProxy::Commands::CreateInstanceProxyClass`" do
expect(instance_proxy_class.target_class).to eq(target_class)
end
end

describe ".==" do
context "when `other` does NOT respond to `target_class`" do
let(:other) { 42 }

it "returns `nil`" do
expect(instance_proxy_class == other).to be_nil
end
end

context "when `other` has different `target_class`" do
let(:other) { described_class.call(target_class: Class.new) }

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

context "when `other` has same attributes" do
let(:other) { described_class.call(target_class: target_class) }

it "returns `true`" do
expect(instance_proxy_class == other).to eq(true)
end
end
end
end

##
# TODO: Specs for autogenerated `#inspect`.
#
end
end
end
end
# rubocop:enable RSpec/NestedGroups
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
specify do
expect { klass.instance_proxy_class }
.to delegate_to(ConvenientService::Common::Plugins::HasInstanceProxy::Commands::CreateInstanceProxyClass, :call)
.with_arguments(namespace: klass)
.with_arguments(target_class: klass)
.and_return_its_value
end

Expand Down

0 comments on commit e9026ee

Please sign in to comment.