-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
60 changed files
with
2,201 additions
and
1,777 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.