Skip to content

Instantly share code, notes, and snippets.

@anicholson
Created August 15, 2020 07:44
Show Gist options
  • Save anicholson/80ee099953e3e449488e7f3f1e53157a to your computer and use it in GitHub Desktop.
Save anicholson/80ee099953e3e449488e7f3f1e53157a to your computer and use it in GitHub Desktop.
Dry::Monads::Do::All is broken for singleton classes
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'dry-monads'
gem 'rspec'
end
module ConvertToUpperCase
class SingletonDoAllShortcut
class << self
include Dry::Monads[:do, :try]
def call(string)
yield to_upper(string)
end
private
def to_upper(s)
Try { String(s).upcase }
end
end
end
class SingletonDoFor
class << self
include Dry::Monads[:try]
include Dry::Monads::Do.for(:call)
def call(string)
yield to_upper(string)
end
private
def to_upper(s)
Try { String(s).upcase }
end
end
end
class SingletonDoAllExplicit
class << self
include Dry::Monads[:try]
include Dry::Monads::Do::All
def call(string)
yield to_upper(string)
end
private
def to_upper(s)
Try { String(s).upcase }
end
end
end
class NonSingleton
include Dry::Monads[:try]
include Dry::Monads::Do::All
def self.call(string)
new.call(string)
end
def call(string)
yield to_upper(string)
end
private
def to_upper(s)
Try { String(s).upcase }
end
end
end
require 'rspec'
require 'rspec/core'
module ConvertToUpperCase
RSpec.describe 'Do::All is broken for singleton_classes' do
shared_examples_for 'an upcasing object' do
let(:input) { 'pow!' }
let(:result) { described_class.call(input) }
it 'does not raise an error' do
expect { result }.not_to raise_error
end
it 'upcases the string' do
expect(result).to eq('POW!')
end
end
[SingletonDoAllShortcut, SingletonDoAllExplicit, SingletonDoFor, NonSingleton].each do |example|
describe example do
it_behaves_like 'an upcasing object'
end
end
end
end
RSpec::Core::Runner.invoke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment