Rakefile in rbs-2.1.0 vs Rakefile in rbs-2.2.0

- old
+ new

@@ -32,10 +32,19 @@ rule ".c" => ".re" do |t| puts "⚠️⚠️⚠️ #{t.name} is older than #{t.source}. You may need to run `rake lexer` ⚠️⚠️⚠️" end +task :annotate do + sh "rbs annotate core stdlib" +end + +task :confirm_annotation do + puts "Testing if RBS docs are updated with respect to RDoc" + sh "git diff --exit-code core stdlib" +end + task :compile => "ext/rbs_extension/lexer.c" task :test_doc do files = Dir.chdir(File.expand_path('..', __FILE__)) do `git ls-files -z`.split("\x0").select do |file| Pathname(file).extname == ".md" end @@ -71,10 +80,11 @@ lib << "singleton" end if lib == ["net-http"] lib << "uri" + lib << "timeout" end if lib == ["resolv"] lib << "socket" lib << "timeout" @@ -104,21 +114,44 @@ 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 TestTarget + def initialize(klass) + @type_name = RBS::Namespace.parse(klass).to_type_name + end + + def path + Pathname(ENV['RBS_GENERATE_TEST_PATH'] || "test/stdlib/#{file_name}_test.rb") + end + + def file_name + @type_name.to_s.gsub(/\A::/, '').gsub(/::/, '_') + end + + def to_s + @type_name.to_s + end + + def absolute_type_name + @absolute_type_name ||= @type_name.absolute! + end + end + + target = TestTarget.new(klass) + path = target.path + raise "#{path} already exists!" if path.exist? + class TestTemplateBuilder - attr_reader :klass, :env + attr_reader :target, :env - def initialize(klass) - @klass = klass + def initialize(target) + @target = target loader = RBS::EnvironmentLoader.new Dir['stdlib/*'].each do |lib| next if lib.end_with?('builtin') @@ -130,39 +163,39 @@ def call ERB.new(<<~ERB, trim_mode: "-").result(binding) require_relative "test_helper" <%- unless class_methods.empty? -%> - class <%= klass %>SingletonTest < Test::Unit::TestCase + class <%= target %>SingletonTest < Test::Unit::TestCase include TypeAssertions # library "pathname", "set", "securerandom" # Declare library signatures to load - testing "singleton(::<%= klass %>)" + testing "singleton(::<%= target %>)" <%- 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 %> + <%= target %>, :<%= method_name %> <%- end -%> end <%- end -%> end <%- end -%> <%- unless instance_methods.empty? -%> - class <%= klass %>Test < Test::Unit::TestCase + class <%= target %>Test < Test::Unit::TestCase include TypeAssertions # library "pathname", "set", "securerandom" # Declare library signatures to load - testing "::<%= klass %>" + testing "::<%= target %>" <%- 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 %> + <%= target %>.new, :<%= method_name %> <%- end -%> end <%- end -%> end <%- end -%> @@ -198,32 +231,30 @@ :!~ => '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 + @class_methods ||= RBS::DefinitionBuilder.new(env: env).build_singleton(target.absolute_type_name).methods.select {|_, definition| + definition.implemented_in == target.absolute_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 + @instance_methods ||= RBS::DefinitionBuilder.new(env: env).build_instance(target.absolute_type_name).methods.select {|_, definition| + definition.implemented_in == target.absolute_type_name } end end - path.write TestTemplateBuilder.new(klass).call + path.write TestTemplateBuilder.new(target).call puts "Created: #{path}" end end task :test_generate_stdlib do sh "RBS_GENERATE_TEST_PATH=/tmp/Array_test.rb rake 'generate:stdlib_test[Array]'" sh "ruby -c /tmp/Array_test.rb" + sh "RBS_GENERATE_TEST_PATH=/tmp/Thread_Mutex_test.rb rake 'generate:stdlib_test[Thread::Mutex]'" + sh "ruby -c /tmp/Thread_Mutex_test.rb" end