spec/code_spec.rb in template-ruby-0.3.1 vs spec/code_spec.rb in template-ruby-0.4.0

- old
+ new

@@ -1,10 +1,18 @@ require "spec_helper" RSpec.describe Code do - subject { described_class.evaluate(input).to_s } + let!(:input) { "" } + let!(:context) { "" } + let!(:io) { StringIO.new } + let!(:timeout) { 0.1 } + let!(:ruby) { {} } + subject do + Code.evaluate(input, context, io: io, timeout: timeout, ruby: ruby).to_s + end + [ ["nothing", ""], ["null", ""], ["nil", ""], %w[true true], @@ -106,15 +114,69 @@ ["1.0 + 'a'", "1.0a"], ["1 << 1", "2"], ["1.0 << 1", "2"], ["1 << 1.0", "2"], ["1.0 << 1.0", "2"], + ["eval('1 + 1')", "2"], ].each do |(input, expected)| context input.inspect do let(:input) { input } it "succeeds" do expect(subject).to eq(expected) + end + end + end + + context "with ruby" do + context "with a constant" do + let!(:input) { "a + a" } + let!(:ruby) { { a: 1 } } + + it "can access a" do + expect(subject).to eq("2") + end + end + + context "with a function without arguments" do + let!(:input) { "a + a" } + let!(:ruby) { { a: ->{ "hello" } } } + + it "can call a" do + expect(subject).to eq("hellohello") + end + end + + context "with a function with regular arguments" do + let!(:input) { "add(1, 2)" } + let!(:ruby) { { add: ->(a, b){ a + b } } } + + it "can call add" do + expect(subject).to eq("3") + end + end + + context "with a function with keyword arguments" do + let!(:input) { "add(a: 1, b: 2)" } + let!(:ruby) { { add: ->(a:, b:){ a + b } } } + + it "can call add" do + expect(subject).to eq("3") + end + end + + context "with a complex function" do + let!(:input) { "add(1, 1, 1, 1, c: 1, d: 1, e: 1)" } + let!(:ruby) do + { + add: ->(a, b = 1, *args, c:, d: 2, **kargs){ + a + b + args.sum + c + d + kargs.values.sum + } + } + end + + it "can call add" do + expect(subject).to eq("7") end end end end