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

Remove conditional check against old Ruby versions

AwesomePrint supports Ruby 1.9.3 or later.
This remove lot of conditional check against Ruby prior to 1.9.3.
This commit is contained in:
Mauro George 2015-02-12 19:57:41 -02:00
parent d8aeb66b4b
commit 7e6e7c01aa
4 changed files with 69 additions and 167 deletions

View file

@ -27,11 +27,7 @@ RSpec.describe "AwesomePrint/ActiveRecord", skip: ->{ !ExtVerifier.has_rails? }.
:rank => 1 :rank => 1
} }
EOS EOS
if RUBY_VERSION < '1.9' str.sub!('?', '1992-10-10 12:30:00 UTC')
str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
else
str.sub!('?', '1992-10-10 12:30:00 UTC')
end
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
end end
@ -55,13 +51,8 @@ RSpec.describe "AwesomePrint/ActiveRecord", skip: ->{ !ExtVerifier.has_rails? }.
} }
] ]
EOS EOS
if RUBY_VERSION < '1.9' str.sub!('??', '1992-10-10 12:30:00 UTC')
str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992') str.sub!('?!', '2003-05-26 14:15:00 UTC')
str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
else
str.sub!('??', '1992-10-10 12:30:00 UTC')
str.sub!('?!', '2003-05-26 14:15:00 UTC')
end
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
end end
@ -87,13 +78,8 @@ RSpec.describe "AwesomePrint/ActiveRecord", skip: ->{ !ExtVerifier.has_rails? }.
} }
] ]
EOS EOS
if RUBY_VERSION < '1.9' str.sub!('??', '1992-10-10 12:30:00 UTC')
str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992') str.sub!('?!', '2003-05-26 14:15:00 UTC')
str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
else
str.sub!('??', '1992-10-10 12:30:00 UTC')
str.sub!('?!', '2003-05-26 14:15:00 UTC')
end
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
end end
end end
@ -1695,11 +1681,7 @@ class SubUser < User {
out = @ap.send(:awesome, User.methods.grep(/first/)) out = @ap.send(:awesome, User.methods.grep(/first/))
if ActiveRecord::VERSION::STRING >= "3.2" if ActiveRecord::VERSION::STRING >= "3.2"
if RUBY_VERSION >= "1.9" expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/)
expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/)
else
expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/)
end
else else
expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/) expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
end end

View file

@ -361,14 +361,7 @@ EOS
it "plain multiline" do it "plain multiline" do
out = @hash.ai(:plain => true) out = @hash.ai(:plain => true)
if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed. expect(out).to eq <<-EOS.strip
expect(out).to match(/^\{[^\}]+\}/m)
expect(out).to match(/ "b" => "b",?/)
expect(out).to match(/ :a => "a",?/)
expect(out).to match(/ :z => "z",?/)
expect(out).to match(/ "alpha" => "alpha",?$/)
else
expect(out).to eq <<-EOS.strip
{ {
"b" => "b", "b" => "b",
:a => "a", :a => "a",
@ -376,7 +369,6 @@ EOS
"alpha" => "alpha" "alpha" => "alpha"
} }
EOS EOS
end
end end
it "plain multiline with sorted keys" do it "plain multiline with sorted keys" do
@ -488,16 +480,7 @@ EOS
it "should present Rational object with arbitrary precision" do it "should present Rational object with arbitrary precision" do
rat = Rational(201020102010201020102010201020102010, 2) rat = Rational(201020102010201020102010201020102010, 2)
out = rat.ai(:plain => true) out = rat.ai(:plain => true)
# expect(out).to eq("100510051005100510051005100510051005/1")
# Ruby 1.9 slightly changed the format of Rational#to_s, see
# http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3 and
# http://www.ruby-forum.com/topic/189397
#
if RUBY_VERSION < "1.9"
expect(out).to eq("100510051005100510051005100510051005")
else
expect(out).to eq("100510051005100510051005100510051005/1")
end
end end
end end
@ -523,38 +506,20 @@ EOS
expect(Set.new.ai).to eq([].ai) expect(Set.new.ai).to eq([].ai)
end end
if RUBY_VERSION > "1.9" it "plain multiline" do
it "plain multiline" do expect(@set.ai(:plain => true)).to eq(@arr.ai(:plain => true))
expect(@set.ai(:plain => true)).to eq(@arr.ai(:plain => true)) end
end
it "plain multiline indented" do it "plain multiline indented" do
expect(@set.ai(:plain => true, :indent => 1)).to eq(@arr.ai(:plain => true, :indent => 1)) expect(@set.ai(:plain => true, :indent => 1)).to eq(@arr.ai(:plain => true, :indent => 1))
end end
it "plain single line" do it "plain single line" do
expect(@set.ai(:plain => true, :multiline => false)).to eq(@arr.ai(:plain => true, :multiline => false)) expect(@set.ai(:plain => true, :multiline => false)).to eq(@arr.ai(:plain => true, :multiline => false))
end end
it "colored multiline (default)" do it "colored multiline (default)" do
expect(@set.ai).to eq(@arr.ai) expect(@set.ai).to eq(@arr.ai)
end
else # Prior to Ruby 1.9 the order of set values is unpredicatble.
it "plain multiline" do
expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true))
end
it "plain multiline indented" do
expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true, :indent => 1)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true, :indent => 1))
end
it "plain single line" do
expect(@set.sort_by{ |x| x.to_s }.ai(:plain => true, :multiline => false)).to eq(@arr.sort_by{ |x| x.to_s }.ai(:plain => true, :multiline => false))
end
it "colored multiline (default)" do
expect(@set.sort_by{ |x| x.to_s }.ai).to eq(@arr.sort_by{ |x| x.to_s }.ai)
end
end end
end end

View file

@ -72,11 +72,7 @@ RSpec.describe "Single method" do
def world(a,b); end def world(a,b); end
end end
method = Hello.instance_method(:world) method = Hello.instance_method(:world)
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(a, b)\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
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
@ -140,11 +136,7 @@ RSpec.describe "Object methods" do
protected protected
def m3(a,b); end def m3(a,b); end
end end
if RUBY_VERSION < '1.9.2' expect(Hello.new.protected_methods.ai(:plain => true, :index => false)).to eq("[\n m3(a, b) Hello\n]")
expect(Hello.new.protected_methods.ai(:plain => true, :index => false)).to eq("[\n m3(arg1, arg2) Hello\n]")
else
expect(Hello.new.protected_methods.ai(:plain => true, :index => false)).to eq("[\n m3(a, b) Hello\n]")
end
end end
end end
@ -167,11 +159,7 @@ RSpec.describe "Object methods" do
def m3(a,b); end def m3(a,b); end
end end
out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/) out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
if RUBY_VERSION < '1.9.2' expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(a, b\)\s+Hello$/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(arg1, arg2\)\s+Hello$/)
else
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m3\(a, b\)\s+Hello$/)
end
end end
end end
@ -193,11 +181,7 @@ RSpec.describe "Object methods" do
def self.m3(a,b); end def self.m3(a,b); end
end end
out = Hello.singleton_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) out = Hello.singleton_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
if RUBY_VERSION < '1.9.2' expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello$/)
expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello$/)
else
expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello$/)
end
end end
end end
end end
@ -227,11 +211,7 @@ RSpec.describe "Class methods" do
def m3(a,b); end def m3(a,b); end
end end
out = Hello.instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) out = Hello.instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
if RUBY_VERSION < '1.9.2' expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
else
expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
end
end end
end end
@ -251,11 +231,7 @@ RSpec.describe "Class methods" do
def m3(a,b); end def m3(a,b); end
end end
out = Hello.public_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) out = Hello.public_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
if RUBY_VERSION < '1.9.2' expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
else
expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
end
end end
end end
@ -277,11 +253,7 @@ RSpec.describe "Class methods" do
def m3(a,b); end def m3(a,b); end
end end
out = Hello.protected_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) out = Hello.protected_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
if RUBY_VERSION < '1.9.2' expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
else
expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
end
end end
end end
@ -303,64 +275,58 @@ RSpec.describe "Class methods" do
def m3(a,b); end def m3(a,b); end
end end
out = Hello.private_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/) out = Hello.private_instance_methods.ai(:plain => true, :index => false).split("\n").grep(/m\d/)
if RUBY_VERSION < '1.9.2' expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
expect(out.first).to match(/^\s+m3\(arg1, arg2\)\s+Hello\s\(unbound\)$/)
else
expect(out.first).to match(/^\s+m3\(a, b\)\s+Hello\s\(unbound\)$/)
end
end end
end end
end end
if RUBY_VERSION >= '1.9.2' RSpec.describe "Ruby 1.9.2+ Method#parameters" do
RSpec.describe "Ruby 1.9.2+ Method#parameters" do before do
before do stub_dotfile!
stub_dotfile! end
end
after do after do
Object.instance_eval{ remove_const :Hello } if defined?(Hello) Object.instance_eval{ remove_const :Hello } if defined?(Hello)
end end
it "()" do it "()" do
class Hello class Hello
def m1; end def m1; end
end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/)
end end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/)
end
it ":req" do it ":req" do
class Hello class Hello
def m1(a, b, c); end def m1(a, b, c); end
end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, b, c\)\s+Hello$/)
end end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, b, c\)\s+Hello$/)
end
it ":opt" do it ":opt" do
class Hello class Hello
def m1(a, b = 1, c = 2); end # m1(a, *b, *c) def m1(a, b = 1, c = 2); end # m1(a, *b, *c)
end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, \*c\)\s+Hello$/)
end end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, \*c\)\s+Hello$/)
end
it ":rest" do it ":rest" do
class Hello class Hello
def m1(*a); end # m1(*a) def m1(*a); end # m1(*a)
end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\*a\)\s+Hello$/)
end end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(\*a\)\s+Hello$/)
end
it ":block" do it ":block" do
class Hello class Hello
def m1(a, b = nil, &blk); end # m1(a, *b, &blk) def m1(a, b = nil, &blk); end # m1(a, *b, &blk)
end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, &blk\)\s+Hello$/)
end end
out = Hello.new.methods.ai(:plain => true).split("\n").grep(/m1/)
expect(out.first).to match(/^\s+\[\s*\d+\]\s+m1\(a, \*b, &blk\)\s+Hello$/)
end end
end end
@ -450,10 +416,6 @@ RSpec.describe "Methods arrays" do
it "appending garbage to methods array should not raise error" do it "appending garbage to methods array should not raise error" do
arr = 42.methods << [ :wtf ] arr = 42.methods << [ :wtf ]
expect { arr.ai(:plain => true) }.not_to raise_error expect { arr.ai(:plain => true) }.not_to raise_error
if RUBY_VERSION < '1.9.2' expect(arr.ai(:plain => true)).to match(/\s+\[:wtf\]\(\?\)\s+\?/) # [ :wtf ].to_s => [:wtf]
expect(arr.ai(:plain => true)).to match(/\s+wtf\(\?\)\s+\?/) # [ :wtf ].to_s => "wtf"
else
expect(arr.ai(:plain => true)).to match(/\s+\[:wtf\]\(\?\)\s+\?/) # [ :wtf ].to_s => [:wtf]
end
end end
end end

View file

@ -32,13 +32,10 @@ RSpec.describe "AwesomePrint" do
expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ "1", "2" ]') expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ "1", "2" ]')
end end
# See https://github.com/michaeldv/awesome_print/issues/85 it "handle array grep when a method is defined in C and thus doesn't have a binding" do
if RUBY_VERSION >= "1.8.7" arr = (0..6).to_a
it "handle array grep when a method is defined in C and thus doesn't have a binding" do grepped = arr.grep(1..4, &:succ)
arr = (0..6).to_a expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ 2, 3, 4, 5 ]')
grepped = arr.grep(1..4, &:succ)
expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ 2, 3, 4, 5 ]')
end
end end
it "returns value passed as a parameter" do it "returns value passed as a parameter" do
@ -177,11 +174,7 @@ EOS
it "shoud not raise ArgumentError when formatting HTML" do it "shoud not raise ArgumentError when formatting HTML" do
out = "hello".ai(:color => { :string => :red }, :html => true) out = "hello".ai(:color => { :string => :red }, :html => true)
if RUBY_VERSION >= "1.9" expect(out).to eq(%Q|<pre>[red]<kbd style="color:red">&quot;hello&quot;</kbd>[/red]</pre>|)
expect(out).to eq(%Q|<pre>[red]<kbd style="color:red">&quot;hello&quot;</kbd>[/red]</pre>|)
else
expect(out).to eq(%Q|<pre>[red]&quot;hello&quot;[/red]</pre>|)
end
end end
it "shoud not raise ArgumentError when formatting HTML (shade color)" do it "shoud not raise ArgumentError when formatting HTML (shade color)" do