Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/dry/monads/maybe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ def to_s
end
end
alias_method :inspect, :to_s

def pretty_print(pp)
pp.text "Some("
unless Unit.equal?(@value)
pp.group(1) do
pp.breakable("")
pp.pp(@value)
end
end
pp.text ")"
end
end

# Represents an absence of a value, i.e. the value nil.
Expand Down Expand Up @@ -238,6 +249,10 @@ def or_fmap(...) = Maybe.coerce(self.or(...))
def to_s = "None"
alias_method :inspect, :to_s

def pretty_print(pp)
pp.text "None"
end

# @api private
def eql?(other) = other.is_a?(None)

Expand Down
22 changes: 22 additions & 0 deletions lib/dry/monads/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ def to_s
end
alias_method :inspect, :to_s

def pretty_print(pp)
pp.text "Success("
unless Unit.equal?(@value)
pp.group(1) do
pp.breakable("")
pp.pp(@value)
end
end
pp.text ")"
end

# Transforms to a Failure instance
#
# @return [Result::Failure]
Expand Down Expand Up @@ -227,6 +238,17 @@ def to_s
end
alias_method :inspect, :to_s

def pretty_print(pp)
pp.text "Failure("
unless Unit.equal?(@value)
pp.group(1) do
pp.breakable("")
pp.pp(@value)
end
end
pp.text ")"
end

# Transform to a Success instance
#
# @return [Result::Success]
Expand Down
16 changes: 16 additions & 0 deletions spec/unit/maybe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@
expect(subject.inspect).to eql('Some("foo")')
end

it "pretty prints" do
out = +""
PP.pp(subject, out)
expect(out).to eql(%{Some("foo")\n})

out = +""
PP.pp(some[unit], out)
expect(out).to eql("Some()\n")
end

describe ".to_proc" do
it "returns a constructor block" do
expect(maybe::Some.to_proc.("foo")).to eql(subject)
Expand Down Expand Up @@ -305,6 +315,12 @@
expect(subject.inspect).to eql("None")
end

it "pretty prints" do
out = +""
PP.pp(subject, out)
expect(out).to eql(%(None\n))
end

describe ".missing_method" do
it "shows a friendly error messsage if an instance method is called" do
expect { described_class.fmap }.to raise_error(
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
expect(subject.inspect).to eql('Success("foo")')
end

it "pretty prints" do
out = +""
PP.pp(subject, out)
expect(out).to eql(%{Success("foo")\n})

out = +""
PP.pp(success[unit], out)
expect(out).to eql("Success()\n")
end

describe ".[]" do
it "builds a Success with an array" do
expect(described_class[:found, 1]).to eql(success[[:found, 1]])
Expand Down Expand Up @@ -351,6 +361,16 @@
expect(subject.inspect).to eql('Failure("bar")')
end

it "pretty prints" do
out = +""
PP.pp(subject, out)
expect(out).to eql(%{Failure("bar")\n})

out = +""
PP.pp(failure[unit], out)
expect(out).to eql("Failure()\n")
end

describe ".[]" do
it "builds a Failure with an array" do
expect(described_class[:not_found, :but_tried]).to eql(failure[[:not_found, :but_tried]])
Expand Down
Loading