From e9026eefe9d89e9cf215146d857806cf398a2424 Mon Sep 17 00:00:00 2001 From: Marian13 Date: Sat, 13 Jan 2024 18:06:35 +0200 Subject: [PATCH] test(has_instance_proxy): add specs --- .../commands/create_instance_proxy_class.rb | 38 ++++----- .../plugins/has_instance_proxy/concern.rb | 2 +- .../create_instance_proxy_class_spec.rb | 78 ++++++++++++++++++- .../has_instance_proxy/concern_spec.rb | 2 +- 4 files changed, 93 insertions(+), 27 deletions(-) diff --git a/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class.rb b/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class.rb index cdfd1c91a5f..ac9832dcf78 100644 --- a/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class.rb +++ b/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class.rb @@ -7,28 +7,22 @@ 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) @@ -36,7 +30,7 @@ def call # @example Result for feature. # # klass = ConvenientService::Common::Plugins::HasInstanceProxy::Commands::CreateInstanceProxyClass.call( - # namespace: SomeFeature + # target_class: SomeFeature # ) # # ## @@ -44,34 +38,34 @@ def call # # # # 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 diff --git a/lib/convenient_service/common/plugins/has_instance_proxy/concern.rb b/lib/convenient_service/common/plugins/has_instance_proxy/concern.rb index b2259c01c39..c8763569158 100644 --- a/lib/convenient_service/common/plugins/has_instance_proxy/concern.rb +++ b/lib/convenient_service/common/plugins/has_instance_proxy/concern.rb @@ -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 diff --git a/spec/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class_spec.rb b/spec/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class_spec.rb index c8a978be4e9..f2a5862741d 100644 --- a/spec/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class_spec.rb +++ b/spec/lib/convenient_service/common/plugins/has_instance_proxy/commands/create_instance_proxy_class_spec.rb @@ -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 diff --git a/spec/lib/convenient_service/common/plugins/has_instance_proxy/concern_spec.rb b/spec/lib/convenient_service/common/plugins/has_instance_proxy/concern_spec.rb index ef480e98de1..e59cbe02fc1 100644 --- a/spec/lib/convenient_service/common/plugins/has_instance_proxy/concern_spec.rb +++ b/spec/lib/convenient_service/common/plugins/has_instance_proxy/concern_spec.rb @@ -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