mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Changed formatting of optional method argments; added more method specs
This commit is contained in:
parent
a0dab8cd57
commit
8f94cef98f
3 changed files with 204 additions and 55 deletions
6
Rakefile
6
Rakefile
|
@ -11,7 +11,11 @@ begin
|
||||||
gem.email = "mike@dvorkin.net"
|
gem.email = "mike@dvorkin.net"
|
||||||
gem.homepage = "http://github.com/michaeldv/awesome_print"
|
gem.homepage = "http://github.com/michaeldv/awesome_print"
|
||||||
gem.authors = ["Michael Dvorkin"]
|
gem.authors = ["Michael Dvorkin"]
|
||||||
gem.add_development_dependency "rspec", ">= 1.2.9"
|
if RUBY_VERSION.to_f >= 1.9
|
||||||
|
gem.add_development_dependency "rspec", ">= 2.0.0"
|
||||||
|
else
|
||||||
|
gem.add_development_dependency "rspec", ">= 1.3.0"
|
||||||
|
end
|
||||||
gem.files = FileList["[A-Z]*", "lib/**/*.rb", "rails/*.rb", "spec/*", "init.rb"]
|
gem.files = FileList["[A-Z]*", "lib/**/*.rb", "rails/*.rb", "spec/*", "init.rb"]
|
||||||
gem.has_rdoc = false
|
gem.has_rdoc = false
|
||||||
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
||||||
|
|
|
@ -246,13 +246,17 @@ class AwesomePrint
|
||||||
# Return [ name, arguments, owner ] tuple for a given method.
|
# Return [ name, arguments, owner ] tuple for a given method.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def method_tuple(method)
|
def method_tuple(method)
|
||||||
args = method.arity.abs.times.map { |i| "arg#{i+1}" }.join(', ')
|
# arity: For Ruby methods that take a variable number of arguments, returns -N - 1,
|
||||||
args << ', ...' if method.arity < 0
|
# where N is the number of required arguments. For methods written in C, returns -1
|
||||||
|
# if the call takes a variable number of arguments.
|
||||||
|
args = method.arity.abs.times.map { |i| "arg#{i+1}" }
|
||||||
|
args[-1] = "*#{args[-1]}" if method.arity < 0
|
||||||
|
|
||||||
if method.to_s =~ /(Unbound)*Method: (.*?)[#\.]/
|
if method.to_s =~ /(Unbound)*Method: (.*?)[#\.]/
|
||||||
owner = "#{$2}#{$1 ? '(unbound)' : ''}".gsub('(', ' (')
|
owner = "#{$2}#{$1 ? '(unbound)' : ''}".gsub('(', ' (')
|
||||||
end
|
end
|
||||||
|
|
||||||
[ method.name.to_s, "(#{args})", owner.to_s ]
|
[ method.name.to_s, "(#{args.join(', ')})", owner.to_s ]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Format hash keys as plain string regardless of underlying data type.
|
# Format hash keys as plain string regardless of underlying data type.
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||||
|
|
||||||
describe "Single method" do
|
describe "Single method" do
|
||||||
|
after do
|
||||||
|
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
||||||
|
end
|
||||||
|
|
||||||
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)
|
||||||
method.ai(:plain => true).should == 'String#upcase()'
|
method.ai(:plain => true).should == 'String#upcase()'
|
||||||
|
@ -33,12 +37,12 @@ describe "Single method" do
|
||||||
|
|
||||||
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)
|
||||||
method.ai(:plain => true).should == 'String#split(arg1, ...)'
|
method.ai(:plain => true).should == '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)
|
||||||
method.ai.should == "\e[1;33mString\e[0m#\e[1;35msplit\e[0m\e[0;37m(arg1, ...)\e[0m"
|
method.ai.should == "\e[1;33mString\e[0m#\e[1;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
|
||||||
|
@ -57,7 +61,6 @@ describe "Single method" do
|
||||||
end
|
end
|
||||||
method = Hello.instance_method(:world)
|
method = Hello.instance_method(:world)
|
||||||
method.ai(:plain => true).should == 'Hello (unbound)#world()'
|
method.ai(:plain => true).should == 'Hello (unbound)#world()'
|
||||||
Object.instance_eval{ remove_const :Hello }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "color: should handle an unbound method" do
|
it "color: should handle an unbound method" do
|
||||||
|
@ -69,60 +72,198 @@ describe "Single method" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "object.methods" do
|
describe "Object methods" do
|
||||||
it "index: should handle object.methods" do
|
after do
|
||||||
out = nil.methods.ai(:plain => true).split("\n").grep(/is_a\?/).first
|
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
||||||
out.should =~ /^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "no index: should handle object.methods" do
|
describe "object.methods" do
|
||||||
out = nil.methods.ai(:plain => true, :index => false).split("\n").grep(/is_a\?/).first
|
it "index: should handle object.methods" do
|
||||||
out.should =~ /^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
|
out = nil.methods.ai(:plain => true).split("\n").grep(/is_a\?/).first
|
||||||
end
|
out.should =~ /^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "object.public_methods" do
|
it "no index: should handle object.methods" do
|
||||||
it "index: should handle object.public_methods" do
|
out = nil.methods.ai(:plain => true, :index => false).split("\n").grep(/is_a\?/).first
|
||||||
out = nil.public_methods.ai(:plain => true).split("\n").grep(/is_a\?/).first
|
out.should =~ /^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
|
||||||
out.should =~ /^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
|
|
||||||
end
|
|
||||||
|
|
||||||
it "no index: should handle object.public_methods" do
|
|
||||||
out = nil.public_methods.ai(:plain => true, :index => false).split("\n").grep(/is_a\?/).first
|
|
||||||
out.should =~ /^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "object.private_methods" do
|
|
||||||
it "index: should handle object.private_methods" do
|
|
||||||
out = nil.private_methods.ai(:plain => true).split("\n").grep(/sleep/).first
|
|
||||||
out.should =~ /^\s+\[\s*\d+\]\s+sleep\(arg1,\s\.{3}\)\s+NilClass \(Kernel\)$/
|
|
||||||
end
|
|
||||||
|
|
||||||
it "no index: should handle object.private_methods" do
|
|
||||||
out = nil.private_methods.ai(:plain => true, :index => false).split("\n").grep(/sleep/).first
|
|
||||||
out.should =~ /^\s+sleep\(arg1,\s\.{3}\)\s+NilClass \(Kernel\)$/
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "object.protected_methods" do
|
|
||||||
it "index: should handle object.protected_methods" do
|
|
||||||
class Hello
|
|
||||||
protected
|
|
||||||
def one; end
|
|
||||||
def two; end
|
|
||||||
end
|
end
|
||||||
Hello.new.protected_methods.ai(:plain => true).should == "[\n [0] one() Hello\n [1] two() Hello\n]"
|
|
||||||
Object.instance_eval{ remove_const :Hello }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "index: should handle object.protected_methods" do
|
describe "object.public_methods" do
|
||||||
class Hello
|
it "index: should handle object.public_methods" do
|
||||||
protected
|
out = nil.public_methods.ai(:plain => true).split("\n").grep(/is_a\?/).first
|
||||||
def world(a,b); end
|
out.should =~ /^\s+\[\s*\d+\]\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle object.public_methods" do
|
||||||
|
out = nil.public_methods.ai(:plain => true, :index => false).split("\n").grep(/is_a\?/).first
|
||||||
|
out.should =~ /^\s+is_a\?\(arg1\)\s+NilClass \(Kernel\)$/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "object.private_methods" do
|
||||||
|
it "index: should handle object.private_methods" do
|
||||||
|
out = nil.private_methods.ai(:plain => true).split("\n").grep(/sleep/).first
|
||||||
|
out.should =~ /^\s+\[\s*\d+\]\s+sleep\(\*arg1\)\s+NilClass \(Kernel\)$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle object.private_methods" do
|
||||||
|
out = nil.private_methods.ai(:plain => true, :index => false).split("\n").grep(/sleep/).first
|
||||||
|
out.should =~ /^\s+sleep\(\*arg1\)\s+NilClass \(Kernel\)$/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "object.protected_methods" do
|
||||||
|
it "index: should handle object.protected_methods" do
|
||||||
|
class Hello
|
||||||
|
protected
|
||||||
|
def m1; end
|
||||||
|
def m2; end
|
||||||
|
end
|
||||||
|
Hello.new.protected_methods.ai(:plain => true).should == "[\n [0] m1() Hello\n [1] m2() Hello\n]"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle object.protected_methods" do
|
||||||
|
class Hello
|
||||||
|
protected
|
||||||
|
def m3(a,b); end
|
||||||
|
end
|
||||||
|
Hello.new.protected_methods.ai(:plain => true, :index => false).should == "[\n m3(arg1, arg2) Hello\n]"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "object.private_methods" do
|
||||||
|
it "index: should handle object.private_methods" do
|
||||||
|
class Hello
|
||||||
|
private
|
||||||
|
def m1; end
|
||||||
|
def m2; end
|
||||||
|
end
|
||||||
|
out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello$/
|
||||||
|
out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle object.private_methods" do
|
||||||
|
class Hello
|
||||||
|
private
|
||||||
|
def m3(a,b); end
|
||||||
|
end
|
||||||
|
out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+\[\d+\]\s+m3\(arg1, arg2\)\s+Hello$/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "object.singleton_methods" do
|
||||||
|
it "index: should handle object.singleton_methods" do
|
||||||
|
class Hello
|
||||||
|
class << self
|
||||||
|
def m1; end
|
||||||
|
def m2; end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
out = Hello.singleton_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello$/
|
||||||
|
out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle object.singleton_methods" do
|
||||||
|
class Hello
|
||||||
|
def self.m3(a,b); end
|
||||||
|
end
|
||||||
|
out = Hello.singleton_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello$/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "Class methods" do
|
||||||
|
after do
|
||||||
|
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "class.instance_methods" do
|
||||||
|
it "index: should handle unbound class.instance_methods" do
|
||||||
|
class Hello
|
||||||
|
def m1; end
|
||||||
|
def m2; end
|
||||||
|
end
|
||||||
|
out = Hello.instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
|
||||||
|
out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle unbound class.instance_methods" do
|
||||||
|
class Hello
|
||||||
|
def m3(a,b); end
|
||||||
|
end
|
||||||
|
out = Hello.instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "class.public_instance_methods" do
|
||||||
|
it "index: should handle class.public_instance_methods" do
|
||||||
|
class Hello
|
||||||
|
def m1; end
|
||||||
|
def m2; end
|
||||||
|
end
|
||||||
|
out = Hello.public_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
|
||||||
|
out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle class.public_instance_methods" do
|
||||||
|
class Hello
|
||||||
|
def m3(a,b); end
|
||||||
|
end
|
||||||
|
out = Hello.public_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "class.protected_instance_methods" do
|
||||||
|
it "index: should handle class.protected_instance_methods" do
|
||||||
|
class Hello
|
||||||
|
protected
|
||||||
|
def m1; end
|
||||||
|
def m2; end
|
||||||
|
end
|
||||||
|
out = Hello.protected_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
|
||||||
|
out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle class.protected_instance_methods" do
|
||||||
|
class Hello
|
||||||
|
protected
|
||||||
|
def m3(a,b); end
|
||||||
|
end
|
||||||
|
out = Hello.protected_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "class.private_instance_methods" do
|
||||||
|
it "index: should handle class.private_instance_methods" do
|
||||||
|
class Hello
|
||||||
|
private
|
||||||
|
def m1; end
|
||||||
|
def m2; end
|
||||||
|
end
|
||||||
|
out = Hello.private_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
|
||||||
|
out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "no index: should handle class.private_instance_methods" do
|
||||||
|
class Hello
|
||||||
|
private
|
||||||
|
def m3(a,b); end
|
||||||
|
end
|
||||||
|
out = Hello.private_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
|
||||||
|
out.first.should =~ /^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/
|
||||||
end
|
end
|
||||||
Hello.new.protected_methods.ai(:plain => true, :index => false).should == "[\n world(arg1, arg2) Hello\n]"
|
|
||||||
Object.instance_eval{ remove_const :Hello }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue