2017-02-05 02:54:32 -05:00
|
|
|
# frozen_string_literal: true
|
2011-09-17 10:34:13 -04:00
|
|
|
begin
|
|
|
|
require 'ripper'
|
2019-05-08 20:23:44 -04:00
|
|
|
require 'stringio'
|
2011-09-17 10:34:13 -04:00
|
|
|
require 'test/unit'
|
|
|
|
ripper_test = true
|
|
|
|
module TestRipper; end
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestRipper::Ripper < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@ripper = Ripper.new '1 + 1'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_column
|
|
|
|
assert_nil @ripper.column
|
|
|
|
end
|
|
|
|
|
2017-09-14 06:53:47 -04:00
|
|
|
def test_state
|
|
|
|
assert_nil @ripper.state
|
|
|
|
end
|
|
|
|
|
2011-09-17 10:34:13 -04:00
|
|
|
def test_encoding
|
2012-11-05 19:49:57 -05:00
|
|
|
assert_equal Encoding::UTF_8, @ripper.encoding
|
2014-09-30 09:25:28 -04:00
|
|
|
ripper = Ripper.new('# coding: iso-8859-15')
|
|
|
|
ripper.parse
|
|
|
|
assert_equal Encoding::ISO_8859_15, ripper.encoding
|
2014-12-01 12:53:02 -05:00
|
|
|
ripper = Ripper.new('# -*- coding: iso-8859-15 -*-')
|
|
|
|
ripper.parse
|
|
|
|
assert_equal Encoding::ISO_8859_15, ripper.encoding
|
2011-09-17 10:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_end_seen_eh
|
2014-09-30 09:25:28 -04:00
|
|
|
@ripper.parse
|
|
|
|
assert_not_predicate @ripper, :end_seen?
|
|
|
|
ripper = Ripper.new('__END__')
|
|
|
|
ripper.parse
|
|
|
|
assert_predicate ripper, :end_seen?
|
2011-09-17 10:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_filename
|
|
|
|
assert_equal '(ripper)', @ripper.filename
|
2017-02-05 02:54:32 -05:00
|
|
|
filename = "ripper".dup
|
2014-03-24 09:19:57 -04:00
|
|
|
ripper = Ripper.new("", filename)
|
|
|
|
filename.clear
|
|
|
|
assert_equal "ripper", ripper.filename
|
2011-09-17 10:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_lineno
|
|
|
|
assert_nil @ripper.lineno
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse
|
2014-09-30 09:25:28 -04:00
|
|
|
assert_nil @ripper.parse
|
2011-09-17 10:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_yydebug
|
2014-09-30 09:25:28 -04:00
|
|
|
assert_not_predicate @ripper, :yydebug
|
2011-09-17 10:34:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_yydebug_equals
|
|
|
|
@ripper.yydebug = true
|
|
|
|
|
2014-09-30 09:25:28 -04:00
|
|
|
assert_predicate @ripper, :yydebug
|
2011-09-17 10:34:13 -04:00
|
|
|
end
|
2016-01-04 02:59:55 -05:00
|
|
|
|
2019-03-18 22:35:50 -04:00
|
|
|
def test_yydebug_ident
|
|
|
|
out = StringIO.new
|
|
|
|
ripper = Ripper.new 'test_xxxx'
|
|
|
|
ripper.yydebug = true
|
|
|
|
ripper.debug_output = out
|
|
|
|
ripper.parse
|
|
|
|
assert_include out.string[/.*"local variable or method".*/], 'test_xxxx'
|
|
|
|
end
|
|
|
|
|
2021-08-14 12:33:31 -04:00
|
|
|
def test_yydebug_string
|
|
|
|
out = StringIO.new
|
|
|
|
ripper = Ripper.new '"woot"'
|
|
|
|
ripper.yydebug = true
|
|
|
|
ripper.debug_output = out
|
|
|
|
ripper.parse
|
|
|
|
assert_include out.string[/.*"literal content".*/], 'woot'
|
|
|
|
end
|
|
|
|
|
2016-01-04 02:59:55 -05:00
|
|
|
def test_regexp_with_option
|
|
|
|
bug11932 = '[ruby-core:72638] [Bug #11932]'
|
2017-02-05 02:54:32 -05:00
|
|
|
src = '/[\xC0-\xF0]/u'.dup.force_encoding(Encoding::UTF_8)
|
2016-01-04 02:59:55 -05:00
|
|
|
ripper = Ripper.new(src)
|
|
|
|
ripper.parse
|
|
|
|
assert_predicate(ripper, :error?)
|
2017-02-05 02:54:32 -05:00
|
|
|
src = '/[\xC0-\xF0]/n'.dup.force_encoding(Encoding::UTF_8)
|
2016-01-04 02:59:55 -05:00
|
|
|
ripper = Ripper.new(src)
|
|
|
|
ripper.parse
|
|
|
|
assert_not_predicate(ripper, :error?, bug11932)
|
|
|
|
end
|
2016-08-03 08:31:25 -04:00
|
|
|
|
|
|
|
def test_regexp_enc_error
|
|
|
|
assert_separately(%w[-rripper], "#{<<-"begin;"}\n#{<<-"end;"}")
|
|
|
|
begin;
|
|
|
|
bug12651 = '[ruby-core:76397] [Bug #12651]'
|
|
|
|
src = <<-END
|
|
|
|
<%- @title = '\u{5bff 9650 7121}' -%>
|
|
|
|
<%- content_for :foo, render(partial: 'bar', locals: {baz: 2}) -%>
|
|
|
|
|
|
|
|
<div class="dead beef">
|
|
|
|
<h2 class="dead beef">\u{5bff 9650 7121}</h2>
|
|
|
|
</div>
|
|
|
|
<div class="dead beef">\u{5bff 9650 7121 3002}<br class="dead beef">\u{5bff 9650 7121 3002}</div>
|
|
|
|
|
|
|
|
<div class="dead beef">
|
|
|
|
<div class="dead beef">
|
|
|
|
<label class="dead beef">\u{5bff 9650 7121}</label>
|
|
|
|
<div class="dead beef">
|
|
|
|
<div class="dead beef"><%= @baz %></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
END
|
|
|
|
assert_nil(Ripper.sexp(src), bug12651)
|
|
|
|
end;
|
|
|
|
end
|
2016-09-26 17:12:12 -04:00
|
|
|
|
|
|
|
# https://bugs.jruby.org/4176
|
|
|
|
def test_dedent_string
|
2017-02-05 02:54:32 -05:00
|
|
|
col = Ripper.dedent_string ' hello'.dup, 0
|
2016-09-26 17:12:12 -04:00
|
|
|
assert_equal 0, col
|
2017-02-05 02:54:32 -05:00
|
|
|
col = Ripper.dedent_string ' hello'.dup, 2
|
2016-09-26 17:12:12 -04:00
|
|
|
assert_equal 2, col
|
2017-02-05 02:54:32 -05:00
|
|
|
col = Ripper.dedent_string ' hello'.dup, 4
|
2016-09-26 17:12:12 -04:00
|
|
|
assert_equal 2, col
|
|
|
|
|
|
|
|
# lexing a squiggly heredoc triggers Ripper#dedent_string use
|
|
|
|
src = <<-END
|
2016-09-27 08:13:52 -04:00
|
|
|
puts <<~end
|
2016-09-26 17:12:12 -04:00
|
|
|
hello
|
|
|
|
end
|
|
|
|
END
|
|
|
|
|
|
|
|
assert_nothing_raised { Ripper.lex src }
|
|
|
|
end
|
2016-12-07 19:45:13 -05:00
|
|
|
|
|
|
|
class TestInput < self
|
|
|
|
Input = Struct.new(:lines) do
|
|
|
|
def gets
|
|
|
|
lines.shift
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@ripper = Ripper.new(Input.new(["1 + 1"]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_gets
|
|
|
|
ripper = assert_nothing_raised {Ripper.new(Input.new([0]))}
|
|
|
|
assert_raise(TypeError) {ripper.parse}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-17 10:34:13 -04:00
|
|
|
end if ripper_test
|