2014-03-14 04:31:24 +00:00
|
|
|
require_relative 'helper'
|
2011-10-05 17:04:44 +00:00
|
|
|
|
|
|
|
# Please keep in mind that any hash signs ("#") in the heredoc strings are
|
|
|
|
# placed on purpose. Without these editors might remove the whitespace on empty
|
|
|
|
# lines.
|
|
|
|
describe Pry::Indent do
|
|
|
|
before do
|
|
|
|
@indent = Pry::Indent.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should indent an array' do
|
|
|
|
input = "array = [\n10,\n15\n]"
|
|
|
|
output = "array = [\n 10,\n 15\n]"
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2011-10-05 17:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should indent a hash' do
|
|
|
|
input = "hash = {\n:name => 'Ruby'\n}"
|
|
|
|
output = "hash = {\n :name => 'Ruby'\n}"
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2011-10-05 17:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should indent a function' do
|
|
|
|
input = "def\nreturn 10\nend"
|
|
|
|
output = "def\n return 10\nend"
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2011-10-05 17:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should indent a module and class' do
|
|
|
|
input = "module Foo\n# Hello world\nend"
|
|
|
|
output = "module Foo\n # Hello world\nend"
|
|
|
|
input_class = "class Foo\n# Hello world\nend"
|
|
|
|
output_class = "class Foo\n # Hello world\nend"
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
|
|
|
@indent.indent(input_class).should eq output_class
|
2011-10-05 17:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should indent separate lines' do
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent('def foo').should eq 'def foo'
|
|
|
|
@indent.indent('return 10').should eq ' return 10'
|
|
|
|
@indent.indent('end').should eq 'end'
|
2011-10-08 13:30:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not indent single line statements' do
|
|
|
|
input = <<TXT.strip
|
|
|
|
def hello; end
|
|
|
|
puts "Hello"
|
|
|
|
TXT
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq input
|
2011-10-08 13:30:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should handle multiple open and closing tokens on a line' do
|
|
|
|
input = <<TXT.strip
|
|
|
|
[10, 15].each do |num|
|
|
|
|
puts num
|
|
|
|
end
|
|
|
|
TXT
|
|
|
|
|
|
|
|
output = <<TXT.strip
|
|
|
|
[10, 15].each do |num|
|
|
|
|
puts num
|
|
|
|
end
|
|
|
|
TXT
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2011-10-05 17:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should properly indent nested code' do
|
|
|
|
input = <<TXT.strip
|
|
|
|
module A
|
|
|
|
module B
|
|
|
|
class C
|
|
|
|
attr_accessor :test
|
|
|
|
# keep
|
|
|
|
def number
|
|
|
|
return 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TXT
|
|
|
|
|
|
|
|
output = <<TXT.strip
|
|
|
|
module A
|
|
|
|
module B
|
|
|
|
class C
|
|
|
|
attr_accessor :test
|
|
|
|
# keep
|
|
|
|
def number
|
|
|
|
return 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TXT
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2011-10-05 17:04:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should indent statements such as if, else, etc' do
|
|
|
|
input = <<TXT.strip
|
2011-10-08 13:30:06 +00:00
|
|
|
if a == 10
|
2011-10-05 17:04:44 +00:00
|
|
|
#
|
2011-10-08 13:30:06 +00:00
|
|
|
elsif a == 15
|
2011-10-05 17:04:44 +00:00
|
|
|
#
|
|
|
|
else
|
|
|
|
#
|
|
|
|
end
|
|
|
|
#
|
|
|
|
while true
|
|
|
|
#
|
|
|
|
end
|
|
|
|
#
|
|
|
|
for num in [10, 15, 20]
|
|
|
|
#
|
|
|
|
end
|
|
|
|
#
|
|
|
|
for num in [10, 15, 20] do
|
|
|
|
#
|
|
|
|
end
|
|
|
|
TXT
|
|
|
|
|
|
|
|
output = <<TXT.strip
|
2011-10-08 13:30:06 +00:00
|
|
|
if a == 10
|
2011-10-05 17:04:44 +00:00
|
|
|
#
|
2011-10-08 13:30:06 +00:00
|
|
|
elsif a == 15
|
2011-10-05 17:04:44 +00:00
|
|
|
#
|
|
|
|
else
|
|
|
|
#
|
|
|
|
end
|
|
|
|
#
|
|
|
|
while true
|
|
|
|
#
|
|
|
|
end
|
|
|
|
#
|
|
|
|
for num in [10, 15, 20]
|
|
|
|
#
|
|
|
|
end
|
|
|
|
#
|
|
|
|
for num in [10, 15, 20] do
|
|
|
|
#
|
|
|
|
end
|
|
|
|
TXT
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2011-10-05 17:04:44 +00:00
|
|
|
end
|
2011-10-09 07:04:54 +00:00
|
|
|
|
2012-12-28 15:37:55 +00:00
|
|
|
it "should correctly handle while <foo> do" do
|
|
|
|
input = "while 5 do\n5\nend"
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq "while 5 do\n 5\nend"
|
2012-12-28 15:37:55 +00:00
|
|
|
end
|
|
|
|
|
2011-10-09 08:39:42 +00:00
|
|
|
it "should ident case statements" do
|
|
|
|
input = <<TXT.strip
|
|
|
|
case foo
|
|
|
|
when 1
|
|
|
|
2
|
|
|
|
when 2
|
|
|
|
if 3
|
|
|
|
4
|
|
|
|
end
|
|
|
|
when 5
|
|
|
|
#
|
|
|
|
else
|
|
|
|
#
|
|
|
|
end
|
|
|
|
TXT
|
|
|
|
|
|
|
|
output = <<TXT.strip
|
|
|
|
case foo
|
|
|
|
when 1
|
|
|
|
2
|
|
|
|
when 2
|
|
|
|
if 3
|
|
|
|
4
|
|
|
|
end
|
|
|
|
when 5
|
|
|
|
#
|
|
|
|
else
|
|
|
|
#
|
|
|
|
end
|
|
|
|
TXT
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2011-10-09 08:39:42 +00:00
|
|
|
end
|
|
|
|
|
2011-10-09 07:04:54 +00:00
|
|
|
it "should indent correctly with nesting" do
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent("[[\n[]]\n]").should eq "[[\n []]\n]"
|
|
|
|
@indent.reset.indent("[[\n[]]\n]").should eq "[[\n []]\n]"
|
|
|
|
@indent.reset.indent("[[{\n[] =>\n[]}]\n]").should eq "[[{\n [] =>\n []}]\n]"
|
2011-10-09 07:04:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not indent single-line ifs" do
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent("foo if bar\n#").should eq "foo if bar\n#"
|
|
|
|
@indent.reset.indent("foo() if bar\n#").should eq "foo() if bar\n#"
|
|
|
|
@indent.reset.indent("foo 'hi' if bar\n#").should eq "foo 'hi' if bar\n#"
|
|
|
|
@indent.reset.indent("foo 1 while bar\n#").should eq "foo 1 while bar\n#"
|
|
|
|
@indent.reset.indent("$foo if false\n#").should eq "$foo if false\n#"
|
|
|
|
@indent.reset.indent("@foo if false\n#").should eq "@foo if false\n#"
|
|
|
|
@indent.reset.indent("@@foo if false\n#").should eq "@@foo if false\n#"
|
|
|
|
@indent.reset.indent("super if true\n#").should eq "super if true\n#"
|
|
|
|
@indent.reset.indent("true if false\n#").should eq "true if false\n#"
|
|
|
|
@indent.reset.indent("String if false\n#").should eq "String if false\n#"
|
2011-10-09 07:04:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should indent cunningly disguised ifs" do
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent("{1 => if bar\n#").should eq "{1 => if bar\n #"
|
|
|
|
@indent.reset.indent("foo(if bar\n#").should eq "foo(if bar\n #"
|
|
|
|
@indent.reset.indent("bar(baz, if bar\n#").should eq "bar(baz, if bar\n #"
|
|
|
|
@indent.reset.indent("[if bar\n#").should eq "[if bar\n #"
|
|
|
|
@indent.reset.indent("true; while bar\n#").should eq "true; while bar\n #"
|
2011-10-09 07:04:54 +00:00
|
|
|
end
|
2011-10-10 04:34:16 +00:00
|
|
|
|
|
|
|
it "should differentiate single/multi-line unless" do
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent("foo unless bar\nunless foo\nbar\nend").should eq "foo unless bar\nunless foo\n bar\nend"
|
2011-10-10 04:34:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not indent single/multi-line until" do
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent("%w{baz} until bar\nuntil foo\nbar\nend").should eq "%w{baz} until bar\nuntil foo\n bar\nend"
|
2011-10-10 04:34:16 +00:00
|
|
|
end
|
2011-10-26 03:57:19 +00:00
|
|
|
|
|
|
|
it "should indent begin rescue end" do
|
|
|
|
input = <<INPUT.strip
|
|
|
|
begin
|
|
|
|
doo :something => :wrong
|
|
|
|
rescue => e
|
|
|
|
doit :right
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
output = <<OUTPUT.strip
|
|
|
|
begin
|
|
|
|
doo :something => :wrong
|
|
|
|
rescue => e
|
|
|
|
doit :right
|
|
|
|
end
|
2012-04-22 07:22:49 +00:00
|
|
|
OUTPUT
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2012-04-22 07:22:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not indent inside strings" do
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(%(def a\n"foo\nbar"\n end)).should eq %(def a\n "foo\nbar"\nend)
|
|
|
|
@indent.indent(%(def a\nputs %w(foo\nbar), 'foo\nbar'\n end)).should eq %(def a\n puts %w(foo\nbar), 'foo\nbar'\nend)
|
2012-04-22 07:22:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not indent inside HEREDOCs" do
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(%(def a\nputs <<FOO\n bar\nFOO\nbaz\nend)).should eq %(def a\n puts <<FOO\n bar\nFOO\n baz\nend)
|
|
|
|
@indent.indent(%(def a\nputs <<-'^^'\n bar\n\t^^\nbaz\nend)).should eq %(def a\n puts <<-'^^'\n bar\n\t^^\n baz\nend)
|
2012-04-22 07:22:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not indent nested HEREDOCs" do
|
|
|
|
input = <<INPUT.strip
|
|
|
|
def a
|
|
|
|
puts <<FOO, <<-BAR, "baz", <<-':p'
|
|
|
|
foo
|
|
|
|
FOO
|
|
|
|
bar
|
|
|
|
BAR
|
|
|
|
tongue
|
|
|
|
:p
|
|
|
|
puts :p
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
|
|
|
|
output = <<OUTPUT.strip
|
|
|
|
def a
|
|
|
|
puts <<FOO, <<-BAR, "baz", <<-':p'
|
|
|
|
foo
|
|
|
|
FOO
|
|
|
|
bar
|
|
|
|
BAR
|
|
|
|
tongue
|
|
|
|
:p
|
|
|
|
puts :p
|
|
|
|
end
|
2011-10-26 03:57:19 +00:00
|
|
|
OUTPUT
|
|
|
|
|
2015-01-22 21:52:20 +00:00
|
|
|
@indent.indent(input).should eq output
|
2011-10-26 03:57:19 +00:00
|
|
|
end
|
2012-07-28 08:39:41 +00:00
|
|
|
|
|
|
|
describe "nesting" do
|
2012-12-09 19:42:41 +00:00
|
|
|
test = File.read("spec/fixtures/example_nesting.rb")
|
2012-07-28 08:39:41 +00:00
|
|
|
|
|
|
|
test.lines.each_with_index do |line, i|
|
|
|
|
result = line.split("#").last.strip
|
|
|
|
if result == ""
|
|
|
|
it "should fail to parse nesting on line #{i + 1} of example_nesting.rb" do
|
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 22:26:47 +00:00
|
|
|
expect { Pry::Indent.nesting_at(test, i + 1) }.to raise_error Pry::Indent::UnparseableNestingError
|
2012-07-28 08:39:41 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
it "should parse nesting on line #{i + 1} of example_nesting.rb" do
|
2015-01-22 21:52:20 +00:00
|
|
|
Pry::Indent.nesting_at(test, i + 1).should eq eval(result)
|
2012-07-28 08:39:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-05 17:04:44 +00:00
|
|
|
end
|