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:
parent
77390135c0
commit
d29f0f0af6
4 changed files with 20 additions and 22 deletions
|
@ -1,2 +1,5 @@
|
|||
AllCops:
|
||||
DisabledByDefault: false
|
||||
NewCops: enable
|
||||
require:
|
||||
- rubocop-rspec
|
||||
|
|
|
@ -3,10 +3,11 @@ require_relative 'base_formatter'
|
|||
module AwesomePrint
|
||||
module Formatters
|
||||
class MethodFormatter < BaseFormatter
|
||||
|
||||
attr_reader :method, :inspector, :options
|
||||
|
||||
def initialize(method, inspector)
|
||||
# RUBOCOP wants this, but it breaks the tests
|
||||
# super
|
||||
@method = method
|
||||
@inspector = inspector
|
||||
@options = inspector.options
|
||||
|
@ -15,7 +16,7 @@ module AwesomePrint
|
|||
def format
|
||||
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
|
||||
|
|
|
@ -39,12 +39,14 @@ module AwesomePrint
|
|||
keyword: :cyan,
|
||||
method: :purpleish,
|
||||
nilclass: :red,
|
||||
proc: :bisque,
|
||||
rational: :blue,
|
||||
string: :yellowish,
|
||||
struct: :pale,
|
||||
symbol: :cyanish,
|
||||
time: :greenish,
|
||||
trueclass: :green,
|
||||
unknown: :red,
|
||||
variable: :cyanish
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,60 +7,52 @@ RSpec.describe 'Single method' do
|
|||
|
||||
it 'plain: should handle a method with no arguments' do
|
||||
method = ''.method(:upcase)
|
||||
if RUBY_VERSION >= '2.4.0'
|
||||
expect(method.ai(plain: true)).to eq('String#upcase(*arg1)')
|
||||
else
|
||||
expect(method.ai(plain: true)).to eq('String#upcase()')
|
||||
end
|
||||
expect(method.ai(plain: true)).to eq('#<String#upcase(*arg1)>')
|
||||
end
|
||||
|
||||
it 'color: should handle a method with no arguments' do
|
||||
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")
|
||||
else
|
||||
expect(method.ai).to eq("\e[1;33mString\e[0m#\e[0;35mupcase\e[0m\e[0;37m()\e[0m")
|
||||
end
|
||||
expect(method.ai).to eq("#<\e[1;33mString\e[0m#\e[0;35mupcase\e[0m\e[0;37m(*arg1)\e[0m>")
|
||||
end
|
||||
|
||||
it 'plain: should handle a method with one argument' do
|
||||
method = ''.method(:include?)
|
||||
expect(method.ai(plain: true)).to eq('String#include?(arg1)')
|
||||
expect(method.ai(plain: true)).to eq('#<String#include?(arg1)>')
|
||||
end
|
||||
|
||||
it 'color: should handle a method with one argument' do
|
||||
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
|
||||
|
||||
it 'plain: should handle a method with two arguments' do
|
||||
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
|
||||
|
||||
it 'color: should handle a method with two arguments' do
|
||||
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
|
||||
|
||||
it 'plain: should handle a method with multiple arguments' do
|
||||
method = ''.method(:split)
|
||||
expect(method.ai(plain: true)).to eq('String#split(*arg1)')
|
||||
expect(method.ai(plain: true)).to eq('#<String#split(*arg1)>')
|
||||
end
|
||||
|
||||
it 'color: should handle a method with multiple arguments' do
|
||||
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
|
||||
|
||||
it 'plain: should handle a method defined in mixin' do
|
||||
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
|
||||
|
||||
it 'color: should handle a method defined in mixin' do
|
||||
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
|
||||
|
||||
it 'plain: should handle an unbound method' do
|
||||
|
@ -68,7 +60,7 @@ RSpec.describe 'Single method' do
|
|||
def world; end
|
||||
end
|
||||
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
|
||||
|
||||
it 'color: should handle an unbound method' do
|
||||
|
@ -79,7 +71,7 @@ RSpec.describe 'Single method' do
|
|||
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")
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue