Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,16 @@ def parse
end
end

# Return encoding of the source.
def encoding
result.source.encoding
end

# Return true if parsed source ended by `__END__`.
def end_seen?
!!result.data_loc
end

##########################################################################
# Visitor methods
##########################################################################
Expand Down
22 changes: 22 additions & 0 deletions test/prism/ruby/ripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@ def test_encoding
assert_equal(Ripper.sexp(source), Translation::Ripper.sexp(source))
end

def test_encoding_method
source = "foo"
assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding)

source = "foo".b
assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding)

source = "# encoding: shift_jis"
assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding)

source = "# encoding: shift_jis".b
assert_equal(Ripper.new(source).tap(&:parse).encoding, Prism::Translation::Ripper.new(source).tap(&:parse).encoding)
end

def test_end_seen
source = ""
assert_equal(Ripper.new(source).tap(&:parse).end_seen?, Prism::Translation::Ripper.new(source).tap(&:parse).end_seen?)

source = "__END__"
assert_equal(Ripper.new(source).tap(&:parse).end_seen?, Prism::Translation::Ripper.new(source).tap(&:parse).end_seen?)
end

def test_sexp_coercion
string_like = Object.new
def string_like.to_str
Expand Down