require "bundler/gem_tasks" require "rake/testtask" Rake::TestTask.new(:test) do |t| t.libs << "test" t.libs << "lib" t.test_files = FileList["test/**/*_test.rb"].reject do |path| path =~ %r{test/stdlib/} end end multitask :default => [:test, :stdlib_test, :rubocop, :validate, :test_doc] task :test_doc => :parser do files = Dir.chdir(File.expand_path('..', __FILE__)) do `git ls-files -z`.split("\x0").select do |file| Pathname(file).extname == ".md" end end sh "ruby bin/run_in_md.rb #{files.join(" ")}" end task :validate => :parser do sh "rbs validate" FileList["stdlib/*"].each do |path| next if path =~ %r{stdlib/builtin} sh "rbs -r#{File.basename(path)} validate" end end FileList["test/stdlib/**/*_test.rb"].each do |test| multitask test => :parser do sh "ruby bin/test_runner.rb #{test}" end multitask stdlib_test: test end task :rubocop do sh "rubocop --parallel" end rule ".rb" => ".y" do |t| sh "racc -v -o #{t.name} #{t.source}" end task :parser => "lib/rbs/parser.rb" task :test => :parser task :stdlib_test => :parser task :build => :parser namespace :generate do task :stdlib_test, [:class] do |_task, args| klass = args.fetch(:class) do raise "Class name is necessary. e.g. rake 'generate:stdlib_test[String]'" end path = Pathname(ENV["RBS_GENERATE_TEST_PATH"] || "test/stdlib/#{klass}_test.rb") raise "#{path} already exists!" if path.exist? require "erb" require "rbs" class TestTemplateBuilder attr_reader :klass, :env def initialize(klass) @klass = klass loader = RBS::EnvironmentLoader.new Dir['stdlib/*'].each do |lib| next if lib.end_with?('builtin') loader.add(library: File.basename(lib)) end @env = RBS::Environment.from_loader(loader).resolve_type_names end def call ERB.new(<<~ERB, trim_mode: "-").result(binding) require_relative "test_helper" require 'rbs/test/test_helper' <%- unless class_methods.empty? -%> class <%= klass %>SingletonTest < Minitest::Test include RBS::Test::TypeAssertions # library "pathname", "set", "securerandom" # Declare library signatures to load testing "singleton(::<%= klass %>)" <%- class_methods.each do |method_name, definition| %> def test_<%= test_name_for(method_name) %> <%- definition.method_types.each do |method_type| -%> assert_send_type "<%= method_type %>", <%= klass %>, :<%= method_name %> <%- end -%> end <%- end -%> end <%- end -%> <%- unless instance_methods.empty? -%> class <%= klass %>Test < Minitest::Test include RBS::Test::TypeAssertions # library "pathname", "set", "securerandom" # Declare library signatures to load testing "::<%= klass %>" <%- instance_methods.each do |method_name, definition| %> def test_<%= test_name_for(method_name) %> <%- definition.method_types.each do |method_type| -%> assert_send_type "<%= method_type %>", <%= klass %>.new, :<%= method_name %> <%- end -%> end <%- end -%> end <%- end -%> ERB end private def test_name_for(method_name) { :== => 'double_equal', :!= => 'not_equal', :=== => 'triple_equal', :[] => 'square_bracket', :[]= => 'square_bracket_assign', :> => 'greater_than', :< => 'less_than', :>= => 'greater_than_equal_to', :<= => 'less_than_equal_to', :<=> => 'spaceship', :+ => 'plus', :- => 'minus', :* => 'multiply', :/ => 'divide', :** => 'power', :% => 'modulus', :& => 'and', :| => 'or', :^ => 'xor', :>> => 'right_shift', :<< => 'left_shift', :=~ => 'pattern_match', :!~ => 'does_not_match', :~ => 'tilde' }.fetch(method_name, method_name) end def type_name @type_name ||= RBS::TypeName.new(name: klass.to_sym, namespace: RBS::Namespace.new(path: [], absolute: true)) end def class_methods @class_methods ||= RBS::DefinitionBuilder.new(env: env).build_singleton(type_name).methods.select {|_, definition| definition.implemented_in == type_name } end def instance_methods @instance_methods ||= RBS::DefinitionBuilder.new(env: env).build_instance(type_name).methods.select {|_, definition| definition.implemented_in == type_name } end end path.write TestTemplateBuilder.new(klass).call puts "Created: #{path}" end end CLEAN.include("lib/rbs/parser.rb")