code-ruby 0.3.1 → 0.5.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 (206) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/CHANGELOG.md +5 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +28 -55
  6. data/TODO +17 -0
  7. data/bin/code +56 -31
  8. data/bin/format +3 -0
  9. data/bin/template +62 -20
  10. data/bin/test +17 -0
  11. data/code-ruby.gemspec +1 -6
  12. data/docs/class.code +9 -0
  13. data/docs/euler/1.template +1 -5
  14. data/docs/euler/5.template +0 -1
  15. data/docs/meetup.code +12 -0
  16. data/docs/precedence.template +6 -39
  17. data/docs/rain.code +22 -0
  18. data/docs/slack.code +17 -0
  19. data/docs/stripe.code +7 -0
  20. data/docs/twitter.code +9 -0
  21. data/language-ruby.gemspec +18 -0
  22. data/lib/code/error.rb +3 -0
  23. data/lib/code/node/base_10.rb +29 -0
  24. data/lib/code/node/base_16.rb +13 -0
  25. data/lib/code/node/base_2.rb +13 -0
  26. data/lib/code/node/base_8.rb +13 -0
  27. data/lib/code/node/boolean.rb +7 -7
  28. data/lib/code/node/call.rb +32 -37
  29. data/lib/code/node/call_argument.rb +11 -27
  30. data/lib/code/node/chained_call.rb +10 -25
  31. data/lib/code/node/code.rb +4 -6
  32. data/lib/code/node/decimal.rb +26 -0
  33. data/lib/code/node/dictionnary.rb +20 -9
  34. data/lib/code/node/equal.rb +18 -20
  35. data/lib/code/node/function.rb +10 -7
  36. data/lib/code/node/function_parameter.rb +31 -0
  37. data/lib/code/node/if.rb +36 -32
  38. data/lib/code/node/if_modifier.rb +35 -36
  39. data/lib/code/node/list.rb +6 -9
  40. data/lib/code/node/negation.rb +5 -23
  41. data/lib/code/node/not.rb +15 -0
  42. data/lib/code/node/nothing.rb +1 -1
  43. data/lib/code/node/number.rb +14 -12
  44. data/lib/code/node/operation.rb +21 -16
  45. data/lib/code/node/power.rb +10 -6
  46. data/lib/code/node/rescue.rb +4 -3
  47. data/lib/code/node/splat.rb +15 -0
  48. data/lib/code/node/statement.rb +47 -69
  49. data/lib/code/node/string.rb +41 -10
  50. data/lib/code/node/ternary.rb +7 -9
  51. data/lib/code/node/unary_minus.rb +5 -12
  52. data/lib/code/node/while.rb +17 -24
  53. data/lib/code/node.rb +7 -8
  54. data/lib/code/object/argument.rb +3 -12
  55. data/lib/code/object/decimal.rb +114 -27
  56. data/lib/code/object/dictionnary.rb +29 -12
  57. data/lib/code/object/function.rb +23 -24
  58. data/lib/code/object/global.rb +42 -0
  59. data/lib/code/object/integer.rb +142 -35
  60. data/lib/code/object/list.rb +74 -95
  61. data/lib/code/object/range.rb +38 -50
  62. data/lib/code/object/ruby_function.rb +31 -0
  63. data/lib/code/object/string.rb +36 -16
  64. data/lib/code/object.rb +114 -54
  65. data/lib/code/parser/addition.rb +12 -20
  66. data/lib/code/parser/and_operator.rb +9 -20
  67. data/lib/code/parser/bitwise_and.rb +9 -20
  68. data/lib/code/parser/bitwise_or.rb +12 -20
  69. data/lib/code/parser/boolean.rb +10 -7
  70. data/lib/code/parser/call.rb +92 -60
  71. data/lib/code/parser/chained_call.rb +47 -0
  72. data/lib/code/parser/class.rb +45 -0
  73. data/lib/code/parser/code.rb +17 -10
  74. data/lib/code/parser/dictionnary.rb +56 -30
  75. data/lib/code/parser/equal.rb +87 -35
  76. data/lib/code/parser/equality.rb +23 -24
  77. data/lib/code/parser/equality_lower.rb +9 -0
  78. data/lib/code/parser/function.rb +67 -40
  79. data/lib/code/parser/greater.rb +25 -0
  80. data/lib/code/parser/group.rb +13 -8
  81. data/lib/code/parser/if.rb +51 -21
  82. data/lib/code/parser/if_modifier.rb +43 -16
  83. data/lib/code/parser/list.rb +32 -19
  84. data/lib/code/parser/multiplication.rb +15 -20
  85. data/lib/code/parser/name.rb +96 -84
  86. data/lib/code/parser/negation.rb +20 -9
  87. data/lib/code/parser/not_keyword.rb +14 -12
  88. data/lib/code/parser/nothing.rb +13 -8
  89. data/lib/code/parser/number.rb +124 -68
  90. data/lib/code/parser/operation.rb +35 -0
  91. data/lib/code/parser/or_keyword.rb +12 -20
  92. data/lib/code/parser/or_operator.rb +9 -20
  93. data/lib/code/parser/power.rb +32 -14
  94. data/lib/code/parser/range.rb +9 -17
  95. data/lib/code/parser/rescue.rb +29 -13
  96. data/lib/code/parser/shift.rb +11 -21
  97. data/lib/code/parser/splat.rb +31 -0
  98. data/lib/code/parser/statement.rb +4 -3
  99. data/lib/code/parser/string.rb +53 -62
  100. data/lib/code/parser/ternary.rb +36 -15
  101. data/lib/code/parser/unary_minus.rb +23 -5
  102. data/lib/code/parser/while.rb +26 -15
  103. data/lib/code/parser/whitespace.rb +49 -0
  104. data/lib/code/parser.rb +15 -0
  105. data/lib/code/ruby.rb +162 -0
  106. data/lib/code-ruby.rb +2 -5
  107. data/lib/code.rb +24 -7
  108. data/lib/language/atom.rb +343 -0
  109. data/lib/language/output.rb +130 -0
  110. data/lib/language/parser/absent/present.rb +8 -0
  111. data/lib/language/parser/absent.rb +6 -0
  112. data/lib/language/parser/end_of_input.rb +6 -0
  113. data/lib/language/parser/interuption.rb +38 -0
  114. data/lib/language/parser/not_end_of_input.rb +6 -0
  115. data/lib/language/parser/str/not_found.rb +16 -0
  116. data/lib/language/parser/str.rb +6 -0
  117. data/lib/language/parser.rb +53 -0
  118. data/lib/language-ruby.rb +10 -0
  119. data/lib/language.rb +80 -0
  120. data/lib/template/node/code_part.rb +1 -1
  121. data/lib/template/node/part.rb +1 -1
  122. data/lib/template/node/template.rb +1 -1
  123. data/lib/template/node/text_part.rb +1 -1
  124. data/lib/template/node.rb +1 -1
  125. data/lib/template/parser/template.rb +26 -17
  126. data/lib/template/parser.rb +15 -0
  127. data/lib/template/version.rb +1 -1
  128. data/lib/template-ruby.rb +2 -5
  129. data/lib/template.rb +23 -13
  130. data/spec/code/addition_spec.rb +13 -0
  131. data/spec/code/and_operator_spec.rb +13 -0
  132. data/spec/code/bitwise_and_spec.rb +13 -0
  133. data/spec/code/bitwise_or_spec.rb +13 -0
  134. data/spec/code/boolean_spec.rb +13 -0
  135. data/spec/code/call_spec.rb +21 -0
  136. data/spec/code/chained_call_spec.rb +16 -0
  137. data/spec/code/dictionnary_spec.rb +17 -0
  138. data/spec/code/equal_spec.rb +26 -0
  139. data/spec/code/equality_spec.rb +13 -0
  140. data/spec/code/function_spec.rb +18 -0
  141. data/spec/code/greater_spec.rb +18 -0
  142. data/spec/code/group_spec.rb +12 -0
  143. data/spec/code/if_modifier_spec.rb +20 -0
  144. data/spec/code/if_spec.rb +25 -0
  145. data/spec/code/list_spec.rb +17 -0
  146. data/spec/code/multiplication_spec.rb +18 -0
  147. data/spec/code/negation_spec.rb +20 -0
  148. data/spec/code/not_keyword_spec.rb +13 -0
  149. data/spec/code/nothing_spec.rb +17 -0
  150. data/spec/code/number_spec.rb +22 -0
  151. data/spec/code/or_keyword_spec.rb +17 -0
  152. data/spec/code/or_operator_spec.rb +16 -0
  153. data/spec/code/parser/boolean_spec.rb +5 -7
  154. data/spec/code/parser/call_spec.rb +16 -56
  155. data/spec/code/parser/chained_call.rb +17 -0
  156. data/spec/code/parser/dictionnary_spec.rb +8 -9
  157. data/spec/code/parser/function_spec.rb +5 -21
  158. data/spec/code/parser/group_spec.rb +18 -0
  159. data/spec/code/parser/list_spec.rb +9 -20
  160. data/spec/code/parser/number_spec.rb +4 -109
  161. data/spec/code/parser/string_spec.rb +9 -17
  162. data/spec/code/parser_spec.rb +23 -0
  163. data/spec/code/power_spec.rb +13 -0
  164. data/spec/code/range_spec.rb +16 -0
  165. data/spec/code/rescue_spec.rb +13 -0
  166. data/spec/code/shift_spec.rb +13 -0
  167. data/spec/code/splat_spec.rb +13 -0
  168. data/spec/code/string_spec.rb +25 -0
  169. data/spec/code/ternary_spec.rb +18 -0
  170. data/spec/code/unary_minus_spec.rb +13 -0
  171. data/spec/code/while_spec.rb +18 -0
  172. data/spec/spec_helper.rb +4 -1
  173. data/template-ruby.gemspec +2 -7
  174. metadata +113 -99
  175. data/lib/code/node/base_10_decimal.rb +0 -32
  176. data/lib/code/node/base_10_integer.rb +0 -32
  177. data/lib/code/node/base_10_number.rb +0 -19
  178. data/lib/code/node/base_16_number.rb +0 -19
  179. data/lib/code/node/base_2_number.rb +0 -19
  180. data/lib/code/node/base_8_number.rb +0 -19
  181. data/lib/code/node/block.rb +0 -17
  182. data/lib/code/node/defined.rb +0 -19
  183. data/lib/code/node/dictionnary_key_value.rb +0 -23
  184. data/lib/code/node/function_argument.rb +0 -45
  185. data/lib/code/node/group.rb +0 -13
  186. data/lib/code/node/keyword_call_argument.rb +0 -30
  187. data/lib/code/node/keyword_function_argument.rb +0 -33
  188. data/lib/code/node/name.rb +0 -55
  189. data/lib/code/node/not_keyword.rb +0 -13
  190. data/lib/code/node/or_keyword.rb +0 -34
  191. data/lib/code/node/range.rb +0 -31
  192. data/lib/code/node/regular_call_argument.rb +0 -34
  193. data/lib/code/node/regular_function_argument.rb +0 -36
  194. data/lib/code/node/string_characters.rb +0 -13
  195. data/lib/code/node/string_component.rb +0 -23
  196. data/lib/code/node/string_interpolation.rb +0 -13
  197. data/lib/code/parser/defined.rb +0 -20
  198. data/lib/code/parser/greater_than.rb +0 -33
  199. data/spec/call_spec.rb +0 -22
  200. data/spec/code/error/type_error_spec.rb +0 -63
  201. data/spec/code/parser/name_spec.rb +0 -15
  202. data/spec/code/parser/nothing_spec.rb +0 -19
  203. data/spec/code_spec.rb +0 -120
  204. data/spec/function_spec.rb +0 -26
  205. data/spec/template/parser/template_spec.rb +0 -19
  206. data/spec/template_spec.rb +0 -33
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "if" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["if true 1", "1"],
9
+ ["unless false 1", "1"],
10
+ ["if false 1", ""],
11
+ ["unless true 1", ""],
12
+ ["if false 1 else 2", "2"],
13
+ ["if false 1 elsif true 2", "2"],
14
+ ["if false 1 elsif false 2", ""],
15
+ ["if false 1 else if true 2", "2"],
16
+ ["if false 1 else if false 2", ""],
17
+ ["if false 1 else unless false 2", "2"],
18
+ ["if false 1 else unless true 2", ""]
19
+ ].each do |input, output|
20
+ context input do
21
+ let(:input) { input }
22
+ it { expect(subject).to eq(output) }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "list" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["[]", "[]"],
9
+ ["[1, 2, 3]", "[1, 2, 3]"],
10
+ ["[[true]]", "[[true]]"]
11
+ ].each do |input, output|
12
+ context input do
13
+ let(:input) { input }
14
+ it { expect(subject).to eq(output) }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "multiplication" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["2 * 3", "6"],
9
+ ["2 * 3 + 2", "8"],
10
+ ["2 / 4", "0.5"],
11
+ ["4 % 3", "1"]
12
+ ].each do |input, output|
13
+ context input do
14
+ let(:input) { input }
15
+ it { expect(subject).to eq(output) }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "function" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ %w[!false true],
9
+ %w[!!true true],
10
+ %w[!!1 true],
11
+ %w[+1 1],
12
+ %w[+1.0 1.0],
13
+ %w[+true true]
14
+ ].each do |input, output|
15
+ context input do
16
+ let(:input) { input }
17
+ it { expect(subject).to eq(output) }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "not keyword" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [["not true", "false"], ["not not false", "false"]].each do |input, output|
8
+ context input do
9
+ let(:input) { input }
10
+ it { expect(subject).to eq(output) }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "nothing" do
4
+ subject { Code.evaluate(input).to_s }
5
+
6
+ [
7
+ ["nothing", ""],
8
+ ["null", ""],
9
+ ["nil", ""],
10
+ ["nothing null", ""]
11
+ ].each do |input, output|
12
+ context input do
13
+ let(:input) { input }
14
+ it { expect(subject).to eq(output) }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "number" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ %w[0 0],
9
+ %w[1.1 1.1],
10
+ %w[0x10 16],
11
+ %w[0o10 8],
12
+ %w[0b10 2],
13
+ %w[1e1 10],
14
+ %w[1.0e1 10.0],
15
+ %w[10e1.0 100.0]
16
+ ].each do |input, output|
17
+ context input do
18
+ let(:input) { input }
19
+ it { expect(subject).to eq(output) }
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "or keyword" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["true or false", "true"],
9
+ ["true and false", "false"],
10
+ ["random = 1 random", "1"]
11
+ ].each do |input, output|
12
+ context input do
13
+ let(:input) { input }
14
+ it { expect(subject).to eq(output) }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "or operator" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["true || false", "true"],
9
+ ["false || false", "false"]
10
+ ].each do |input, output|
11
+ context input do
12
+ let(:input) { input }
13
+ it { expect(subject).to eq(output) }
14
+ end
15
+ end
16
+ end
@@ -1,18 +1,16 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Code::Parser::Boolean do
4
- subject { described_class.new.parse(input) }
4
+ subject { Code::Parser.parse(input) }
5
5
 
6
6
  [
7
- ["true", { boolean: "true" }],
8
- ["false", { boolean: "false" }],
9
- ].each do |(input, expected)|
7
+ ["true", [{ boolean: "true" }]],
8
+ ["false", [{ boolean: "false" }]]
9
+ ].each do |input, output|
10
10
  context input.inspect do
11
11
  let(:input) { input }
12
12
 
13
- it "succeeds" do
14
- expect(subject).to eq(expected)
15
- end
13
+ it { expect(subject).to eq(output) }
16
14
  end
17
15
  end
18
16
  end
@@ -1,66 +1,26 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Code::Parser::Call do
4
- subject { described_class.new.parse(input) }
4
+ subject { Code::Parser.parse(input) }
5
5
 
6
6
  [
7
- [
8
- "user.first_name",
9
- { call: { left: { name: "user" }, right: [{ name: "first_name" }] } },
10
- ],
11
- [
12
- "3.times",
13
- {
14
- call: {
15
- left: {
16
- number: {
17
- base_10: {
18
- integer: {
19
- whole: "3",
20
- },
21
- },
22
- },
23
- },
24
- right: [{ name: "times" }],
25
- },
26
- },
27
- ],
28
- ].each do |(input, expected)|
29
- context input.inspect do
30
- let(:input) { input }
31
-
32
- it "succeeds" do
33
- expect(subject).to eq(expected)
34
- end
35
- end
36
- end
37
-
38
- [
39
- "User.first",
40
- "User.first(10)",
41
- 'User.find_by(name: "Dorian")',
42
- "User.update_all(**attributes)",
43
- "User.each(&block)",
44
- "user.update(*args)",
45
- "sort([1, 2, 3], :asc)",
46
- "render()",
47
- "render(item)",
48
- "Renderer.render(item)",
49
- "render(item) { 'Hello' }",
50
- "render(item) { |item| item * 2 }",
51
- "render(item) { |item1, item2| item1 + item2 }",
52
- "render(item) do 'Hello' end",
53
- "render(item) do |item| item * 2 end",
54
- "render(item) do |item1, item2| item1 + item2 end",
55
- "(1..2).any?(&:even?)",
56
- "(2..5).select { |i| i.even? }.any? { |n| n.even? }",
7
+ "a",
8
+ "hello",
9
+ "hello()",
10
+ "render(user)",
11
+ 'render(user, first_name: "Dorian")',
12
+ "render { }",
13
+ "render do end",
14
+ "render { |name| name.upcase }",
15
+ "render(user) { |name| name.upcase }",
16
+ "render(user) do |name| name.upcase end",
17
+ 'render { "Dorian" }',
18
+ 'render(user) { "Dorian" }',
19
+ 'render(user) do "Dorian" end'
57
20
  ].each do |input|
58
- context input.inspect do
21
+ context input do
59
22
  let(:input) { input }
60
-
61
- it "succeeds" do
62
- expect { subject }.to_not raise_error
63
- end
23
+ it { subject }
64
24
  end
65
25
  end
66
26
  end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Code::Parser::ChainedCall do
4
+ subject { Code::Parser.parse(input) }
5
+
6
+ [
7
+ "a.b",
8
+ "user.first_name",
9
+ "user.admin?",
10
+ 'user.update!(name: "Dorian")'
11
+ ].each do |input|
12
+ context input do
13
+ let(:input) { input }
14
+ it { subject }
15
+ end
16
+ end
17
+ end
@@ -1,19 +1,18 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Code::Parser::Dictionnary do
4
- subject { described_class.new.parse(input) }
4
+ subject { Code::Parser.parse(input) }
5
5
 
6
6
  [
7
- '{name: "Dorian"}',
8
- '{a: true, "b": false}',
9
- "{ true => 1, false => 2}",
7
+ "{}",
8
+ "{ }",
9
+ "{/* comment */}",
10
+ "{ a: 1, b: 2, c: 3 }",
11
+ '{ "first_name": "Dorian" }'
10
12
  ].each do |input|
11
- context input.inspect do
13
+ context input do
12
14
  let(:input) { input }
13
-
14
- it "succeeds" do
15
- expect { subject }.to_not raise_error
16
- end
15
+ it { subject }
17
16
  end
18
17
  end
19
18
  end
@@ -1,32 +1,16 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Code::Parser::Function do
4
- subject { described_class.new.parse(input) }
4
+ subject { Code::Parser.parse(input) }
5
5
 
6
6
  [
7
7
  "() => {}",
8
- '() => { "Hello" }',
9
- "(a) => { }",
10
- "(a = 1)=> {}",
11
- "(a, b) =>{}",
12
- "(a, b = 2, c = b)=>{}",
13
- "(a:)=>{}",
14
- "(a:, b:)=>{}",
15
- "(a, b:, c)=>{}",
16
- "(a:, b: 1)=>{}",
17
- "(a, b: 1, c)=>{}",
18
- "(*args)=>{}",
19
- "(*args, **kargs)=>{}",
20
- "(&block)=>{}",
21
- "(a = b = 1 + 1 b)=>{}",
22
- "(a: b = 1 + 1 b)=>{}",
8
+ "(a, b) => { add(a, b) }",
9
+ "(a:, b:) => { add(a, b) }"
23
10
  ].each do |input|
24
- context input.inspect do
11
+ context input do
25
12
  let(:input) { input }
26
-
27
- it "succeeds" do
28
- expect { subject }.to_not raise_error
29
- end
13
+ it { subject }
30
14
  end
31
15
  end
32
16
  end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Code::Parser::Group do
4
+ subject { Code::Parser.parse(input) }
5
+
6
+ [
7
+ [
8
+ "(true (nothing))",
9
+ [{ group: [{ boolean: "true" }, { group: [{ nothing: "nothing" }] }] }]
10
+ ]
11
+ ].each do |input, output|
12
+ context input.inspect do
13
+ let(:input) { input }
14
+
15
+ it { expect(subject).to eq(output) }
16
+ end
17
+ end
18
+ end
@@ -1,29 +1,18 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Code::Parser::List do
4
- subject { described_class.new.parse(input) }
4
+ subject { Code::Parser.parse(input) }
5
5
 
6
6
  [
7
- [
8
- "[true, false]",
9
- {
10
- list: [
11
- { code: [{ boolean: "true" }] },
12
- { code: [{ boolean: "false" }] },
13
- ],
14
- },
15
- ],
16
- [
17
- "[nothing, a]",
18
- { list: [{ code: [{ nothing: "nothing" }] }, { code: [{ name: "a" }] }] },
19
- ],
20
- ].each do |(input, expected)|
21
- context input.inspect do
7
+ "[]",
8
+ "[ ]",
9
+ "[/* comment */]",
10
+ "[1, 2, 3]",
11
+ "['hello', 1, true, [false, nothing]]"
12
+ ].each do |input|
13
+ context input do
22
14
  let(:input) { input }
23
-
24
- it "succeeds" do
25
- expect(subject).to eq(expected)
26
- end
15
+ it { subject }
27
16
  end
28
17
  end
29
18
  end
@@ -1,117 +1,12 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Code::Parser::Number do
4
- subject { described_class.new.parse(input) }
4
+ subject { Code::Parser.parse(input) }
5
5
 
6
- [
7
- ["1", { number: { base_10: { integer: { whole: "1" } } } }],
8
- ["3", { number: { base_10: { integer: { whole: "3" } } } }],
9
- ["1.0", { number: { base_10: { decimal: { whole: "1", decimal: "0" } } } }],
10
- [
11
- "-1.0",
12
- {
13
- number: {
14
- base_10: {
15
- decimal: {
16
- sign: "-",
17
- whole: "1",
18
- decimal: "0",
19
- },
20
- },
21
- },
22
- },
23
- ],
24
- [
25
- "+1.0",
26
- {
27
- number: {
28
- base_10: {
29
- decimal: {
30
- sign: "+",
31
- whole: "1",
32
- decimal: "0",
33
- },
34
- },
35
- },
36
- },
37
- ],
38
- ["0", { number: { base_10: { integer: { whole: "0" } } } }],
39
- ["+0", { number: { base_10: { integer: { sign: "+", whole: "0" } } } }],
40
- ["-0", { number: { base_10: { integer: { sign: "-", whole: "0" } } } }],
41
- ["0b010", { number: { base_2: "010" } }],
42
- ["0o01234567", { number: { base_8: "01234567" } }],
43
- [
44
- "0x0123456789aAbBcCdDeEfF",
45
- { number: { base_16: "0123456789aAbBcCdDeEfF" } },
46
- ],
47
- [
48
- "10e20",
49
- {
50
- number: {
51
- base_10: {
52
- integer: {
53
- whole: "10",
54
- exponent: {
55
- integer: {
56
- whole: "20",
57
- },
58
- },
59
- },
60
- },
61
- },
62
- },
63
- ],
64
- [
65
- "10.34e23.45",
66
- {
67
- number: {
68
- base_10: {
69
- decimal: {
70
- whole: "10",
71
- decimal: "34",
72
- exponent: {
73
- decimal: {
74
- whole: "23",
75
- decimal: "45",
76
- },
77
- },
78
- },
79
- },
80
- },
81
- },
82
- ],
83
- [
84
- "+10e-20e1.0",
85
- {
86
- number: {
87
- base_10: {
88
- integer: {
89
- sign: "+",
90
- whole: "10",
91
- exponent: {
92
- integer: {
93
- sign: "-",
94
- whole: "20",
95
- exponent: {
96
- decimal: {
97
- whole: "1",
98
- decimal: "0",
99
- },
100
- },
101
- },
102
- },
103
- },
104
- },
105
- },
106
- },
107
- ],
108
- ].each do |(input, expected)|
109
- context input.inspect do
6
+ %w[0 1 1.1 123.123 1_000.12_244 0xf0 0o70 0b10].each do |input|
7
+ context input do
110
8
  let(:input) { input }
111
-
112
- it "succeeds" do
113
- expect(subject).to eq(expected)
114
- end
9
+ it { subject }
115
10
  end
116
11
  end
117
12
  end
@@ -1,29 +1,21 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Code::Parser::String do
4
- subject { described_class.new.parse(input) }
4
+ subject { Code::Parser.parse(input) }
5
5
 
6
6
  [
7
- "'hello'",
8
- '"hello"',
9
7
  "''",
10
8
  '""',
11
- "'\\''",
12
- '"\\t"',
13
- "'\\r'",
14
- '"\\b\\f\\n\\r\\t"',
15
- '"\\uABCG"',
16
- "'\\u0123\\u4567\\u89aA\\ubBcC\\UdDeE\\ufFfF'",
17
- ":asc",
18
- "'1 + 1 = {1 + 1}'",
19
- "'a + b = {'{'a'}{'b'}'}'"
9
+ "'Hello Dorian'",
10
+ '"Hello Dorian"',
11
+ "'Hello \\{name}'",
12
+ '"Hello \\{name}"',
13
+ "'Hello {name}'",
14
+ '"Hello {name}"'
20
15
  ].each do |input|
21
- context input.inspect do
16
+ context input do
22
17
  let(:input) { input }
23
-
24
- it "succeeds" do
25
- expect { subject }.to_not raise_error
26
- end
18
+ it { subject }
27
19
  end
28
20
  end
29
21
  end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "Code::Parser" do
4
+ subject { Code::Parser.parse(input) }
5
+
6
+ [
7
+ ["", []],
8
+ [" ", []],
9
+ ["\n", []],
10
+ ["nothing", [{ nothing: "nothing" }]],
11
+ [" nothing ", [{ nothing: "nothing" }]],
12
+ [
13
+ "nothing null nil",
14
+ [{ nothing: "nothing" }, { nothing: "null" }, { nothing: "nil" }]
15
+ ]
16
+ ].each do |input, output|
17
+ context input.inspect do
18
+ let(:input) { input }
19
+
20
+ it { expect(subject).to eq(output) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "function" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [["2 ** 3", "8"], ["2 ** 3 ** 2", "512"]].each do |input, output|
8
+ context input do
9
+ let(:input) { input }
10
+ it { expect(subject).to eq(output) }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "range" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["(0..1).to_list", "[0, 1]"],
9
+ ["(0...1).to_list", "[0]"]
10
+ ].each do |input, output|
11
+ context input do
12
+ let(:input) { input }
13
+ it { expect(subject).to eq(output) }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "rescue" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [["a rescue 'oops'", "oops"]].each do |input, output|
8
+ context input do
9
+ let(:input) { input }
10
+ it { expect(subject).to eq(output) }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "shift" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [["1 >> 1", "0"], ["1 << 1", "2"]].each do |input, output|
8
+ context input do
9
+ let(:input) { input }
10
+ it { expect(subject).to eq(output) }
11
+ end
12
+ end
13
+ end