-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
214 lines (173 loc) · 5.79 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require "bundler/gem_tasks"
require "rake/testtask"
require "rbconfig"
ruby = ENV["RUBY"] || RbConfig.ruby
racc = ENV.fetch("RACC", "racc")
rbs = File.join(__dir__, "exe/rbs")
bin = File.join(__dir__, "bin")
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} #{__dir__}/bin/run_in_md.rb #{files.join(" ")}"
end
task :validate => :parser do
sh "#{ruby} #{rbs} validate --silent"
FileList["stdlib/*"].each do |path|
lib = [File.basename(path).to_s]
if lib == ["bigdecimal-math"]
lib << "bigdecimal"
end
if lib == ["yaml"]
lib << "dbm"
lib << "pstore"
end
if lib == ["logger"]
lib << "monitor"
end
if lib == ["csv"]
lib << "forwardable"
end
if lib == ["prime"]
lib << "singleton"
end
sh "#{ruby} #{rbs} #{lib.map {|l| "-r #{l}"}.join(" ")} validate --silent"
end
end
FileList["test/stdlib/**/*_test.rb"].each do |test|
task test => :parser do
sh "#{ruby} -Ilib #{bin}/test_runner.rb #{test}"
end
task 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
task :confirm_parser do
puts "Testing if parser.rb is updated with respect to parser.y"
sh "#{racc} -v -o lib/rbs/parser.rb lib/rbs/parser.y"
sh "git diff --exit-code lib/rbs/parser.rb"
end
namespace :generate do
desc "Generate a test file for a stdlib class signatures"
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"
<%- unless class_methods.empty? -%>
class <%= klass %>SingletonTest < Test::Unit::TestCase
include 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 < Test::Unit::TestCase
include 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
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"
end
CLEAN.include("lib/rbs/parser.rb")