Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Fix CI for TruffleRuby #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2023
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
3 changes: 2 additions & 1 deletion lib/syntax_tree/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def parse_comments(statements, index)
program =
SyntaxTree.parse(SyntaxTree.read(File.expand_path("node.rb", __dir__)))

main_statements = program.statements.body.last.bodystmt.statements.body
program_statements = program.statements
main_statements = program_statements.body.last.bodystmt.statements.body
main_statements.each_with_index do |main_statement, main_statement_index|
# Ensure we are only looking at class declarations.
next unless main_statement.is_a?(SyntaxTree::ClassDeclaration)
Expand Down
70 changes: 37 additions & 33 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
# frozen_string_literal: true

require "simplecov"
SimpleCov.start do
add_filter("idempotency_test.rb") unless ENV["CI"]
add_group("lib", "lib")
add_group("test", "test")
unless RUBY_ENGINE == "truffleruby"
require "simplecov"
SimpleCov.start do
add_filter("idempotency_test.rb") unless ENV["CI"]
add_group("lib", "lib")
add_group("test", "test")
end
end

$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
require "syntax_tree"
require "syntax_tree/cli"

# Here we are going to establish type verification whenever a new node is
# created. We do this through the reflection module, which in turn parses the
# source code of the node classes.
require "syntax_tree/reflection"
SyntaxTree::Reflection.nodes.each do |name, node|
next if name == :Statements

clazz = SyntaxTree.const_get(name)
parameters = clazz.instance_method(:initialize).parameters

# First, verify that all of the parameters listed in the list of attributes.
# If there are any parameters that aren't listed in the attributes, then
# something went wrong with the parsing in the reflection module.
raise unless (parameters.map(&:last) - node.attributes.keys).empty?

# Now we're going to use an alias chain to redefine the initialize method to
# include type checking.
clazz.alias_method(:initialize_without_verify, :initialize)
clazz.define_method(:initialize) do |**kwargs|
kwargs.each do |kwarg, value|
attribute = node.attributes.fetch(kwarg)

unless attribute.type === value
raise TypeError,
"invalid type for #{name}##{kwarg}, expected " \
"#{attribute.type.inspect}, got #{value.inspect}"
unless RUBY_ENGINE == "truffleruby"
# Here we are going to establish type verification whenever a new node is
# created. We do this through the reflection module, which in turn parses the
# source code of the node classes.
require "syntax_tree/reflection"
SyntaxTree::Reflection.nodes.each do |name, node|
next if name == :Statements

clazz = SyntaxTree.const_get(name)
parameters = clazz.instance_method(:initialize).parameters

# First, verify that all of the parameters listed in the list of attributes.
# If there are any parameters that aren't listed in the attributes, then
# something went wrong with the parsing in the reflection module.
raise unless (parameters.map(&:last) - node.attributes.keys).empty?

# Now we're going to use an alias chain to redefine the initialize method to
# include type checking.
clazz.alias_method(:initialize_without_verify, :initialize)
clazz.define_method(:initialize) do |**kwargs|
kwargs.each do |kwarg, value|
attribute = node.attributes.fetch(kwarg)

unless attribute.type === value
raise TypeError,
"invalid type for #{name}##{kwarg}, expected " \
"#{attribute.type.inspect}, got #{value.inspect}"
end
end
end

initialize_without_verify(**kwargs)
initialize_without_verify(**kwargs)
end
end
end

Expand Down