code-ruby 3.1.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bin/code +97 -20
  4. data/lib/code/concerns/shared.rb +331 -15
  5. data/lib/code/format.rb +15 -1
  6. data/lib/code/network.rb +87 -0
  7. data/lib/code/node/call.rb +79 -2
  8. data/lib/code/node/call_argument.rb +14 -0
  9. data/lib/code/node/code.rb +5 -4
  10. data/lib/code/node/function_parameter.rb +7 -4
  11. data/lib/code/node/list.rb +29 -1
  12. data/lib/code/object/base_64.rb +132 -6
  13. data/lib/code/object/boolean.rb +60 -0
  14. data/lib/code/object/class.rb +138 -2
  15. data/lib/code/object/code.rb +111 -3
  16. data/lib/code/object/context.rb +57 -1
  17. data/lib/code/object/cryptography.rb +63 -0
  18. data/lib/code/object/date.rb +13339 -462
  19. data/lib/code/object/decimal.rb +1725 -0
  20. data/lib/code/object/dictionary.rb +1790 -11
  21. data/lib/code/object/duration.rb +28 -0
  22. data/lib/code/object/function.rb +261 -23
  23. data/lib/code/object/global.rb +534 -1
  24. data/lib/code/object/html.rb +179 -7
  25. data/lib/code/object/http.rb +244 -14
  26. data/lib/code/object/ics.rb +75 -13
  27. data/lib/code/object/identifier_list.rb +17 -2
  28. data/lib/code/object/integer.rb +1937 -2
  29. data/lib/code/object/json.rb +75 -1
  30. data/lib/code/object/list.rb +3383 -10
  31. data/lib/code/object/nothing.rb +53 -0
  32. data/lib/code/object/number.rb +110 -0
  33. data/lib/code/object/parameter.rb +140 -0
  34. data/lib/code/object/range.rb +576 -14
  35. data/lib/code/object/smtp.rb +95 -12
  36. data/lib/code/object/string.rb +944 -3
  37. data/lib/code/object/super.rb +10 -1
  38. data/lib/code/object/time.rb +13358 -498
  39. data/lib/code/object/url.rb +65 -0
  40. data/lib/code/object.rb +543 -0
  41. data/lib/code/parser.rb +161 -24
  42. data/lib/code-ruby.rb +3 -0
  43. data/lib/code.rb +30 -3
  44. metadata +135 -84
  45. data/.github/dependabot.yml +0 -15
  46. data/.github/workflows/ci.yml +0 -38
  47. data/.gitignore +0 -30
  48. data/.node-version +0 -1
  49. data/.npm-version +0 -1
  50. data/.prettierignore +0 -2
  51. data/.rspec +0 -1
  52. data/.rubocop.yml +0 -140
  53. data/.ruby-version +0 -1
  54. data/.tool-versions +0 -3
  55. data/AGENTS.md +0 -43
  56. data/Gemfile +0 -22
  57. data/Gemfile.lock +0 -292
  58. data/Rakefile +0 -5
  59. data/bin/bundle +0 -123
  60. data/bin/bundle-audit +0 -31
  61. data/bin/bundler-audit +0 -31
  62. data/bin/dorian +0 -31
  63. data/bin/rspec +0 -31
  64. data/bin/rubocop +0 -31
  65. data/bin/test +0 -5
  66. data/code-ruby.gemspec +0 -34
  67. data/docs/precedence.txt +0 -36
  68. data/package-lock.json +0 -14
  69. data/package.json +0 -7
  70. data/spec/bin/code_spec.rb +0 -48
  71. data/spec/code/format_spec.rb +0 -153
  72. data/spec/code/node/call_spec.rb +0 -11
  73. data/spec/code/object/boolean_spec.rb +0 -18
  74. data/spec/code/object/cryptography_spec.rb +0 -25
  75. data/spec/code/object/decimal_spec.rb +0 -50
  76. data/spec/code/object/dictionary_spec.rb +0 -98
  77. data/spec/code/object/function_spec.rb +0 -268
  78. data/spec/code/object/http_spec.rb +0 -33
  79. data/spec/code/object/ics_spec.rb +0 -50
  80. data/spec/code/object/integer_spec.rb +0 -42
  81. data/spec/code/object/list_spec.rb +0 -22
  82. data/spec/code/object/nothing_spec.rb +0 -14
  83. data/spec/code/object/range_spec.rb +0 -23
  84. data/spec/code/object/string_spec.rb +0 -26
  85. data/spec/code/parser/boolean_spec.rb +0 -11
  86. data/spec/code/parser/chained_call_spec.rb +0 -16
  87. data/spec/code/parser/dictionary_spec.rb +0 -18
  88. data/spec/code/parser/function_spec.rb +0 -16
  89. data/spec/code/parser/group_spec.rb +0 -11
  90. data/spec/code/parser/if_modifier_spec.rb +0 -18
  91. data/spec/code/parser/list_spec.rb +0 -17
  92. data/spec/code/parser/number_spec.rb +0 -11
  93. data/spec/code/parser/string_spec.rb +0 -20
  94. data/spec/code/parser_spec.rb +0 -52
  95. data/spec/code/type_spec.rb +0 -21
  96. data/spec/code_spec.rb +0 -717
  97. data/spec/spec_helper.rb +0 -21
  98. data/spec/zeitwerk/loader_spec.rb +0 -7
data/spec/code_spec.rb DELETED
@@ -1,717 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Code do
6
- def format_input(input)
7
- Code::Format.format(described_class.parse(input))
8
- end
9
-
10
- def evaluate_with_output(input)
11
- output = StringIO.new
12
- result = described_class.evaluate(input, output: output)
13
- [result, output.string]
14
- end
15
-
16
- def control_flow_block_calls
17
- [
18
- "[1].each { CONTROL }",
19
- "[1, 2].any? { CONTROL }",
20
- "[1].detect { CONTROL }",
21
- "[1].map { CONTROL }",
22
- "xs = [1] xs.map! { CONTROL }",
23
- "[1].max { CONTROL }",
24
- "[1].none? { CONTROL }",
25
- "[1].all? { CONTROL }",
26
- "[1, 2].reduce { |acc, item| CONTROL }",
27
- "[nothing].compact { CONTROL }",
28
- "xs = [nothing] xs.compact! { CONTROL }",
29
- "[1].select { CONTROL }",
30
- "xs = [1] xs.select! { CONTROL }",
31
- "[1].reject { CONTROL }",
32
- "xs = [1] xs.reject! { CONTROL }",
33
- "[1].sort { CONTROL }",
34
- "[1].uniq { CONTROL }",
35
- "(1..1).all? { CONTROL }",
36
- "(1..1).any? { CONTROL }",
37
- "(1..1).each { CONTROL }",
38
- "(1..1).map { CONTROL }",
39
- "(1..1).select { CONTROL }",
40
- "{a: 1}.any? { CONTROL }",
41
- "{a: 1}.compact { CONTROL }",
42
- "hash = {a: 1} hash.compact! { CONTROL }",
43
- "{a: 1}.delete(:b) { CONTROL }",
44
- "hash = {a: 1} hash.delete_if { CONTROL }",
45
- "hash = {a: 1} hash.delete_unless { CONTROL }",
46
- "{a: 1}.each { CONTROL }",
47
- "{a: 1}.fetch(:b) { CONTROL }",
48
- "hash = {a: 1} hash.keep_if { CONTROL }",
49
- "hash = {a: 1} hash.keep_unless { CONTROL }",
50
- "{a: 1}.key(2) { CONTROL }",
51
- "{a: 1}.merge({a: 2}) { CONTROL }",
52
- "hash = {a: 1} hash.merge!({a: 2}) { CONTROL }",
53
- "{a: 1}.select { CONTROL }",
54
- "hash = {a: 1} hash.select! { CONTROL }",
55
- "{a: 1}.transform_values { CONTROL }",
56
- "1.times { CONTROL }",
57
- "Html.p { CONTROL }",
58
- "Html.escape { CONTROL }",
59
- "Html.unescape { CONTROL }",
60
- "Html.join(Html.br) { CONTROL }",
61
- "Html.text { CONTROL }",
62
- "Html.raw { CONTROL }",
63
- "Html.raw(\"<p>a</p>\").css(\"p\").map { CONTROL }"
64
- ]
65
- end
66
-
67
- %w[return break next continue].each do |control_flow|
68
- it "continues after #{control_flow} in block methods" do
69
- aggregate_failures do
70
- control_flow_block_calls.each do |block_call|
71
- input = "#{block_call.sub("CONTROL", control_flow)} puts(:end)"
72
- formatted = format_input(input)
73
-
74
- _result, output = evaluate_with_output(input)
75
- _formatted_result, formatted_output = evaluate_with_output(formatted)
76
-
77
- expect(output).to eq("end\n"), input
78
- expect(formatted_output).to eq("end\n"), formatted
79
- end
80
- end
81
- end
82
- end
83
-
84
- (
85
- [
86
- "{ a: 1, b: 2 }.transform_values { |key| key.upcase }",
87
- "{ a: { b: [1, 2, 3, 4] } }.dig(:a, :b, 2)",
88
- "{ a: 1, b: 2 }.transform_values { |key, value, index| index * 2 }",
89
- "{ a: 1, b: 2 }.transform_values { |key, value, index, dictionary| dictionary.a }",
90
- "sum = (a, b: 2) => { a + b } sum(1)",
91
- "Object.new !== Object.new"
92
- ] +
93
- %w[
94
- 1..3
95
- 1...3
96
- :abc.size
97
- Date.new.change
98
- {}.zero?
99
- {}.any?
100
- {}.many?
101
- 1.zero?
102
- 1.any?
103
- 1.many?
104
- 1.0.zero?
105
- 1.0.any?
106
- 1.0.many?
107
- [].zero?
108
- [].any?
109
- [].many?
110
- Base64
111
- Base64.new
112
- Smtp
113
- Smtp.new
114
- Time.monday?
115
- Time.tuesday?
116
- Time.wednesday?
117
- Time.thursday?
118
- Time.friday?
119
- Time.saturday?
120
- Time.sunday?
121
- Time.now.monday?
122
- Time.now.tuesday?
123
- Time.now.wednesday?
124
- Time.now.thursday?
125
- Time.now.friday?
126
- Time.now.saturday?
127
- Time.now.sunday?
128
- Time.now.second
129
- Time.now.seconds
130
- Time.now.minute
131
- Time.now.minutes
132
- Time.now.hour
133
- Time.now.hours
134
- Time.now.day
135
- Time.now.days
136
- Time.now.month
137
- Time.now.months
138
- Time.now.year
139
- Time.now.years
140
- Time.second
141
- Time.seconds
142
- Time.minute
143
- Time.minutes
144
- Time.hour
145
- Time.hours
146
- Time.day
147
- Time.days
148
- Time.month
149
- Time.months
150
- Time.year
151
- Time.years
152
- 1.day.ago
153
- 1.day.from_now
154
- 1.hour.ago
155
- 1.hour.from_now
156
- 2.days.ago
157
- 2.days.from_now
158
- 2.hours.ago
159
- 2.hours.ago.hour
160
- 2.hours.from_now
161
- 2.hours.from_now.hour
162
- Time.hour
163
- Date.hour
164
- Boolean.new
165
- Boolean.new(true)
166
- Boolean.new(false)
167
- Class.new
168
- Class.new(Boolean)
169
- Class.new(Class)
170
- Context.new
171
- Context.new(a:1)
172
- Date.new
173
- Date.new.hour
174
- Date.new("2024-03-05")
175
- Date.new("2024-03-05").hour
176
- Date.today
177
- Date.yesterday
178
- Date.tomorrow
179
- Date.tomorrow.hour
180
- Decimal.new
181
- Decimal.new(0)
182
- Decimal.new(1.2)
183
- Dictionary.new
184
- Dictionary.new(a:1)
185
- Duration.new
186
- Duration.new(1.day)
187
- Duration.new("P1D")
188
- Function.new
189
- Integer.new
190
- Integer.new(0)
191
- Integer.new(1)
192
- Integer.new(1.2)
193
- List.new
194
- List.new([])
195
- List.new([1,2])
196
- Nothing.new
197
- Nothing.new(1)
198
- Object.new
199
- Object.new(1)
200
- Range.new
201
- Range.new(1,2)
202
- Range.new(-1)
203
- Range.new(1,2,exclude_end:false)
204
- Range.new(1,2,exclude_end:true)
205
- String.new
206
- String.new(:hello)
207
- Time.new
208
- Time.new("2024-03-05.06:10:59.UTC")
209
- Time.now
210
- Time.tomorrow
211
- Time.yesterday
212
- Time.tomorrow
213
- Code.new
214
- Parameter.new
215
- IdentifierList.new
216
- IdentifierList.new([])
217
- Time.new(nothing).before?
218
- Json.parse('1')
219
- Json.parse('[]')
220
- Json.parse('{}')
221
- Json.parse('random-string')
222
- Ics.parse("BEGIN:VCALENDAR\nEND:VCALENDAR")
223
- {}["".to_string]
224
- ] + ["Time.hour >= 6 and Time.hour <= 23"]
225
- ).each do |input|
226
- it(input) do
227
- original_result, original_output = evaluate_with_output(input)
228
- formatted = format_input(input)
229
- formatted_result, formatted_output = evaluate_with_output(formatted)
230
-
231
- expect(formatted_result.class).to eq(original_result.class)
232
- expect(formatted_output).to eq(original_output)
233
- end
234
- end
235
-
236
- examples =
237
- [
238
- [
239
- "user = { name: :Dorian, age: 31 } user.delete_if(String) user.keys",
240
- '["age"]'
241
- ],
242
- [
243
- "(user = { name: :Dorian, age: 31 }).transform_values { user.name }.age",
244
- ":Dorian"
245
- ],
246
- ["{ end: 2 }.keys.first", ":end"],
247
- %w[!!1 true],
248
- %w[!!true true],
249
- %w[!false true],
250
- %w["" ''],
251
- %w["Hello".downcase :hello],
252
- %w['' ""],
253
- %w[+1 1],
254
- %w[+1.0 1.0],
255
- %w[+true true],
256
- %w[--1 1],
257
- %w[--1.0 1.0],
258
- %w[-1 -1],
259
- %w[-1.0 -1.0],
260
- %w[0 0],
261
- %w[0b10 2],
262
- %w[0o10 8],
263
- %w[0x10 16],
264
- %w[1.0e1 10.0],
265
- %w[1.1 1.1],
266
- %w[1.day.ago.after? false],
267
- %w[1.day.ago.after?(1.day.from_now) false],
268
- %w[1.day.ago.before? true],
269
- %w[1.day.ago.before?(1.day.from_now) true],
270
- %w[1.day.from_now.after? true],
271
- %w[1.day.from_now.after?(1.day.ago) true],
272
- %w[1.day.from_now.before? false],
273
- %w[1.day.from_now.before?(1.day.ago) false],
274
- %w[10e1.0 100.0],
275
- %w[1e1 10],
276
- %w[2.days.ago.future? false],
277
- %w[2.days.ago.past? true],
278
- %w[2.days.from_now.future? true],
279
- %w[2.days.from_now.past? false],
280
- %w[9975×14÷8 17456.25],
281
- %w["Hello".starts_with?("He") true],
282
- %w["Hello".starts_with?("lo") false],
283
- %w["Hello".start_with?("He") true],
284
- %w["Hello".start_with?("lo") false],
285
- %w["Hello".ends_with?("lo") true],
286
- %w["Hello".ends_with?("He") false],
287
- %w["Hello".end_with?("lo") true],
288
- %w["Hello".end_with?("He") false],
289
- %w[:Hello.include?(:H) true],
290
- %w[:admin? :admin?],
291
- %w[:hello :hello],
292
- %w[:update!.to_string :update!],
293
- %w[Boolean(1) true],
294
- %w[Boolean(2.days.ago) true],
295
- %w[Boolean(2.days.ago) true],
296
- %w[Boolean(false) false],
297
- %w[Boolean(nothing) false],
298
- %w[Boolean(true) true],
299
- %w[Boolean({}) true],
300
- %w[Boolean.new false],
301
- %w[Boolean.new(1) true],
302
- %w[Boolean.new(2.days.ago) true],
303
- %w[Boolean.new(2.days.ago) true],
304
- %w[Boolean.new(false) false],
305
- %w[Boolean.new(nothing) false],
306
- %w[Boolean.new(true) true],
307
- %w[Boolean.new({}) true],
308
- %w[Class(2.days.ago) Time],
309
- %w[Class(Boolean) Boolean],
310
- %w[Class(Time) Time],
311
- %w[Class(nothing) Nothing],
312
- %w[Class(true) Boolean],
313
- %w[String String],
314
- %w[Time Time],
315
- %w[Class Class],
316
- %w[Class.new Nothing],
317
- %w[Class.new Nothing],
318
- %w[Class.new(2.days.ago) Time],
319
- %w[Class.new(Boolean) Boolean],
320
- %w[Class.new(Date) Date],
321
- %w[Class.new(Time) Time],
322
- %w[Class.new(nothing) Nothing],
323
- %w[Class.new(true) Boolean],
324
- %w[Date Date],
325
- ["Date.format == Date.format(:default)", "true"],
326
- %w[Date("0001-01-01").to_string :0001-01-01],
327
- [
328
- "Date(\"2024-03-02\").format == Date(\"2024-03-02\").format(:default)",
329
- "true"
330
- ],
331
- %w[Date("2024-03-02").to_string :2024-03-02],
332
- %w[Decimal(1) 1.0],
333
- %w[Decimal(1) 1],
334
- %w[Decimal(:1) 1.0],
335
- %w[Decimal(:1) 1],
336
- %w[Decimal.new 0],
337
- %w[Decimal.new(1) 1.0],
338
- %w[Decimal.new(1) 1],
339
- %w[Decimal.new(:1) 1.0],
340
- %w[Decimal.new(:1) 1],
341
- %w[false false],
342
- %w[true true],
343
- %w[{} {}],
344
- ["'Hello Dorian'", '"Hello Dorian"'],
345
- ["'Hello \\{name}'", ':Hello + " \\{name}"'],
346
- ["'Hello {1}'", '"Hello 1"'],
347
- ["(true false)", "false"],
348
- ["1 & 1", "1"],
349
- ["1 + 1", "2"],
350
- ["1 < 2", "true"],
351
- ["1 << 1", "2"],
352
- ["1 <= 1", "true"],
353
- ["1 == 1 and 2 == 2", "true"],
354
- ["1 == 1", "true"],
355
- ["1 > 2", "false"],
356
- ["1 >= 1", "true"],
357
- ["1 >> 1", "0"],
358
- ["1 ^ 2", "3"],
359
- ["1 | 2", "3"],
360
- ["2 * 3 + 2", "8"],
361
- ["2 * 3", "6"],
362
- ["2 ** 3 ** 2", "512"],
363
- ["2 ** 3", "8"],
364
- ["2 + 2 * 3", "8"],
365
- ["2 / 4", "0.5"],
366
- ["3.times {}", "3"],
367
- ["4 % 3", "1"],
368
- ["Boolean([])", "true"],
369
- ["Boolean(true, false)", "true"],
370
- ["Boolean.new([])", "true"],
371
- ["Boolean.new(false, true)", "false"],
372
- ["Boolean.new(true, false)", "true"],
373
- ["Class(true, 1)", "Boolean"],
374
- ["Class.new(Boolean, Time)", "Boolean"],
375
- ["Class.new(Time, Boolean)", "Time"],
376
- %w[Date("2024-3-2").to_string :2024-03-02],
377
- %w[Date("2024-3-2").to_string :2024-03-02],
378
- %w[Time.format.present? true],
379
- ["Class(Time.format(locale: :fr))", "String"],
380
- [
381
- "Time.new(\"2024-03-05.06:10:59.UTC\").format(locale: :fr) == Time.new(\"2024-03-05.06:10:59.UTC\").format(:default, locale: :fr)",
382
- "true"
383
- ],
384
- [
385
- "Time.new(\"2024-03-05.06:10:59.UTC\").format == Time.new(\"2024-03-05.06:10:59.UTC\").format(:default)",
386
- "true"
387
- ],
388
- ["Decimal(1, :2)", "100"],
389
- ["Decimal(:1, 2)", "100.0"],
390
- ["Decimal.new(1, :2)", "100"],
391
- ["Decimal.new(:1, 2)", "100.0"],
392
- ["[,,].include?(4)", "false"],
393
- ["[,].include?(4)", "false"],
394
- ["[1, 2, 3, \n ].include?(4)", "false"],
395
- ["[1, 2, 3,].include?(4)", "false"],
396
- ["[1, 2, 3]", "[1, 2, 3]"],
397
- ["[1, 2, 3].include?(2)", "true"],
398
- ["[1, 2, 3].include?(4)", "false"],
399
- [
400
- "summary = { by_status: { draft: 1 } } k = \"draft\" summary.by_status[k]",
401
- "1"
402
- ],
403
- [
404
- "summary = { by_status: {} } k = \"draft\" summary.by_status[k] = 1 summary",
405
- '{"by_status" => {"draft" => 1}}'
406
- ],
407
- [
408
- "summary = { by_status: {} } [{status: \"draft\"}].each { |track| summary.by_status[track[:status]] = 1 } summary",
409
- '{"by_status" => {"draft" => 1}}'
410
- ],
411
- ["[1, 2, 3].map { |i| continue(0) if i.even? i ** 2}", "[1, 0, 9]"],
412
- ["[1, 2, 3].map { |i| next if i == 2 i ** 2}", "[1, nothing, 9]"],
413
- ["[1, 2, 3].map { |i| next(0) if i.even? i ** 2}", "[1, 0, 9]"],
414
- ["[1, 2, 3].select { |n| n.even? }", "[2]"],
415
- ["[1, 2, 3].select { |n| n.even? }.select { |n| n.odd? }", "[]"],
416
- ["[1, 2, 3, 4].uniq { |n| n % 2 }", "[1, 2]"],
417
- ['[1, "1", 2, "2"].uniq(String)', '["1", "2"]'],
418
- ["[1, 2, 3, 4].uniq(String)", "[]"],
419
- ["[1, 2].map(&:to_string)", "[:1, :2]"],
420
- ["[1, 2].map(&:to_string)", '["1", "2"]'],
421
- ["[1, 2].map(&:to_string)", '["1", "2"]'],
422
- ["[1].each do end", "[1]"],
423
- ["x = [1].each { break(42) } x", "42"],
424
- ["[[true]]", "[[true]]"],
425
- ["[]", "[]"],
426
- ["\r\n", "nothing"],
427
- ["a = 0 [1, 2, 3].each { |i| next if i == 2 a += i } a", "4"],
428
- [
429
- "a = 0\nb = 0\nwhile a < 4\n a += 1\n continue if a == 2\n b += a\nend\nb",
430
- "8"
431
- ],
432
- ["a = 0 loop a += 1 break end a", "1"],
433
- ["x = loop break(42) end x", "42"],
434
- ["x = loop { |index| break(index) if index > 3 } x", "4"],
435
- ["x = loop do |index| break(index) if index > 3 end x", "4"],
436
- ["a = 0\nuntil a > 10 a += 1 end a", "11"],
437
- ["a = 0\nwhile a < 10 a += 1 end a", "10"],
438
- ["a = 1 3.times { a += 1 } a", "4"],
439
- ["a = 1 a *= 2 a", "2"],
440
- ["a = 1 a += 1 a", "2"],
441
- ["a = 1 a += 1 a", "2"],
442
- ["a = 1 a -= 1 a", "0"],
443
- ["a = 3 a -= 1 if false a", "3"],
444
- ["a = 1 a = - 1 if false a", "1"],
445
- ["a = 3\n(a -= 1) if false\na", "3"],
446
- ["a = 1 a /= 2 a", "0.5"],
447
- ["a = 1 a <<= 1 a", "2"],
448
- ["a = 1 a >>= 1 a", "0"]
449
- ] +
450
- [
451
- ["a = 1 a ^= 2 a", "3"],
452
- ["a = 1 a |= 2 a", "3"],
453
- ["a = 1 a", "1"],
454
- ["a = 1 b = 2 c = a + b c", "3"],
455
- ["a = 1", "1"],
456
- ["a = 10 a %= 2 a", "0"],
457
- ["a = b = c = 1 a + b + c", "3"],
458
- ["a = false a &&= true a", "false"],
459
- ["a = false a ||= true a", "true"],
460
- ["a rescue :oops", ":oops"],
461
- ["false ? 1 : 2", "2"],
462
- ["false ? 1", ""],
463
- ["false || false", "false"],
464
- ["if false 1 else 2", "2"],
465
- ["if false 1 else if false 2", "nothing"],
466
- ["if false 1 else if true 2", "2"],
467
- ["if false 1 else if false 2 else 3 end", "3"],
468
- ["if false 1 else if true 2 else 3 end", "2"],
469
- ["if false 1 else unless false 2", "2"],
470
- ["if false 1 else unless true 2 else 3 end", "3"],
471
- ["if false 1 else unless true 2", "nothing"],
472
- ["if false 1 elsif false 2", "nothing"],
473
- ["if false 1 elsif true 2", "2"],
474
- ["if false 1", "nothing"],
475
- %w[break(5) 5],
476
- %w[continue(6) 6],
477
- ["if true 1", "1"],
478
- %w[next(7) 7],
479
- ["not not false", "false"],
480
- ["not true", "false"],
481
- %w[return(9) 9],
482
- ["f = () => { return(3) 4 } f()", "3"],
483
- ["a = 1 orirginal = 2 orirginal", "2"],
484
- ["a = 1 andy = 2 andy", "2"],
485
- ["ifonly = 1 ifonly", "1"],
486
- ["unlessy = 2 unlessy", "2"],
487
- ["elsiffy = 3 elsiffy", "3"],
488
- ["elsunlessy = 4 elsunlessy", "4"],
489
- ["elsewhere = 5 elsewhere", "5"],
490
- ["whiley = 6 whiley", "6"],
491
- ["untily = 7 untily", "7"],
492
- ["looped = 8 looped", "8"],
493
- ["doable = 9 doable", "9"],
494
- ["beginner = 10 beginner", "10"],
495
- ["endless = 11 endless", "11"],
496
- ["trueblue = 12 trueblue", "12"],
497
- ["falsey = 13 falsey", "13"],
498
- ["nothingness = 14 nothingness", "14"],
499
- ["random = 1 random", "1"],
500
- ["true && false", "false"],
501
- ["true && true", "true"],
502
- ["true ? 1 : 2", "1"],
503
- ["true ? 1", "1"],
504
- ["true and false", "false"],
505
- ["true or false", "true"],
506
- ["weekday? = false\n or true\nweekday?", "true"],
507
- ["true || false", "true"],
508
- ["unless false 1", "1"],
509
- ["unless true 1", "nothing"],
510
- ["until true", "nothing"],
511
- ["while false", "nothing"],
512
- ["{ a: 1, b: 2, c: 3 }", '{"a" => 1, "b" => 2, "c" => 3}'],
513
- ['"Hello Dorian"', '"Hello Dorian"'],
514
- ['"Hello \\{name}"', '"Hello \\{" + "name}"'],
515
- ['"Hello {1}"', '"Hello 1"'],
516
- ['user = {} user.name = "Dorian" user.name', ":Dorian"],
517
- ['user = {} user[:name] = "Dorian" user[:name]', ":Dorian"],
518
- [
519
- 'value = {} email = "a" calendar_id = "c" value[email] ||= {} value[email][calendar_id] ||= {} value[email][calendar_id]',
520
- "{}"
521
- ],
522
- ['{ "first_name": "Dorian" }', '{"first_name" => "Dorian"}'],
523
- ['{ "first_name": "Dorian" }.as_json', '{"first_name" => "Dorian"}'],
524
- %w[nothing.to_json :null],
525
- %w[1.to_json "1"],
526
- %w[1.0.to_json '"1.0"'],
527
- %w[1.1.to_json '"1.1"'],
528
- ["a = {} a.merge!(a: 1) a", "{a: 1}"],
529
- ["a = {} a.merge(a: 1) a", "{}"],
530
- %w[1&.even? false],
531
- ["nothing&.even? || 1", "1"],
532
- ["nothing&.even? && 1", "nothing"],
533
- %w[2&.even? true],
534
- ["a = 1 a&.even?", "false"],
535
- ["a = 2 a&.even?", "true"],
536
- ["a = nothing a&.even?", "nothing"],
537
- ["false && puts(:Hello)", "false"],
538
- ["true || puts(:Hello)", "true"],
539
- ["false and puts(:Hello)", "false"],
540
- ["true or puts(:Hello)", "true"],
541
- ["[1, 2].join", "'12'"],
542
- ["[1, 2].join(',')", "'1,2'"],
543
- ["[nothing, 2].join(',')", "',2'"],
544
- ["[nothing, nothing].join", "''"],
545
- ["[1, 2].select(&:even?)", "[2]"],
546
- ["[1, 2].reject(&:even?)", "[1]"],
547
- ["a = [1, 2] a.select!(&:even?) a", "[2]"],
548
- ["a = [1, 2] a.reject!(&:even?) a", "[1]"],
549
- ["[1, 2].map(&:even?)", "[false, true]"]
550
- ] +
551
- [
552
- ["a = [1, 2] a.map!(&:even?) a", "[false, true]"],
553
- ["Html.p { \"Hello\" }.to_html", "'<p>Hello</p>'"],
554
- %w[Html.br.to_html '<br>'],
555
- [
556
- "Html.div { Html.div { Html.span { :Nested } } }.to_html",
557
- "'<div><div><span>Nested</span></div></div>'"
558
- ],
559
- [
560
- "Html.div { Html.p { Html.span { :hello } } }.to_html",
561
- "'<div><p><span>hello</span></p></div>'"
562
- ],
563
- [
564
- "Html.join([Html.p { :hello }, Html.p { :world }], Html.br).to_html",
565
- "'<p>hello</p><br><p>world</p>'"
566
- ],
567
- %w[Html.join.to_html ''],
568
- [
569
- "Html.div { Html.text(\"<span>\") }.to_html",
570
- "'<div>&lt;span&gt;</div>'"
571
- ],
572
- [
573
- "Html.div { Html.raw(\"<span>ok</span>\") }.to_html",
574
- "'<div><span>ok</span></div>'"
575
- ],
576
- ["Html.escape(\"<span>&</span>\")", "'&lt;span&gt;&amp;&lt;/span&gt;'"],
577
- ["Html.unescape(\"A&nbsp;&nbsp;B &amp; C\")", "'A  B & C'"],
578
- ["[1, 2, 3].any?", "true"],
579
- ["[1, 2, 3].any?(&:even?)", "true"],
580
- ["[1, 2, 3].none?", "false"],
581
- ["[1, 2, 3].none?(&:even?)", "false"],
582
- ["subject = 1 { subject }", "{ subject: 1 }"],
583
- ["subject = 1 { subject: }", "{ subject: 1 }"],
584
- ["'{1} {2}'", "'1 2'"],
585
- ['Time.zone = "Etc/UTC"', '"Etc/UTC"'],
586
- %w[Json.parse("1") 1],
587
- %w[{a:1}.to_query "a=1"],
588
- ["dorian = 1 dorian#.to_something", "1"],
589
- ["", ""]
590
- ]
591
-
592
- examples.each do |input, expected|
593
- it "#{input} == #{expected}" do
594
- formatted = format_input(input)
595
-
596
- output = StringIO.new
597
- code_input = described_class.evaluate(input, output: output)
598
- code_expected = described_class.evaluate(expected)
599
- expect(code_input).to eq(code_expected)
600
- expect(output.string).to eq("")
601
-
602
- formatted_output = StringIO.new
603
- formatted_result =
604
- described_class.evaluate(formatted, output: formatted_output)
605
- expect(formatted_result).to eq(code_input)
606
- expect(formatted_output.string).to eq("")
607
- next if code_input.is_a?(Code::Object::Decimal)
608
-
609
- expect(code_input.to_json).to eq(code_expected.to_json)
610
- YAML.unsafe_load(code_input.to_yaml)
611
- YAML.unsafe_load(code_expected.to_yaml)
612
- end
613
- end
614
-
615
- [
616
- ["puts(true)", "true\n"],
617
- %w[print(false) false],
618
- ["[1].each { break } puts(:end)", "end\n"]
619
- ].each do |input, expected|
620
- it "#{input} prints #{expected}" do
621
- formatted = format_input(input)
622
-
623
- output = StringIO.new
624
- described_class.evaluate(input, output: output)
625
- expect(output.string).to eq(expected)
626
-
627
- formatted_output = StringIO.new
628
- described_class.evaluate(formatted, output: formatted_output)
629
- expect(formatted_output.string).to eq(expected)
630
- end
631
- end
632
-
633
- it "doesn't crash with dictionnary as parameter" do
634
- input = <<~INPUT
635
- [
636
- {
637
- videos: [{}]
638
- },
639
- {
640
- videos: [{}]
641
- }
642
- ].map do |post|
643
- post.videos.map { |video| }
644
- end
645
- INPUT
646
- described_class.evaluate(input)
647
- described_class.evaluate(format_input(input))
648
- end
649
-
650
- it "doesn't crash with functions" do
651
- input = <<~INPUT
652
- send! = (subject:) => { subject }
653
-
654
- send!(subject: "pomodoro start")
655
- send!(subject: "pomodoro break")
656
- send!(subject: "pomodoro start")
657
- send!(subject: "pomodoro break")
658
- INPUT
659
- described_class.evaluate(input)
660
- described_class.evaluate(format_input(input))
661
- end
662
-
663
- it "raises for undefined constant receiver assignment" do
664
- expect do
665
- described_class.evaluate("UnknownConstant.zone = 1")
666
- end.to raise_error(Code::Error, /UnknownConstant is not defined/)
667
- end
668
-
669
- it "parses ics events into dictionaries" do
670
- result = described_class.evaluate(<<~CODE)
671
- events = Ics.parse("BEGIN:VCALENDAR
672
- VERSION:2.0
673
- PRODID:-//code-ruby//EN
674
- BEGIN:VEVENT
675
- UID:event-1
676
- DTSTART:20260327T120000Z
677
- DTEND:20260327T133000Z
678
- SUMMARY:Lunch
679
- DESCRIPTION:Team lunch
680
- LOCATION:Paris
681
- URL:https://example.com/events/1
682
- CATEGORIES:food,team
683
- END:VEVENT
684
- END:VCALENDAR")
685
-
686
- {
687
- size: events.size,
688
- summary: events.first.summary,
689
- location: events.first.location,
690
- uid: events.first.uid,
691
- categories: events.first.categories,
692
- starts_at: events.first.starts_at.to_string,
693
- ends_at: events.first.ends_at.to_string,
694
- all_day: events.first.all_day
695
- }
696
- CODE
697
-
698
- expect(result).to eq(
699
- {
700
- size: 1,
701
- summary: "Lunch",
702
- location: "Paris",
703
- uid: "event-1",
704
- categories: %w[food team],
705
- starts_at: "2026-03-27 12:00:00 UTC",
706
- ends_at: "2026-03-27 13:30:00 UTC",
707
- all_day: false
708
- }.to_code
709
- )
710
- end
711
-
712
- it "returns an empty list for invalid ics" do
713
- expect(described_class.evaluate('Ics.parse("not an ics file")')).to eq(
714
- [].to_code
715
- )
716
- end
717
- end