From d29f0f0af6f3de6025df0955e233f3e644665f89 Mon Sep 17 00:00:00 2001 From: "Bryan Hanks, PMP" Date: Fri, 29 Jan 2021 08:35:52 -0600 Subject: [PATCH 1/2] Method outputs as # format --- .rubocop.yml | 3 ++ .../formatters/method_formatter.rb | 5 +-- lib/awesome_print/inspector.rb | 2 ++ spec/methods_spec.rb | 32 +++++++------------ 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c96fa308..1156982c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,2 +1,5 @@ AllCops: DisabledByDefault: false + NewCops: enable +require: + - rubocop-rspec diff --git a/lib/awesome_print/formatters/method_formatter.rb b/lib/awesome_print/formatters/method_formatter.rb index 26c398db..13fd2aa8 100644 --- a/lib/awesome_print/formatters/method_formatter.rb +++ b/lib/awesome_print/formatters/method_formatter.rb @@ -3,10 +3,11 @@ 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 @@ def initialize(method, inspector) 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 diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 437feb5f..c1a0e06e 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -39,12 +39,14 @@ def initialize(options = {}) keyword: :cyan, method: :purpleish, nilclass: :red, + proc: :bisque, rational: :blue, string: :yellowish, struct: :pale, symbol: :cyanish, time: :greenish, trueclass: :green, + unknown: :red, variable: :cyanish } } diff --git a/spec/methods_spec.rb b/spec/methods_spec.rb index 222727e8..aba22aa5 100644 --- a/spec/methods_spec.rb +++ b/spec/methods_spec.rb @@ -7,60 +7,52 @@ 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('#') 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('#') 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('#') 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('#') 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('#') 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 @@ class Hello 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('#') end it 'color: should handle an unbound method' do @@ -79,7 +71,7 @@ def world(a, b); end 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 From cbc666518d32840d1860076794d46fccfdbee0fa Mon Sep 17 00:00:00 2001 From: "Bryan Hanks, PMP" Date: Fri, 29 Jan 2021 09:15:07 -0600 Subject: [PATCH 2/2] Rubocop inspector --- lib/awesome_print/inspector.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index c1a0e06e..2eb5aad4 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -151,7 +151,7 @@ def load_dotfile load dotfile if dotfile_readable?(dotfile) end - def dotfile_readable? dotfile + def dotfile_readable?(dotfile) if @@dotfile_readable.nil? || @@dotfile != dotfile @@dotfile_readable = File.readable?(@@dotfile = dotfile) end @@ -165,7 +165,7 @@ def merge_custom_defaults! load_dotfile merge_options!(AwesomePrint.defaults) if AwesomePrint.defaults.is_a?(Hash) rescue => e - $stderr.puts "Could not load '.aprc' from ENV['HOME']: #{e}" + warn "Could not load '.aprc' from ENV['HOME']: #{e}" end end end