2014-03-14 00:31:24 -04:00
|
|
|
require_relative 'helper'
|
2011-08-23 03:54:58 -04:00
|
|
|
|
|
|
|
describe Pry do
|
|
|
|
describe "output failsafe" do
|
|
|
|
after do
|
|
|
|
Pry.config.print = Pry::DEFAULT_PRINT
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should catch serialization exceptions" do
|
|
|
|
Pry.config.print = lambda { |*a| raise "catch-22" }
|
|
|
|
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 18:26:47 -04:00
|
|
|
expect { mock_pry("1") }.to_not raise_error
|
2011-08-23 03:54:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should display serialization exceptions" do
|
|
|
|
Pry.config.print = lambda { |*a| raise "catch-22" }
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("1")).to match(/\(pry\) output error: #<RuntimeError: catch-22>/)
|
2011-08-23 03:54:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should catch errors serializing exceptions" do
|
|
|
|
Pry.config.print = lambda do |*a|
|
|
|
|
raise Exception.new("catch-22").tap{ |e| class << e; def inspect; raise e; end; end }
|
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("1")).to match(/\(pry\) output error: failed to show result/)
|
2011-08-23 03:54:58 -04:00
|
|
|
end
|
|
|
|
end
|
2011-08-24 04:07:52 -04:00
|
|
|
|
|
|
|
describe "DEFAULT_PRINT" do
|
|
|
|
it "should output the right thing" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("[1]")).to match(/^=> \[1\]/)
|
2012-10-15 04:20:18 -04:00
|
|
|
end
|
|
|
|
|
2013-01-14 12:31:48 -05:00
|
|
|
it "should include the =>" do
|
2014-02-05 08:29:25 -05:00
|
|
|
pry = Pry.new
|
2012-11-08 02:58:18 -05:00
|
|
|
accumulator = StringIO.new
|
2014-04-30 05:08:29 -04:00
|
|
|
pry.config.output = accumulator
|
2014-02-05 08:29:25 -05:00
|
|
|
pry.config.print.call(accumulator, [1], pry)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(accumulator.string).to eq("=> \[1\]\n")
|
2011-08-24 04:07:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not be phased by un-inspectable things" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("class NastyClass; undef pretty_inspect; end", "NastyClass.new")).to match(/#<.*NastyClass:0x.*?>/)
|
2011-08-24 04:07:52 -04:00
|
|
|
end
|
2013-10-23 17:51:36 -04:00
|
|
|
|
|
|
|
it "doesn't leak colour for object literals" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("Object.new")).to match(/=> #<Object:0x[a-z0-9]+>\n/)
|
2013-10-23 17:51:36 -04:00
|
|
|
end
|
2011-08-24 04:07:52 -04:00
|
|
|
end
|
2012-10-21 01:54:58 -04:00
|
|
|
|
2014-02-05 08:29:25 -05:00
|
|
|
describe "output_prefix" do
|
|
|
|
it "should be able to change output_prefix" do
|
|
|
|
pry = Pry.new
|
|
|
|
accumulator = StringIO.new
|
2014-04-30 05:08:29 -04:00
|
|
|
pry.config.output = accumulator
|
2014-02-05 08:29:25 -05:00
|
|
|
pry.config.output_prefix = "-> "
|
|
|
|
pry.config.print.call(accumulator, [1], pry)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(accumulator.string).to eq("-> \[1\]\n")
|
2014-02-05 08:29:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-28 18:21:14 -05:00
|
|
|
describe "color" do
|
|
|
|
before do
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.color = true
|
2012-11-28 18:21:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.color = false
|
2012-11-28 18:21:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should colorize strings as though they were ruby" do
|
2014-02-05 08:29:25 -05:00
|
|
|
pry = Pry.new
|
2012-11-28 18:21:14 -05:00
|
|
|
accumulator = StringIO.new
|
2013-10-29 00:36:00 -04:00
|
|
|
colorized = CodeRay.scan("[1]", :ruby).term
|
2014-04-30 05:08:29 -04:00
|
|
|
pry.config.output = accumulator
|
2014-02-05 08:29:25 -05:00
|
|
|
pry.config.print.call(accumulator, [1], pry)
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(accumulator.string).to eq("=> #{colorized}\n")
|
2012-11-28 18:21:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not colorize strings that already include color" do
|
2014-02-05 08:29:25 -05:00
|
|
|
pry = Pry.new
|
2012-11-28 18:21:14 -05:00
|
|
|
f = Object.new
|
|
|
|
def f.inspect
|
|
|
|
"\e[1;31mFoo\e[0m"
|
|
|
|
end
|
|
|
|
accumulator = StringIO.new
|
2014-04-30 05:08:29 -04:00
|
|
|
pry.config.output = accumulator
|
2014-02-05 08:29:25 -05:00
|
|
|
pry.config.print.call(accumulator, f, pry)
|
2012-11-28 18:21:14 -05:00
|
|
|
# We add an extra \e[0m to prevent color leak
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(accumulator.string).to eq("=> \e[1;31mFoo\e[0m\e[0m\n")
|
2012-11-28 18:21:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-21 01:54:58 -04:00
|
|
|
describe "output suppression" do
|
|
|
|
before do
|
|
|
|
@t = pry_tester
|
|
|
|
end
|
|
|
|
it "should normally output the result" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("1 + 2")).to eq("=> 3\n")
|
2012-10-21 01:54:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not output anything if the input ends with a semicolon" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("1 + 2;")).to eq("")
|
2012-10-21 01:54:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should output something if the input ends with a comment" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("1 + 2 # basic addition")).to eq("=> 3\n")
|
2012-10-21 01:54:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not output something if the input is only a comment" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(mock_pry("# basic addition")).to eq("")
|
2012-10-21 01:54:58 -04:00
|
|
|
end
|
|
|
|
end
|
2015-08-23 22:01:56 -04:00
|
|
|
|
|
|
|
describe "custom non-IO object as $stdout" do
|
|
|
|
it "does not crash pry" do
|
|
|
|
old_stdout = $stdout
|
|
|
|
custom_io = Class.new { def write(*) end }.new
|
|
|
|
pry_eval = PryTester.new(binding)
|
|
|
|
expect(pry_eval.eval("$stdout = custom_io", ":ok")).to eq(:ok)
|
|
|
|
$stdout = old_stdout
|
|
|
|
end
|
|
|
|
end
|
2011-08-23 03:54:58 -04:00
|
|
|
end
|