code-ruby 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/Gemfile +2 -1
  4. data/Gemfile.lock +27 -54
  5. data/TODO +17 -0
  6. data/bin/code +56 -31
  7. data/bin/format +3 -0
  8. data/bin/template +62 -20
  9. data/bin/test +17 -0
  10. data/code-ruby.gemspec +1 -3
  11. data/docs/class.code +9 -0
  12. data/docs/euler/1.template +1 -5
  13. data/docs/euler/5.template +0 -1
  14. data/docs/meetup.code +12 -0
  15. data/docs/precedence.template +6 -39
  16. data/docs/rain.code +22 -0
  17. data/docs/slack.code +17 -0
  18. data/docs/stripe.code +7 -0
  19. data/docs/twitter.code +9 -0
  20. data/language-ruby.gemspec +18 -0
  21. data/lib/code/node/base_10.rb +29 -0
  22. data/lib/code/node/base_16.rb +13 -0
  23. data/lib/code/node/base_2.rb +13 -0
  24. data/lib/code/node/base_8.rb +13 -0
  25. data/lib/code/node/boolean.rb +7 -7
  26. data/lib/code/node/call.rb +32 -38
  27. data/lib/code/node/call_argument.rb +11 -27
  28. data/lib/code/node/chained_call.rb +10 -27
  29. data/lib/code/node/code.rb +4 -6
  30. data/lib/code/node/decimal.rb +26 -0
  31. data/lib/code/node/dictionnary.rb +20 -9
  32. data/lib/code/node/equal.rb +18 -20
  33. data/lib/code/node/function.rb +10 -7
  34. data/lib/code/node/function_parameter.rb +31 -0
  35. data/lib/code/node/if.rb +36 -32
  36. data/lib/code/node/if_modifier.rb +35 -36
  37. data/lib/code/node/list.rb +6 -8
  38. data/lib/code/node/negation.rb +5 -23
  39. data/lib/code/node/not.rb +15 -0
  40. data/lib/code/node/nothing.rb +1 -1
  41. data/lib/code/node/number.rb +14 -12
  42. data/lib/code/node/operation.rb +21 -16
  43. data/lib/code/node/power.rb +10 -6
  44. data/lib/code/node/rescue.rb +4 -3
  45. data/lib/code/node/splat.rb +15 -0
  46. data/lib/code/node/statement.rb +48 -70
  47. data/lib/code/node/string.rb +42 -16
  48. data/lib/code/node/ternary.rb +7 -9
  49. data/lib/code/node/unary_minus.rb +5 -12
  50. data/lib/code/node/while.rb +17 -24
  51. data/lib/code/node.rb +7 -8
  52. data/lib/code/object/argument.rb +2 -11
  53. data/lib/code/object/decimal.rb +45 -30
  54. data/lib/code/object/dictionnary.rb +6 -5
  55. data/lib/code/object/function.rb +20 -23
  56. data/lib/code/object/global.rb +11 -6
  57. data/lib/code/object/integer.rb +73 -30
  58. data/lib/code/object/list.rb +40 -34
  59. data/lib/code/object/range.rb +18 -17
  60. data/lib/code/object/ruby_function.rb +12 -6
  61. data/lib/code/object/string.rb +22 -12
  62. data/lib/code/object.rb +82 -24
  63. data/lib/code/parser/addition.rb +12 -20
  64. data/lib/code/parser/and_operator.rb +9 -20
  65. data/lib/code/parser/bitwise_and.rb +9 -20
  66. data/lib/code/parser/bitwise_or.rb +12 -20
  67. data/lib/code/parser/boolean.rb +10 -7
  68. data/lib/code/parser/call.rb +92 -60
  69. data/lib/code/parser/chained_call.rb +47 -0
  70. data/lib/code/parser/class.rb +45 -0
  71. data/lib/code/parser/code.rb +17 -10
  72. data/lib/code/parser/dictionnary.rb +56 -30
  73. data/lib/code/parser/equal.rb +87 -35
  74. data/lib/code/parser/equality.rb +23 -24
  75. data/lib/code/parser/equality_lower.rb +9 -0
  76. data/lib/code/parser/function.rb +67 -42
  77. data/lib/code/parser/greater.rb +25 -0
  78. data/lib/code/parser/group.rb +13 -8
  79. data/lib/code/parser/if.rb +51 -21
  80. data/lib/code/parser/if_modifier.rb +43 -16
  81. data/lib/code/parser/list.rb +32 -19
  82. data/lib/code/parser/multiplication.rb +15 -20
  83. data/lib/code/parser/name.rb +96 -84
  84. data/lib/code/parser/negation.rb +20 -9
  85. data/lib/code/parser/not_keyword.rb +14 -12
  86. data/lib/code/parser/nothing.rb +13 -8
  87. data/lib/code/parser/number.rb +124 -68
  88. data/lib/code/parser/operation.rb +35 -0
  89. data/lib/code/parser/or_keyword.rb +12 -20
  90. data/lib/code/parser/or_operator.rb +9 -20
  91. data/lib/code/parser/power.rb +32 -14
  92. data/lib/code/parser/range.rb +9 -17
  93. data/lib/code/parser/rescue.rb +29 -13
  94. data/lib/code/parser/shift.rb +11 -21
  95. data/lib/code/parser/splat.rb +31 -0
  96. data/lib/code/parser/statement.rb +4 -3
  97. data/lib/code/parser/string.rb +53 -62
  98. data/lib/code/parser/ternary.rb +36 -15
  99. data/lib/code/parser/unary_minus.rb +23 -5
  100. data/lib/code/parser/while.rb +26 -15
  101. data/lib/code/parser/whitespace.rb +49 -0
  102. data/lib/code/parser.rb +15 -0
  103. data/lib/code/ruby.rb +13 -12
  104. data/lib/code-ruby.rb +2 -11
  105. data/lib/code.rb +16 -13
  106. data/lib/language/atom.rb +343 -0
  107. data/lib/language/output.rb +130 -0
  108. data/lib/language/parser/absent/present.rb +8 -0
  109. data/lib/language/parser/absent.rb +6 -0
  110. data/lib/language/parser/end_of_input.rb +6 -0
  111. data/lib/language/parser/interuption.rb +38 -0
  112. data/lib/language/parser/not_end_of_input.rb +6 -0
  113. data/lib/language/parser/str/not_found.rb +16 -0
  114. data/lib/language/parser/str.rb +6 -0
  115. data/lib/language/parser.rb +53 -0
  116. data/lib/language-ruby.rb +10 -0
  117. data/lib/language.rb +80 -0
  118. data/lib/template/node/code_part.rb +1 -1
  119. data/lib/template/node/part.rb +1 -1
  120. data/lib/template/node/template.rb +1 -1
  121. data/lib/template/node/text_part.rb +1 -1
  122. data/lib/template/node.rb +1 -1
  123. data/lib/template/parser/template.rb +26 -17
  124. data/lib/template/parser.rb +15 -0
  125. data/lib/template/version.rb +1 -1
  126. data/lib/template-ruby.rb +2 -11
  127. data/lib/template.rb +6 -11
  128. data/spec/code/addition_spec.rb +13 -0
  129. data/spec/code/and_operator_spec.rb +13 -0
  130. data/spec/code/bitwise_and_spec.rb +13 -0
  131. data/spec/code/bitwise_or_spec.rb +13 -0
  132. data/spec/code/boolean_spec.rb +13 -0
  133. data/spec/code/call_spec.rb +21 -0
  134. data/spec/code/chained_call_spec.rb +16 -0
  135. data/spec/code/dictionnary_spec.rb +17 -0
  136. data/spec/code/equal_spec.rb +26 -0
  137. data/spec/code/equality_spec.rb +13 -0
  138. data/spec/code/function_spec.rb +18 -0
  139. data/spec/code/greater_spec.rb +18 -0
  140. data/spec/code/group_spec.rb +12 -0
  141. data/spec/code/if_modifier_spec.rb +20 -0
  142. data/spec/code/if_spec.rb +25 -0
  143. data/spec/code/list_spec.rb +17 -0
  144. data/spec/code/multiplication_spec.rb +18 -0
  145. data/spec/code/negation_spec.rb +20 -0
  146. data/spec/code/not_keyword_spec.rb +13 -0
  147. data/spec/code/nothing_spec.rb +17 -0
  148. data/spec/code/number_spec.rb +22 -0
  149. data/spec/code/or_keyword_spec.rb +17 -0
  150. data/spec/code/or_operator_spec.rb +16 -0
  151. data/spec/code/parser/boolean_spec.rb +5 -7
  152. data/spec/code/parser/call_spec.rb +16 -56
  153. data/spec/code/parser/chained_call.rb +17 -0
  154. data/spec/code/parser/dictionnary_spec.rb +8 -9
  155. data/spec/code/parser/function_spec.rb +5 -21
  156. data/spec/code/parser/group_spec.rb +18 -0
  157. data/spec/code/parser/list_spec.rb +9 -20
  158. data/spec/code/parser/number_spec.rb +4 -109
  159. data/spec/code/parser/string_spec.rb +9 -17
  160. data/spec/code/parser_spec.rb +23 -0
  161. data/spec/code/power_spec.rb +13 -0
  162. data/spec/code/range_spec.rb +16 -0
  163. data/spec/code/rescue_spec.rb +13 -0
  164. data/spec/code/shift_spec.rb +13 -0
  165. data/spec/code/splat_spec.rb +13 -0
  166. data/spec/code/string_spec.rb +25 -0
  167. data/spec/code/ternary_spec.rb +18 -0
  168. data/spec/code/unary_minus_spec.rb +13 -0
  169. data/spec/code/while_spec.rb +18 -0
  170. data/spec/spec_helper.rb +4 -1
  171. data/template-ruby.gemspec +2 -4
  172. metadata +110 -71
  173. data/lib/code/node/base_10_decimal.rb +0 -32
  174. data/lib/code/node/base_10_integer.rb +0 -32
  175. data/lib/code/node/base_10_number.rb +0 -19
  176. data/lib/code/node/base_16_number.rb +0 -19
  177. data/lib/code/node/base_2_number.rb +0 -19
  178. data/lib/code/node/base_8_number.rb +0 -19
  179. data/lib/code/node/block.rb +0 -17
  180. data/lib/code/node/defined.rb +0 -19
  181. data/lib/code/node/dictionnary_key_value.rb +0 -23
  182. data/lib/code/node/function_argument.rb +0 -45
  183. data/lib/code/node/group.rb +0 -13
  184. data/lib/code/node/keyword_call_argument.rb +0 -30
  185. data/lib/code/node/keyword_function_argument.rb +0 -33
  186. data/lib/code/node/name.rb +0 -28
  187. data/lib/code/node/not_keyword.rb +0 -13
  188. data/lib/code/node/or_keyword.rb +0 -34
  189. data/lib/code/node/range.rb +0 -31
  190. data/lib/code/node/regular_call_argument.rb +0 -34
  191. data/lib/code/node/regular_function_argument.rb +0 -36
  192. data/lib/code/node/string_characters.rb +0 -13
  193. data/lib/code/node/string_component.rb +0 -23
  194. data/lib/code/node/string_interpolation.rb +0 -13
  195. data/lib/code/parser/defined.rb +0 -20
  196. data/lib/code/parser/greater_than.rb +0 -33
  197. data/spec/call_spec.rb +0 -22
  198. data/spec/code/error/type_error_spec.rb +0 -63
  199. data/spec/code/parser/name_spec.rb +0 -15
  200. data/spec/code/parser/nothing_spec.rb +0 -19
  201. data/spec/code_spec.rb +0 -182
  202. data/spec/function_spec.rb +0 -26
  203. data/spec/template/parser/template_spec.rb +0 -19
  204. data/spec/template_spec.rb +0 -52
@@ -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
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "string" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [["[1, 2].map(&:to_string)", '["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
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "string" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["''", ""],
9
+ ['""', ""],
10
+ %w[:hello hello],
11
+ %w[:admin? admin?],
12
+ %w[:update! update!],
13
+ ["'Hello Dorian'", "Hello Dorian"],
14
+ ['"Hello Dorian"', "Hello Dorian"],
15
+ ["'Hello \\{name}'", "Hello {name}"],
16
+ ['"Hello \\{name}"', "Hello {name}"],
17
+ ["'Hello {1}'", "Hello 1"],
18
+ ['"Hello {1}"', "Hello 1"]
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,18 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "ternary" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["true ? 1", "1"],
9
+ ["false ? 1", ""],
10
+ ["true ? 1 : 2", "1"],
11
+ ["false ? 1 : 2", "2"]
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,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
+ [%w[-1 -1], %w[--1 1], %w[-1.0 -1.0], %w[--1.0 1.0]].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,18 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "while" do
4
+ let(:timeout) { 0 }
5
+ subject { Code.evaluate(input, timeout: timeout).to_s }
6
+
7
+ [
8
+ ["while false", ""],
9
+ ["a = 0\nwhile a < 10 a += 1 end a", "10"],
10
+ ["until true", ""],
11
+ ["a = 0\nuntil a > 10 a += 1 end a", "11"]
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
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
1
  require_relative "../lib/template-ruby"
2
+ require "json"
2
3
 
3
- RSpec.configure { |c| c.example_status_persistence_file_path = "examples.txt" }
4
+ RSpec.configure do |c|
5
+ c.example_status_persistence_file_path = "tmp/examples.txt"
6
+ end