1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Method outputs as #<method> format

This commit is contained in:
Bryan Hanks, PMP 2021-01-29 08:35:52 -06:00
parent 77390135c0
commit d29f0f0af6
4 changed files with 20 additions and 22 deletions

View file

@ -1,2 +1,5 @@
AllCops: AllCops:
DisabledByDefault: false DisabledByDefault: false
NewCops: enable
require:
- rubocop-rspec

View file

@ -3,10 +3,11 @@ require_relative 'base_formatter'
module AwesomePrint module AwesomePrint
module Formatters module Formatters
class MethodFormatter < BaseFormatter class MethodFormatter < BaseFormatter
attr_reader :method, :inspector, :options attr_reader :method, :inspector, :options
def initialize(method, inspector) def initialize(method, inspector)
# RUBOCOP wants this, but it breaks the tests
# super
@method = method @method = method
@inspector = inspector @inspector = inspector
@options = inspector.options @options = inspector.options
@ -15,7 +16,7 @@ module AwesomePrint
def format def format
name, args, owner = method_tuple(method) name, args, owner = method_tuple(method)
"#{colorize(owner, :class)}##{colorize(name, :method)}#{colorize(args, :args)}" "#<#{colorize(owner, :class)}##{colorize(name, :method)}#{colorize(args, :args)}>"
end end
end end
end end

View file

@ -39,12 +39,14 @@ module AwesomePrint
keyword: :cyan, keyword: :cyan,
method: :purpleish, method: :purpleish,
nilclass: :red, nilclass: :red,
proc: :bisque,
rational: :blue, rational: :blue,
string: :yellowish, string: :yellowish,
struct: :pale, struct: :pale,
symbol: :cyanish, symbol: :cyanish,
time: :greenish, time: :greenish,
trueclass: :green, trueclass: :green,
unknown: :red,
variable: :cyanish variable: :cyanish
} }
} }

View file

@ -7,60 +7,52 @@ RSpec.describe 'Single method' do
it 'plain: should handle a method with no arguments' do it 'plain: should handle a method with no arguments' do
method = ''.method(:upcase) method = ''.method(:upcase)
if RUBY_VERSION >= '2.4.0' expect(method.ai(plain: true)).to eq('#<String#upcase(*arg1)>')
expect(method.ai(plain: true)).to eq('String#upcase(*arg1)')
else
expect(method.ai(plain: true)).to eq('String#upcase()')
end
end end
it 'color: should handle a method with no arguments' do it 'color: should handle a method with no arguments' do
method = ''.method(:upcase) method = ''.method(:upcase)
if RUBY_VERSION >= '2.4.0' expect(method.ai).to eq("#<\e[1;33mString\e[0m#\e[0;35mupcase\e[0m\e[0;37m(*arg1)\e[0m>")
expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mupcase\e[0m\e[0;37m(*arg1)\e[0m")
else
expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mupcase\e[0m\e[0;37m()\e[0m")
end
end end
it 'plain: should handle a method with one argument' do it 'plain: should handle a method with one argument' do
method = ''.method(:include?) method = ''.method(:include?)
expect(method.ai(plain: true)).to eq('String#include?(arg1)') expect(method.ai(plain: true)).to eq('#<String#include?(arg1)>')
end end
it 'color: should handle a method with one argument' do it 'color: should handle a method with one argument' do
method = ''.method(:include?) method = ''.method(:include?)
expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35minclude?\e[0m\e[0;37m(arg1)\e[0m") expect(method.ai).to eq("#<\e[1;33mString\e[0m#\e[0;35minclude?\e[0m\e[0;37m(arg1)\e[0m>")
end end
it 'plain: should handle a method with two arguments' do it 'plain: should handle a method with two arguments' do
method = ''.method(:tr) method = ''.method(:tr)
expect(method.ai(plain: true)).to eq('String#tr(arg1, arg2)') expect(method.ai(plain: true)).to eq('#<String#tr(arg1, arg2)>')
end end
it 'color: should handle a method with two arguments' do it 'color: should handle a method with two arguments' do
method = ''.method(:tr) method = ''.method(:tr)
expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mtr\e[0m\e[0;37m(arg1, arg2)\e[0m") expect(method.ai).to eq("#<\e[1;33mString\e[0m#\e[0;35mtr\e[0m\e[0;37m(arg1, arg2)\e[0m>")
end end
it 'plain: should handle a method with multiple arguments' do it 'plain: should handle a method with multiple arguments' do
method = ''.method(:split) method = ''.method(:split)
expect(method.ai(plain: true)).to eq('String#split(*arg1)') expect(method.ai(plain: true)).to eq('#<String#split(*arg1)>')
end end
it 'color: should handle a method with multiple arguments' do it 'color: should handle a method with multiple arguments' do
method = ''.method(:split) method = ''.method(:split)
expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35msplit\e[0m\e[0;37m(*arg1)\e[0m") expect(method.ai).to eq("#<\e[1;33mString\e[0m#\e[0;35msplit\e[0m\e[0;37m(*arg1)\e[0m>")
end end
it 'plain: should handle a method defined in mixin' do it 'plain: should handle a method defined in mixin' do
method = ''.method(:is_a?) method = ''.method(:is_a?)
expect(method.ai(plain: true)).to eq('String (Kernel)#is_a?(arg1)') expect(method.ai(plain: true)).to eq('#<String (Kernel)#is_a?(arg1)>')
end end
it 'color: should handle a method defined in mixin' do it 'color: should handle a method defined in mixin' do
method = ''.method(:is_a?) method = ''.method(:is_a?)
expect(method.ai).to eq("\e[1;33mString (Kernel)\e[0m#\e[0;35mis_a?\e[0m\e[0;37m(arg1)\e[0m") expect(method.ai).to eq("#<\e[1;33mString (Kernel)\e[0m#\e[0;35mis_a?\e[0m\e[0;37m(arg1)\e[0m>")
end end
it 'plain: should handle an unbound method' do it 'plain: should handle an unbound method' do
@ -68,7 +60,7 @@ RSpec.describe 'Single method' do
def world; end def world; end
end end
method = Hello.instance_method(:world) method = Hello.instance_method(:world)
expect(method.ai(plain: true)).to eq('Hello (unbound)#world()') expect(method.ai(plain: true)).to eq('#<Hello (unbound)#world()>')
end end
it 'color: should handle an unbound method' do it 'color: should handle an unbound method' do
@ -79,7 +71,7 @@ RSpec.describe 'Single method' do
if RUBY_VERSION < '1.9.2' if RUBY_VERSION < '1.9.2'
expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(arg1, arg2)\e[0m") expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(arg1, arg2)\e[0m")
else else
expect(method.ai).to eq("\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(a, b)\e[0m") expect(method.ai).to eq("#<\e[1;33mHello (unbound)\e[0m#\e[0;35mworld\e[0m\e[0;37m(a, b)\e[0m>")
end end
end end
end end