Skip to content

Commit

Permalink
Tests overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed May 30, 2024
1 parent 0dd4c1b commit 2a29c8e
Show file tree
Hide file tree
Showing 60 changed files with 2,201 additions and 1,777 deletions.
25 changes: 23 additions & 2 deletions lib/prism/ffi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ def self.with_file(filepath)

class << self
# Mirror the Prism.dump API by using the serialization API.
def dump(code, **options)
LibRubyParser::PrismString.with_string(code) { |string| dump_common(string, options) }
def dump(source, **options)
LibRubyParser::PrismString.with_string(source) { |string| dump_common(string, options) }
end

# Mirror the Prism.dump_file API by using the serialization API.
Expand Down Expand Up @@ -302,6 +302,27 @@ def parse_file_failure?(filepath, **options)
!parse_file_success?(filepath, **options)
end

# Mirror the Prism.profile API by using the serialization API.
def profile(source, **options)
LibRubyParser::PrismString.with_string(source) do |string|
LibRubyParser::PrismBuffer.with do |buffer|
LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
nil
end
end
end

# Mirror the Prism.profile_file API by using the serialization API.
def profile_file(filepath, **options)
LibRubyParser::PrismString.with_file(filepath) do |string|
LibRubyParser::PrismBuffer.with do |buffer|
options[:filepath] = filepath
LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
nil
end
end
end

private

def dump_common(string, options) # :nodoc:
Expand Down
4 changes: 2 additions & 2 deletions lib/prism/translation/ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ def visit_constant_path_or_write_node(node)
def visit_constant_path_target_node(node)
inner =
if node.parent.nil?
s(node, :colon3, node.child.name)
s(node, :colon3, node.name)
else
s(node, :colon2, visit(node.parent), node.child.name)
s(node, :colon2, visit(node.parent), node.name)
end

s(node, :const, inner)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "test_helper"
require_relative "../test_helper"

module Prism
class CommandLineTest < TestCase
Expand Down Expand Up @@ -67,26 +67,26 @@ def test_command_line_e
end

def test_command_line_x_implicit
result = Prism.parse(<<~RUBY)
result = Prism.parse_statement(<<~RUBY)
#!/bin/bash
exit 1
#!/usr/bin/env ruby
1
RUBY

assert_kind_of IntegerNode, result.value.statements.body.first
assert_kind_of IntegerNode, result
end

def test_command_line_x_explicit
result = Prism.parse(<<~RUBY, command_line: "x")
result = Prism.parse_statement(<<~RUBY, command_line: "x")
exit 1
#!/usr/bin/env ruby
1
RUBY

assert_kind_of IntegerNode, result.value.statements.body.first
assert_kind_of IntegerNode, result
end

def test_command_line_x_implicit_fail
Expand Down
56 changes: 56 additions & 0 deletions test/prism/api/dump_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

return if ENV["PRISM_BUILD_MINIMAL"]

require_relative "../test_helper"

module Prism
class DumpTest < TestCase
Fixture.each do |fixture|
define_method(fixture.test_name) { assert_dump(fixture) }
end

def test_dump
filepath = __FILE__
source = File.read(filepath, binmode: true, external_encoding: Encoding::UTF_8)

assert_equal Prism.lex(source, filepath: filepath).value, Prism.lex_file(filepath).value
assert_equal Prism.dump(source, filepath: filepath), Prism.dump_file(filepath)

serialized = Prism.dump(source, filepath: filepath)
ast1 = Prism.load(source, serialized).value
ast2 = Prism.parse(source, filepath: filepath).value
ast3 = Prism.parse_file(filepath).value

assert_equal_nodes ast1, ast2
assert_equal_nodes ast2, ast3
end

def test_dump_file
assert_nothing_raised do
Prism.dump_file(__FILE__)
end

error = assert_raise Errno::ENOENT do
Prism.dump_file("idontexist.rb")
end

assert_equal "No such file or directory - idontexist.rb", error.message

assert_raise TypeError do
Prism.dump_file(nil)
end
end

private

def assert_dump(fixture)
source = fixture.read

result = Prism.parse(source, filepath: fixture.path)
dumped = Prism.dump(source, filepath: fixture.path)

assert_equal_nodes(result.value, Prism.load(source, dumped).value)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "test_helper"
require_relative "../test_helper"

module Prism
class ParseCommentsTest < TestCase
Expand All @@ -17,5 +17,17 @@ def test_parse_file_comments
assert_kind_of Array, comments
assert_equal 1, comments.length
end

def test_parse_file_comments_error
error = assert_raise Errno::ENOENT do
Prism.parse_file_comments("idontexist.rb")
end

assert_equal "No such file or directory - idontexist.rb", error.message

assert_raise TypeError do
Prism.parse_file_comments(nil)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative "test_helper"
require "stringio"
require_relative "../test_helper"

module Prism
class ParseStreamTest < TestCase
Expand All @@ -10,24 +9,24 @@ def test_single_line
result = Prism.parse_stream(io)

assert result.success?
assert_kind_of Prism::CallNode, result.value.statements.body.first
assert_kind_of Prism::CallNode, result.statement
end

def test_multi_line
io = StringIO.new("1 + 2\n3 + 4")
result = Prism.parse_stream(io)

assert result.success?
assert_kind_of Prism::CallNode, result.value.statements.body.first
assert_kind_of Prism::CallNode, result.value.statements.body.last
assert_kind_of Prism::CallNode, result.statement
assert_kind_of Prism::CallNode, result.statement
end

def test_multi_read
io = StringIO.new("a" * 4096 * 4)
result = Prism.parse_stream(io)

assert result.success?
assert_kind_of Prism::CallNode, result.value.statements.body.first
assert_kind_of Prism::CallNode, result.statement
end

def test___END__
Expand Down
16 changes: 16 additions & 0 deletions test/prism/api/parse_success_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative "../test_helper"

module Prism
class ParseSuccessTest < TestCase
def test_parse_success?
assert Prism.parse_success?("1")
refute Prism.parse_success?("<>")
end

def test_parse_file_success?
assert Prism.parse_file_success?(__FILE__)
end
end
end
66 changes: 66 additions & 0 deletions test/prism/api/parse_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require_relative "../test_helper"

module Prism
class ParseTest < TestCase
def test_parse_empty_string
result = Prism.parse("")
assert_equal [], result.value.statements.body
end

def test_parse_takes_file_path
filepath = "filepath.rb"
result = Prism.parse("def foo; __FILE__; end", filepath: filepath)

assert_equal filepath, find_source_file_node(result.value).filepath
end

def test_parse_takes_line
line = 4
result = Prism.parse("def foo\n __FILE__\nend", line: line)

assert_equal line, result.value.location.start_line
assert_equal line + 1, find_source_file_node(result.value).location.start_line

result = Prism.parse_lex("def foo\n __FILE__\nend", line: line)
assert_equal line, result.value.first.location.start_line
end

def test_parse_takes_negative_lines
line = -2
result = Prism.parse("def foo\n __FILE__\nend", line: line)

assert_equal line, result.value.location.start_line
assert_equal line + 1, find_source_file_node(result.value).location.start_line

result = Prism.parse_lex("def foo\n __FILE__\nend", line: line)
assert_equal line, result.value.first.location.start_line
end

def test_parse_file
node = Prism.parse_file(__FILE__).value
assert_kind_of ProgramNode, node

error = assert_raise Errno::ENOENT do
Prism.parse_file("idontexist.rb")
end

assert_equal "No such file or directory - idontexist.rb", error.message

assert_raise TypeError do
Prism.parse_file(nil)
end
end

private

def find_source_file_node(program)
queue = [program]
while (node = queue.shift)
return node if node.is_a?(SourceFileNode)
queue.concat(node.compact_child_nodes)
end
end
end
end
2 changes: 1 addition & 1 deletion test/prism/bom_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Don't bother checking this on these engines, this is such a specific Ripper
# test.
return if RUBY_ENGINE == "jruby" || RUBY_ENGINE == "truffleruby"
return if RUBY_ENGINE != "ruby"

require_relative "test_helper"

Expand Down
Loading

0 comments on commit 2a29c8e

Please sign in to comment.