-
Notifications
You must be signed in to change notification settings - Fork 43
/
test_ruby.py
282 lines (216 loc) · 6.33 KB
/
test_ruby.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import unittest
import textwrap
from pseudo import generate
from pseudo.pseudo_tree import Node
import suite
class TestRuby(unittest.TestCase, metaclass=suite.TestLanguage): # dark magic bitches
_language = 'ruby'
_import = 'require'
_parse_import = lambda self, line: line[9:-1]
# make declarative style great again
# expected ruby translation for each example in suite:
int_ = '42'
float_ = '42.42'
string = "'la'"
boolean = 'true'
null = 'nil'
dictionary = "{la: 0}"
list_ = "['la']"
local = 'egg'
set_ = (['set'], 'Set.new([2])')
tuple_ = '[2, 42.2]'
array = '[2, 4]'
regex = '/[a-b]/'
typename = 'Egg'
instance_variable = '@egg'
attr = 'e.egg'
assignments = [
'egg = ham',
'@egg = ham',
'T.egg = ham',
"x[4] = 'String'"
]
call = 'map(x)'
method_call = 'e.filter(42)'
standard_call = [
'puts 42',
'gets',
'Math.log(ham)',
"source = File.read('f.py')"
]
# io
io_display = "puts 2, 'z'"
io_read = 'source = gets'
io_read_file = "source = File.read('z.py')"
io_write_file = "File.write('z.py', source)"
# math
math_ln = 'Math.log(z)'
math_log = 'Math.log(z, 2.0)'
math_tan = 'Math.tan(z)'
math_sin = 'Math.sin(z)'
math_cos = 'Math.cos(z)'
# regexp
regexp_compile = '/#{s}/'
regexp_escape = 'Regexp.escape(s)'
standard_method_call = [
'l.length',
"'l'[0...2]",
]
# List
list_push = "cpus.push('')"
list_pop = 'cpus.pop'
list_length = 'cpus.length'
list_map = "cpus.map { |value| value + 'a' }"
list_remove = "cpus.delete(s)"
list_remove_at = "cpus.delete_at(0)"
list_length = 'cpus.length'
list_slice = 'cpus[2...-1]'
list_slice_from = 'cpus[2..-1]'
list_slice_to = 'cpus[0...2]'
list_filter = 'cpus.select { |value| value.length == 0 }'
list_reduce = textwrap.dedent('''\
cpus.reduce('') do |value, other|
result = value + other
result
end''')
list_any = 'cpus.any? { |value| value.length == 0 }'
list_all = 'cpus.all? { |value| value.length == 0 }'
list_find = 'cpus.index(s)'
list_present = '!cpus.empty?'
list_empty = 'cpus.empty?'
list_contains = 'cpus.include?(s)'
list_sort = 'cpus.sort!'
# Hash
dictionary_length = 'pointers.length'
dictionary_contains = 'pointers.include?(s)'
dictionary_keys = 'pointers.keys'
dictionary_values = 'pointers.values'
# Set
set_length = (['set'], 'words.length')
set_contains = (['set'], 'words.include?(s)')
set_union = (['set'], 'words | words')
set_intersection = (['set'], 'words.intersection(words)')
# Tuple
tuple_length = 'flowers.length'
# Array
array_length = 'cars.length'
# String
string_substr = 's[1...-1]'
string_substr_from = 's[2..-1]'
string_substr_to = 's[0...-2]'
string_length = 's.length'
string_find = 's.index(t)'
string_find_from = 's.index(t, z)'
string_count = 's.count(t)'
string_concat = 's + t'
string_partition = 's.partition(t)'
string_split = 's.split(t)'
string_trim = 's.trim'
string_reversed = 's.reverse'
string_center = 's.center(z, t)'
string_present = '!s.empty?'
string_empty = 's.empty?'
string_contains = 's.include?(t)'
string_to_int = 's.to_i'
string_pad_left = 's.ljust(0, t)'
string_pad_right = 's.rjust(0, t)'
# Regexp
regexp_match = 's.scan(r)'
# RegexpMatch # result of s.scan is an array, fix regex in next versions
regexp_match_group = 'm[2][0]'
regexp_match_has_match = '!m.empty?'
binary_op = 'ham + egg'
unary_op = '-a'
comparison = 'egg > ham'
interpolation = '"#{s}la#{4}"'
if_statement = textwrap.dedent('''\
if egg == ham
l[0...2]
elsif egg == ham
puts 4.2
else
z
end''')
for_statement = [
textwrap.dedent('''\
sequence.each do |a|
log(a)
end'''),
textwrap.dedent('''\
(0...42).step(2).each do |j|
analyze(j)
end'''),
textwrap.dedent('''\
z.each_with_index do |k, j|
analyze(j, k)
end'''),
textwrap.dedent('''\
z.each do |j, k|
analyze(k, j)
end'''),
textwrap.dedent('''\
z.zip(zz).each do |k, l|
a(k, l)
end''')
]
while_statement = textwrap.dedent('''\
while f >= 42
b = g
end''')
function_definition = textwrap.dedent('''\
def weird(z)
fixed = fix(z)
fixed
end''')
method_definition = textwrap.dedent('''\
def parse(source)
@ast = 0
[source]
end''')
anonymous_function = [
'-> source { ves(source.length) }',
textwrap.dedent('''\
-> source do
puts source
ves(source.length)
end''')
]
class_statement = [textwrap.dedent('''\
class A < B
def initialize(a)
@a = a
end
def parse
42
end
end''')]
this = 'self'
constructor = textwrap.dedent('''\
def initialize(a, b)
@a = a
@b = b
end''')
neg_index = "'la'[-2]"
index = "'la'[2]"
try_statement = [
textwrap.dedent('''\
begin
a
h(-4)
rescue StandardError => e
puts e
end'''),
textwrap.dedent('''\
class NeptunError < StandardError
end
begin
a
h(-4)
rescue NeptunError => e
puts e
end''')
]
throw_statement = textwrap.dedent('''\
class NeptunError < StandardError
end
throw NeptunError.new(\'no tea\')''')