template-ruby 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/Gemfile +2 -1
- data/Gemfile.lock +27 -54
- data/TODO +17 -0
- data/bin/code +56 -31
- data/bin/format +3 -0
- data/bin/template +62 -20
- data/bin/test +17 -0
- data/code-ruby.gemspec +1 -3
- data/docs/class.code +9 -0
- data/docs/euler/1.template +1 -5
- data/docs/euler/5.template +0 -1
- data/docs/meetup.code +12 -0
- data/docs/precedence.template +6 -39
- data/docs/rain.code +22 -0
- data/docs/slack.code +17 -0
- data/docs/stripe.code +7 -0
- data/docs/twitter.code +9 -0
- data/language-ruby.gemspec +18 -0
- data/lib/code/node/base_10.rb +29 -0
- data/lib/code/node/base_16.rb +13 -0
- data/lib/code/node/base_2.rb +13 -0
- data/lib/code/node/base_8.rb +13 -0
- data/lib/code/node/boolean.rb +7 -7
- data/lib/code/node/call.rb +32 -38
- data/lib/code/node/call_argument.rb +11 -27
- data/lib/code/node/chained_call.rb +10 -27
- data/lib/code/node/code.rb +4 -6
- data/lib/code/node/decimal.rb +26 -0
- data/lib/code/node/dictionnary.rb +20 -9
- data/lib/code/node/equal.rb +18 -20
- data/lib/code/node/function.rb +10 -7
- data/lib/code/node/function_parameter.rb +31 -0
- data/lib/code/node/if.rb +36 -32
- data/lib/code/node/if_modifier.rb +35 -36
- data/lib/code/node/list.rb +6 -8
- data/lib/code/node/negation.rb +5 -23
- data/lib/code/node/not.rb +15 -0
- data/lib/code/node/nothing.rb +1 -1
- data/lib/code/node/number.rb +14 -12
- data/lib/code/node/operation.rb +21 -16
- data/lib/code/node/power.rb +10 -6
- data/lib/code/node/rescue.rb +4 -3
- data/lib/code/node/splat.rb +15 -0
- data/lib/code/node/statement.rb +48 -70
- data/lib/code/node/string.rb +42 -16
- data/lib/code/node/ternary.rb +7 -9
- data/lib/code/node/unary_minus.rb +5 -12
- data/lib/code/node/while.rb +17 -24
- data/lib/code/node.rb +7 -8
- data/lib/code/object/argument.rb +2 -11
- data/lib/code/object/decimal.rb +45 -30
- data/lib/code/object/dictionnary.rb +6 -5
- data/lib/code/object/function.rb +20 -23
- data/lib/code/object/global.rb +11 -6
- data/lib/code/object/integer.rb +73 -30
- data/lib/code/object/list.rb +40 -34
- data/lib/code/object/range.rb +18 -17
- data/lib/code/object/ruby_function.rb +12 -6
- data/lib/code/object/string.rb +22 -12
- data/lib/code/object.rb +82 -24
- data/lib/code/parser/addition.rb +12 -20
- data/lib/code/parser/and_operator.rb +9 -20
- data/lib/code/parser/bitwise_and.rb +9 -20
- data/lib/code/parser/bitwise_or.rb +12 -20
- data/lib/code/parser/boolean.rb +10 -7
- data/lib/code/parser/call.rb +92 -60
- data/lib/code/parser/chained_call.rb +47 -0
- data/lib/code/parser/class.rb +45 -0
- data/lib/code/parser/code.rb +17 -10
- data/lib/code/parser/dictionnary.rb +56 -30
- data/lib/code/parser/equal.rb +87 -35
- data/lib/code/parser/equality.rb +23 -24
- data/lib/code/parser/equality_lower.rb +9 -0
- data/lib/code/parser/function.rb +67 -42
- data/lib/code/parser/greater.rb +25 -0
- data/lib/code/parser/group.rb +13 -8
- data/lib/code/parser/if.rb +51 -21
- data/lib/code/parser/if_modifier.rb +43 -16
- data/lib/code/parser/list.rb +32 -19
- data/lib/code/parser/multiplication.rb +15 -20
- data/lib/code/parser/name.rb +96 -84
- data/lib/code/parser/negation.rb +20 -9
- data/lib/code/parser/not_keyword.rb +14 -12
- data/lib/code/parser/nothing.rb +13 -8
- data/lib/code/parser/number.rb +124 -68
- data/lib/code/parser/operation.rb +35 -0
- data/lib/code/parser/or_keyword.rb +12 -20
- data/lib/code/parser/or_operator.rb +9 -20
- data/lib/code/parser/power.rb +32 -14
- data/lib/code/parser/range.rb +9 -17
- data/lib/code/parser/rescue.rb +29 -13
- data/lib/code/parser/shift.rb +11 -21
- data/lib/code/parser/splat.rb +31 -0
- data/lib/code/parser/statement.rb +4 -3
- data/lib/code/parser/string.rb +53 -62
- data/lib/code/parser/ternary.rb +36 -15
- data/lib/code/parser/unary_minus.rb +23 -5
- data/lib/code/parser/while.rb +26 -15
- data/lib/code/parser/whitespace.rb +49 -0
- data/lib/code/parser.rb +15 -0
- data/lib/code/ruby.rb +13 -12
- data/lib/code-ruby.rb +2 -11
- data/lib/code.rb +16 -13
- data/lib/language/atom.rb +343 -0
- data/lib/language/output.rb +130 -0
- data/lib/language/parser/absent/present.rb +8 -0
- data/lib/language/parser/absent.rb +6 -0
- data/lib/language/parser/end_of_input.rb +6 -0
- data/lib/language/parser/interuption.rb +38 -0
- data/lib/language/parser/not_end_of_input.rb +6 -0
- data/lib/language/parser/str/not_found.rb +16 -0
- data/lib/language/parser/str.rb +6 -0
- data/lib/language/parser.rb +53 -0
- data/lib/language-ruby.rb +10 -0
- data/lib/language.rb +80 -0
- data/lib/template/node/code_part.rb +1 -1
- data/lib/template/node/part.rb +1 -1
- data/lib/template/node/template.rb +1 -1
- data/lib/template/node/text_part.rb +1 -1
- data/lib/template/node.rb +1 -1
- data/lib/template/parser/template.rb +26 -17
- data/lib/template/parser.rb +15 -0
- data/lib/template/version.rb +1 -1
- data/lib/template-ruby.rb +2 -11
- data/lib/template.rb +6 -11
- data/spec/code/addition_spec.rb +13 -0
- data/spec/code/and_operator_spec.rb +13 -0
- data/spec/code/bitwise_and_spec.rb +13 -0
- data/spec/code/bitwise_or_spec.rb +13 -0
- data/spec/code/boolean_spec.rb +13 -0
- data/spec/code/call_spec.rb +21 -0
- data/spec/code/chained_call_spec.rb +16 -0
- data/spec/code/dictionnary_spec.rb +17 -0
- data/spec/code/equal_spec.rb +26 -0
- data/spec/code/equality_spec.rb +13 -0
- data/spec/code/function_spec.rb +18 -0
- data/spec/code/greater_spec.rb +18 -0
- data/spec/code/group_spec.rb +12 -0
- data/spec/code/if_modifier_spec.rb +20 -0
- data/spec/code/if_spec.rb +25 -0
- data/spec/code/list_spec.rb +17 -0
- data/spec/code/multiplication_spec.rb +18 -0
- data/spec/code/negation_spec.rb +20 -0
- data/spec/code/not_keyword_spec.rb +13 -0
- data/spec/code/nothing_spec.rb +17 -0
- data/spec/code/number_spec.rb +22 -0
- data/spec/code/or_keyword_spec.rb +17 -0
- data/spec/code/or_operator_spec.rb +16 -0
- data/spec/code/parser/boolean_spec.rb +5 -7
- data/spec/code/parser/call_spec.rb +16 -56
- data/spec/code/parser/chained_call.rb +17 -0
- data/spec/code/parser/dictionnary_spec.rb +8 -9
- data/spec/code/parser/function_spec.rb +5 -21
- data/spec/code/parser/group_spec.rb +18 -0
- data/spec/code/parser/list_spec.rb +9 -20
- data/spec/code/parser/number_spec.rb +4 -109
- data/spec/code/parser/string_spec.rb +9 -17
- data/spec/code/parser_spec.rb +23 -0
- data/spec/code/power_spec.rb +13 -0
- data/spec/code/range_spec.rb +16 -0
- data/spec/code/rescue_spec.rb +13 -0
- data/spec/code/shift_spec.rb +13 -0
- data/spec/code/splat_spec.rb +13 -0
- data/spec/code/string_spec.rb +25 -0
- data/spec/code/ternary_spec.rb +18 -0
- data/spec/code/unary_minus_spec.rb +13 -0
- data/spec/code/while_spec.rb +18 -0
- data/spec/spec_helper.rb +4 -1
- data/template-ruby.gemspec +2 -4
- metadata +112 -72
- data/lib/code/node/base_10_decimal.rb +0 -32
- data/lib/code/node/base_10_integer.rb +0 -32
- data/lib/code/node/base_10_number.rb +0 -19
- data/lib/code/node/base_16_number.rb +0 -19
- data/lib/code/node/base_2_number.rb +0 -19
- data/lib/code/node/base_8_number.rb +0 -19
- data/lib/code/node/block.rb +0 -17
- data/lib/code/node/defined.rb +0 -19
- data/lib/code/node/dictionnary_key_value.rb +0 -23
- data/lib/code/node/function_argument.rb +0 -45
- data/lib/code/node/group.rb +0 -13
- data/lib/code/node/keyword_call_argument.rb +0 -30
- data/lib/code/node/keyword_function_argument.rb +0 -33
- data/lib/code/node/name.rb +0 -28
- data/lib/code/node/not_keyword.rb +0 -13
- data/lib/code/node/or_keyword.rb +0 -34
- data/lib/code/node/range.rb +0 -31
- data/lib/code/node/regular_call_argument.rb +0 -34
- data/lib/code/node/regular_function_argument.rb +0 -36
- data/lib/code/node/string_characters.rb +0 -13
- data/lib/code/node/string_component.rb +0 -23
- data/lib/code/node/string_interpolation.rb +0 -13
- data/lib/code/parser/defined.rb +0 -20
- data/lib/code/parser/greater_than.rb +0 -33
- data/spec/call_spec.rb +0 -22
- data/spec/code/error/type_error_spec.rb +0 -63
- data/spec/code/parser/name_spec.rb +0 -15
- data/spec/code/parser/nothing_spec.rb +0 -19
- data/spec/code_spec.rb +0 -182
- data/spec/function_spec.rb +0 -26
- data/spec/template/parser/template_spec.rb +0 -19
- 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 {
|
4
|
+
subject { Code::Parser.parse(input) }
|
5
5
|
|
6
6
|
[
|
7
|
-
["true", { boolean: "true" }],
|
8
|
-
["false", { boolean: "false" }]
|
9
|
-
].each do |
|
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
|
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 {
|
4
|
+
subject { Code::Parser.parse(input) }
|
5
5
|
|
6
6
|
[
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
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 {
|
4
|
+
subject { Code::Parser.parse(input) }
|
5
5
|
|
6
6
|
[
|
7
|
-
|
8
|
-
|
9
|
-
"{
|
7
|
+
"{}",
|
8
|
+
"{ }",
|
9
|
+
"{/* comment */}",
|
10
|
+
"{ a: 1, b: 2, c: 3 }",
|
11
|
+
'{ "first_name": "Dorian" }'
|
10
12
|
].each do |input|
|
11
|
-
context input
|
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 {
|
4
|
+
subject { Code::Parser.parse(input) }
|
5
5
|
|
6
6
|
[
|
7
7
|
"() => {}",
|
8
|
-
|
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
|
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 {
|
4
|
+
subject { Code::Parser.parse(input) }
|
5
5
|
|
6
6
|
[
|
7
|
-
[
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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 {
|
4
|
+
subject { Code::Parser.parse(input) }
|
5
5
|
|
6
|
-
[
|
7
|
-
|
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 {
|
4
|
+
subject { Code::Parser.parse(input) }
|
5
5
|
|
6
6
|
[
|
7
|
-
"'hello'",
|
8
|
-
'"hello"',
|
9
7
|
"''",
|
10
8
|
'""',
|
11
|
-
"'
|
12
|
-
'"
|
13
|
-
"'\\
|
14
|
-
'"\\
|
15
|
-
'"
|
16
|
-
"'
|
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
|
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
|