Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop, added some code improvements from [@nathan-appere] #403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
AllCops:
DisabledByDefault: false
NewCops: enable
require:
- rubocop-rspec
5 changes: 3 additions & 2 deletions lib/awesome_print/formatters/method_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/awesome_print/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -149,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
Expand All @@ -163,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
32 changes: 12 additions & 20 deletions spec/methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,60 @@

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
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('#<Hello (unbound)#world()>')
end

it 'color: should handle an unbound method' do
Expand All @@ -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
Expand Down