Skip to content

Commit

Permalink
feat(has_j_send_status_and_attributes): introduce Code.===
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Apr 30, 2023
1 parent 6365baa commit c2414cc
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module HasJSendStatusAndAttributes
module Entities
class Code
module Commands
##
# Check whether `code` can be considered as `Code` instance.
#
class IsCode < Support::Command
##
# @!attribute [r] code
Expand All @@ -27,7 +30,7 @@ def initialize(code:)
end

##
# @return [void]
# @return [Boolean]
#
def call
code.class.include?(Entities::Code::Concern)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ def cast(other)
new(value: other.value)
end
end

##
# @param other [Object] Can be any type.
# @return [Boolean, nil]
#
# @internal
# NOTE: Check `Module.===` in order to get an idea of how `super` works.
# - https://ruby-doc.org/core-2.7.0/Module.html#method-i-3D-3D-3D
#
def ===(other)
Commands::IsCode.call(code: other) || super
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code::Concern::ClassMethods do
include ConvenientService::RSpec::Matchers::DelegateTo

example_group "class methods" do
describe ".cast" do
let(:casted) { ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code.cast(other) }
Expand Down Expand Up @@ -45,6 +47,64 @@
end
end
end

describe ".===" do
let(:code_class) { ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code }

let(:other) { 42 }

specify do
expect { code_class === other }
.to delegate_to(ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code::Commands::IsCode, :call)
.with_arguments(code: other)
end

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

context "when `other` is code instance in terms of `ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code::Commands::IsCode`" do
let(:service) do
Class.new do
include ConvenientService::Configs::Minimal

def result
success
end
end
end

let(:other) { service.result.code }

specify do
expect { code_class === other }
.to delegate_to(ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code::Commands::IsCode, :call)
.with_arguments(code: other)
end

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

context "when `other` is `ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code` instance" do
let(:other) { ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code.cast(:success) }

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

context "when `other` is `ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code` descendant instance" do
let(:descendant_class) { Class.new(code_class) }

let(:other) { descendant_class.cast(:success) }

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

0 comments on commit c2414cc

Please sign in to comment.