2018-01-14 18:55:16 -05:00
|
|
|
require "spec_helper"
|
|
|
|
require "singleton"
|
2011-12-06 14:46:46 -05:00
|
|
|
|
|
|
|
describe Docile do
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
describe ".dsl_eval" do
|
2013-07-28 18:20:59 -04:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "when DSL context object is an Array" do
|
2013-07-28 18:20:59 -04:00
|
|
|
let(:array) { [] }
|
|
|
|
let!(:result) { execute_dsl_against_array }
|
|
|
|
|
|
|
|
def execute_dsl_against_array
|
|
|
|
Docile.dsl_eval(array) do
|
|
|
|
push 1
|
|
|
|
push 2
|
|
|
|
pop
|
|
|
|
push 3
|
|
|
|
end
|
|
|
|
end
|
2011-12-06 18:41:43 -05:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "executes the block against the DSL context object" do
|
2014-04-16 12:12:15 -04:00
|
|
|
expect(array).to eq([1, 3])
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "returns the DSL object after executing block against it" do
|
2014-04-16 12:12:15 -04:00
|
|
|
expect(result).to eq(array)
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
|
|
|
|
2013-11-26 15:53:45 -05:00
|
|
|
it "doesn't proxy #__id__" do
|
2014-02-04 13:56:49 -05:00
|
|
|
Docile.dsl_eval(array) { expect(__id__).not_to eq(array.__id__) }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2013-07-05 16:33:55 -04:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
it "raises NoMethodError if the DSL object doesn't implement the method" do
|
|
|
|
expect { Docile.dsl_eval(array) { no_such_method } }.to raise_error(NoMethodError)
|
|
|
|
end
|
2013-07-05 16:34:26 -04:00
|
|
|
end
|
|
|
|
|
2011-12-06 18:41:43 -05:00
|
|
|
Pizza = Struct.new(:cheese, :pepperoni, :bacon, :sauce)
|
|
|
|
|
|
|
|
class PizzaBuilder
|
|
|
|
def cheese(v=true); @cheese = v; end
|
|
|
|
def pepperoni(v=true); @pepperoni = v; end
|
|
|
|
def bacon(v=true); @bacon = v; end
|
|
|
|
def sauce(v=nil); @sauce = v; end
|
|
|
|
def build
|
|
|
|
Pizza.new(!!@cheese, !!@pepperoni, !!@bacon, @sauce)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "when DSL context object is a Builder pattern" do
|
2013-07-28 18:20:59 -04:00
|
|
|
let(:builder) { PizzaBuilder.new }
|
|
|
|
let(:result) { execute_dsl_against_builder_and_call_build }
|
|
|
|
|
|
|
|
def execute_dsl_against_builder_and_call_build
|
|
|
|
@sauce = :extra
|
|
|
|
Docile.dsl_eval(builder) do
|
|
|
|
bacon
|
|
|
|
cheese
|
|
|
|
sauce @sauce
|
|
|
|
end.build
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "returns correctly built object" do
|
2014-02-04 13:56:49 -05:00
|
|
|
expect(result).to eq(Pizza.new(true, false, true, :extra))
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 18:41:43 -05:00
|
|
|
end
|
|
|
|
|
2011-12-06 14:46:46 -05:00
|
|
|
class InnerDSL
|
2018-01-14 18:55:16 -05:00
|
|
|
def initialize; @b = "b"; end
|
2011-12-06 14:46:46 -05:00
|
|
|
attr_accessor :b
|
|
|
|
end
|
|
|
|
|
|
|
|
class OuterDSL
|
2018-01-14 18:55:16 -05:00
|
|
|
def initialize; @a = "a"; end
|
2011-12-06 14:46:46 -05:00
|
|
|
attr_accessor :a
|
2013-07-05 16:07:25 -04:00
|
|
|
|
2011-12-06 14:46:46 -05:00
|
|
|
def inner(&block)
|
|
|
|
Docile.dsl_eval(InnerDSL.new, &block)
|
|
|
|
end
|
2013-03-31 23:53:44 -04:00
|
|
|
|
2013-07-05 16:07:25 -04:00
|
|
|
def inner_with_params(param, &block)
|
2013-03-31 23:53:44 -04:00
|
|
|
Docile.dsl_eval(InnerDSL.new, param, :foo, &block)
|
|
|
|
end
|
2011-12-06 14:46:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def outer(&block)
|
|
|
|
Docile.dsl_eval(OuterDSL.new, &block)
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "when given parameters for the DSL block" do
|
2013-07-28 18:20:59 -04:00
|
|
|
def parameterized(*args, &block)
|
|
|
|
Docile.dsl_eval(OuterDSL.new, *args, &block)
|
|
|
|
end
|
2013-03-31 23:53:44 -04:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "passes parameters to the block" do
|
2013-03-31 23:53:44 -04:00
|
|
|
parameterized(1,2,3) do |x,y,z|
|
2014-02-04 13:56:49 -05:00
|
|
|
expect(x).to eq(1)
|
|
|
|
expect(y).to eq(2)
|
|
|
|
expect(z).to eq(3)
|
2013-03-31 23:53:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "finds parameters before methods" do
|
2014-02-04 13:56:49 -05:00
|
|
|
parameterized(1) { |a| expect(a).to eq(1) }
|
2013-03-31 23:53:44 -04:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "find outer dsl parameters in inner dsl scope" do
|
2013-03-31 23:53:44 -04:00
|
|
|
parameterized(1,2,3) do |a,b,c|
|
|
|
|
inner_with_params(c) do |d,e|
|
2014-02-04 13:56:49 -05:00
|
|
|
expect(a).to eq(1)
|
|
|
|
expect(b).to eq(2)
|
|
|
|
expect(c).to eq(3)
|
|
|
|
expect(d).to eq(c)
|
|
|
|
expect(e).to eq(:foo)
|
2013-03-31 23:53:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-02 14:05:36 -05:00
|
|
|
context "when block's context has helper methods which call DSL methods" do
|
|
|
|
class BlockContextWithHelperMethods
|
|
|
|
def initialize(array_as_dsl)
|
|
|
|
@array_as_dsl = array_as_dsl
|
|
|
|
end
|
|
|
|
|
|
|
|
# Classic dynamic programming factorial, using the methods of {Array}
|
|
|
|
# as a DSL to implement it, via helper methods {#calculate_factorials}
|
|
|
|
# and {#save_factorials} which are defined in this class, so therefore
|
|
|
|
# outside the block.
|
|
|
|
def factorial_as_dsl_against_array(n)
|
|
|
|
Docile.dsl_eval(@array_as_dsl) { calculate_factorials(n) }.last
|
|
|
|
end
|
|
|
|
|
|
|
|
# Uses the helper method {#save_factorials} below.
|
|
|
|
def calculate_factorials(n)
|
|
|
|
(2..n).each { |i| save_factorial(i) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Uses the methods {Array#push} and {Array#at} as a DSL from a helper
|
|
|
|
# method defined in the block's context. Successfully calling this
|
|
|
|
# proves that we can find helper methods from outside the block, and
|
|
|
|
# then find DSL methods from inside those helper methods.
|
|
|
|
def save_factorial(i)
|
|
|
|
push(i * at(i - 1))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { context.method(:factorial_as_dsl_against_array) }
|
|
|
|
|
|
|
|
let(:context) { BlockContextWithHelperMethods.new(array_as_dsl) }
|
|
|
|
|
|
|
|
let(:array_as_dsl) { [1, 1] }
|
|
|
|
|
|
|
|
it "finds DSL methods within helper method defined in block's context" do
|
|
|
|
# see https://en.wikipedia.org/wiki/Factorial
|
|
|
|
[
|
|
|
|
[0, 1],
|
|
|
|
[1, 1],
|
|
|
|
[2, 2],
|
|
|
|
[3, 6],
|
|
|
|
[4, 24],
|
|
|
|
[5, 120],
|
|
|
|
[6, 720],
|
|
|
|
[7, 5_040],
|
|
|
|
[8, 40_320],
|
|
|
|
[9, 362_880],
|
|
|
|
[10, 3_628_800],
|
|
|
|
[11, 39_916_800],
|
|
|
|
[12, 479_001_600],
|
|
|
|
[13, 6_227_020_800],
|
|
|
|
[14, 87_178_291_200],
|
|
|
|
[15, 1_307_674_368_000]
|
|
|
|
].each do |n, expected_factorial|
|
|
|
|
array_as_dsl.replace([1, 1])
|
|
|
|
expect(subject.call(n)).to eq expected_factorial
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes fallback instrumentation from the DSL object after block" do
|
|
|
|
expect { subject.call(5) }.
|
|
|
|
not_to change { context.respond_to?(:method_missing) }.
|
|
|
|
from(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes method to remove fallbacl from the DSL object after block" do
|
|
|
|
expect { subject.call(5) }.
|
|
|
|
not_to change { context.respond_to?(:__docile_undo_fallback__) }.
|
|
|
|
from(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when helper methods call methods that are undefined" do
|
|
|
|
let(:array_as_dsl) { "not an array" }
|
|
|
|
|
|
|
|
it "raises NoMethodError" do
|
|
|
|
expect { subject.call(5) }.
|
|
|
|
to raise_error(NoMethodError, /undefined method `at' /)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes fallback instrumentation from the DSL object after block" do
|
|
|
|
expect { subject.call(5) rescue nil }.
|
|
|
|
not_to change { context.respond_to?(:method_missing) }.
|
|
|
|
from(false)
|
|
|
|
end
|
2014-02-04 14:44:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "when DSL have NoMethod error inside" do
|
2018-02-02 14:05:36 -05:00
|
|
|
class DSLWithNoMethod
|
|
|
|
def initialize(b); @b = b; end
|
|
|
|
attr_accessor :b
|
|
|
|
def push_element
|
|
|
|
@b.push 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raise NoMethodError error from nil" do
|
2014-02-04 14:44:38 -05:00
|
|
|
Docile.dsl_eval(DSLWithNoMethod.new(nil)) do
|
2018-02-02 14:05:36 -05:00
|
|
|
expect { push_element }.
|
|
|
|
to raise_error(NoMethodError, /undefined method `push' (for|on) nil:NilClass/)
|
2014-02-04 14:44:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "when DSL blocks are nested" do
|
2011-12-06 14:46:46 -05:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "method lookup" do
|
|
|
|
it "finds method of outer dsl in outer dsl scope" do
|
|
|
|
outer { expect(a).to eq("a") }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 14:46:46 -05:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "finds method of inner dsl in inner dsl scope" do
|
|
|
|
outer { inner { expect(b).to eq("b") } }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 15:05:31 -05:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "finds method of outer dsl in inner dsl scope" do
|
|
|
|
outer { inner { expect(a).to eq("a") } }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 15:05:31 -05:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
it "finds method of block's context in outer dsl scope" do
|
2018-01-14 18:55:16 -05:00
|
|
|
def c; "c"; end
|
|
|
|
outer { expect(c).to eq("c") }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 15:05:31 -05:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
it "finds method of block's context in inner dsl scope" do
|
2018-01-14 18:55:16 -05:00
|
|
|
def c; "c"; end
|
|
|
|
outer { inner { expect(c).to eq("c") } }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 15:05:31 -05:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "finds method of outer dsl in preference to block context" do
|
|
|
|
def a; "not a"; end
|
|
|
|
outer { expect(a).to eq("a") }
|
|
|
|
outer { inner { expect(a).to eq("a") } }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 15:05:31 -05:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "local variable lookup" do
|
|
|
|
it "finds local variable from block context in outer dsl scope" do
|
|
|
|
foo = "foo"
|
|
|
|
outer { expect(foo).to eq("foo") }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 15:05:31 -05:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "finds local variable from block definition in inner dsl scope" do
|
|
|
|
bar = "bar"
|
|
|
|
outer { inner { expect(bar).to eq("bar") } }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 15:34:46 -05:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "instance variable lookup" do
|
|
|
|
it "finds instance variable from block definition in outer dsl scope" do
|
|
|
|
@iv1 = "iv1"; outer { expect(@iv1).to eq("iv1") }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 16:21:11 -05:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
it "proxies instance variable assignments in block in outer dsl scope back into block's context" do
|
2018-01-14 18:55:16 -05:00
|
|
|
@iv1 = "foo"; outer { @iv1 = "bar" }; expect(@iv1).to eq("bar")
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "finds instance variable from block definition in inner dsl scope" do
|
|
|
|
@iv2 = "iv2"; outer { inner { expect(@iv2).to eq("iv2") } }
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 16:21:11 -05:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
it "proxies instance variable assignments in block in inner dsl scope back into block's context" do
|
2018-01-14 18:55:16 -05:00
|
|
|
@iv2 = "foo"; outer { inner { @iv2 = "bar" } }; expect(@iv2).to eq("bar")
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2011-12-06 16:21:11 -05:00
|
|
|
end
|
2013-07-28 18:20:59 -04:00
|
|
|
|
2011-12-06 14:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "when DSL context object is a Dispatch pattern" do
|
2013-07-28 18:20:59 -04:00
|
|
|
class DispatchScope
|
|
|
|
def params
|
|
|
|
{ :a => 1, :b => 2, :c => 3 }
|
|
|
|
end
|
2013-04-01 00:44:23 -04:00
|
|
|
end
|
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
class MessageDispatch
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@responders = {}
|
|
|
|
end
|
2013-04-01 00:44:23 -04:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
def add_responder path, &block
|
|
|
|
@responders[path] = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def dispatch path, request
|
|
|
|
Docile.dsl_eval(DispatchScope.new, request, &@responders[path])
|
|
|
|
end
|
2013-04-01 00:44:23 -04:00
|
|
|
end
|
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
def respond(path, &block)
|
|
|
|
MessageDispatch.instance.add_responder(path, &block)
|
2013-04-01 00:44:23 -04:00
|
|
|
end
|
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
def send_request(path, request)
|
|
|
|
MessageDispatch.instance.dispatch(path, request)
|
2013-04-01 00:44:23 -04:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "dispatches correctly" do
|
2013-07-28 18:20:59 -04:00
|
|
|
@first = @second = nil
|
2013-04-01 00:44:23 -04:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
respond "/path" do |request|
|
2013-07-28 18:20:59 -04:00
|
|
|
@first = request
|
|
|
|
end
|
2013-04-01 00:44:23 -04:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
respond "/new_bike" do |bike|
|
2013-07-28 18:20:59 -04:00
|
|
|
@second = "Got a new #{bike}"
|
|
|
|
end
|
2013-04-01 00:44:23 -04:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
def x(y) ; "Got a #{y}"; end
|
2018-01-14 18:55:16 -05:00
|
|
|
respond "/third" do |third|
|
|
|
|
expect(x(third)).to eq("Got a third thing")
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2013-04-01 00:44:23 -04:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
fourth = nil
|
2018-01-14 18:55:16 -05:00
|
|
|
respond "/params" do |arg|
|
2013-07-28 18:20:59 -04:00
|
|
|
fourth = params[arg]
|
|
|
|
end
|
2013-04-01 00:44:23 -04:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
send_request "/path", 1
|
|
|
|
send_request "/new_bike", "ten speed"
|
|
|
|
send_request "/third", "third thing"
|
|
|
|
send_request "/params", :b
|
2013-04-01 00:54:06 -04:00
|
|
|
|
2014-02-04 13:56:49 -05:00
|
|
|
expect(@first).to eq(1)
|
2018-01-14 18:55:16 -05:00
|
|
|
expect(@second).to eq("Got a new ten speed")
|
2014-02-04 13:56:49 -05:00
|
|
|
expect(fourth).to eq(2)
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2013-04-01 00:44:23 -04:00
|
|
|
|
|
|
|
end
|
2013-07-05 16:07:25 -04:00
|
|
|
|
2018-05-23 22:16:32 -04:00
|
|
|
context "when DSL context object is the same as the block's context object" do
|
|
|
|
class DSLContextSameAsBlockContext
|
|
|
|
def foo(v = nil)
|
|
|
|
@foo = v if v
|
|
|
|
@foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def bar(v = nil)
|
|
|
|
@bar = v if v
|
|
|
|
@bar
|
|
|
|
end
|
|
|
|
|
|
|
|
def dsl_eval(block)
|
|
|
|
Docile.dsl_eval(self, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dsl_eval_string(string)
|
|
|
|
block = binding.eval("proc { #{string} }")
|
|
|
|
dsl_eval(block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:dsl) { DSLContextSameAsBlockContext.new }
|
|
|
|
|
|
|
|
it "calls DSL methods and sets instance variables on the DSL conetxt object" do
|
|
|
|
dsl.dsl_eval_string('foo 0; bar 1')
|
|
|
|
expect(dsl.foo).to eq(0)
|
|
|
|
expect(dsl.bar).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-12-06 15:05:31 -05:00
|
|
|
end
|
2011-12-06 14:46:46 -05:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
describe ".dsl_eval_with_block_return" do
|
2018-01-11 14:27:50 -05:00
|
|
|
let(:array) { [] }
|
|
|
|
let!(:result) { execute_dsl_against_array }
|
|
|
|
|
|
|
|
def execute_dsl_against_array
|
|
|
|
Docile.dsl_eval_with_block_return(array) do
|
|
|
|
push 1
|
|
|
|
push 2
|
|
|
|
pop
|
|
|
|
push 3
|
2018-01-14 18:55:16 -05:00
|
|
|
"Return me!"
|
2018-01-11 14:27:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "executes the block against the DSL context object" do
|
2018-01-11 14:27:50 -05:00
|
|
|
expect(array).to eq([1, 3])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the block's return value" do
|
2018-01-14 18:55:16 -05:00
|
|
|
expect(result).to eq("Return me!")
|
2018-01-11 14:27:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
describe ".dsl_eval_immutable" do
|
2013-07-28 19:21:08 -04:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "when DSL context object is a frozen String" do
|
2013-07-28 23:55:14 -04:00
|
|
|
let(:original) { "I'm immutable!".freeze }
|
2013-07-28 19:21:08 -04:00
|
|
|
let!(:result) { execute_non_mutating_dsl_against_string }
|
|
|
|
|
|
|
|
def execute_non_mutating_dsl_against_string
|
|
|
|
Docile.dsl_eval_immutable(original) do
|
|
|
|
reverse
|
|
|
|
upcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-28 19:22:45 -04:00
|
|
|
it "doesn't modify the original string" do
|
2014-04-16 12:12:15 -04:00
|
|
|
expect(original).to eq("I'm immutable!")
|
2013-07-28 19:21:08 -04:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "chains the commands in the block against the DSL context object" do
|
2014-04-16 12:12:15 -04:00
|
|
|
expect(result).to eq("!ELBATUMMI M'I")
|
2013-07-28 19:21:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
context "when DSL context object is a number" do
|
2013-07-28 23:55:14 -04:00
|
|
|
let(:original) { 84.5 }
|
|
|
|
let!(:result) { execute_non_mutating_dsl_against_number }
|
|
|
|
|
|
|
|
def execute_non_mutating_dsl_against_number
|
|
|
|
Docile.dsl_eval_immutable(original) do
|
|
|
|
fdiv(2)
|
|
|
|
floor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "chains the commands in the block against the DSL context object" do
|
2014-04-16 12:12:15 -04:00
|
|
|
expect(result).to eq(42)
|
2013-07-28 23:55:14 -04:00
|
|
|
end
|
|
|
|
end
|
2013-07-28 19:21:08 -04:00
|
|
|
end
|
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2013-07-25 11:29:34 -04:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
describe Docile::FallbackContextProxy do
|
2013-07-25 11:29:34 -04:00
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
describe "#instance_variables" do
|
2013-07-28 18:20:59 -04:00
|
|
|
subject { create_fcp_and_set_one_instance_variable.instance_variables }
|
|
|
|
let(:expected_type_of_names) { type_of_ivar_names_on_this_ruby }
|
|
|
|
let(:actual_type_of_names) { subject.first.class }
|
|
|
|
let(:excluded) { Docile::FallbackContextProxy::NON_PROXIED_INSTANCE_VARIABLES }
|
|
|
|
|
|
|
|
def create_fcp_and_set_one_instance_variable
|
2013-07-25 11:29:34 -04:00
|
|
|
fcp = Docile::FallbackContextProxy.new(nil, nil)
|
2018-01-14 18:55:16 -05:00
|
|
|
fcp.instance_variable_set(:@foo, "foo")
|
2013-07-28 18:20:59 -04:00
|
|
|
fcp
|
|
|
|
end
|
|
|
|
|
|
|
|
def type_of_ivar_names_on_this_ruby
|
|
|
|
@a = 1
|
|
|
|
instance_variables.first.class
|
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "returns proxied instance variables" do
|
2014-02-04 13:56:49 -05:00
|
|
|
expect(subject.map(&:to_sym)).to include(:@foo)
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2013-07-25 11:29:34 -04:00
|
|
|
|
2013-07-28 18:20:59 -04:00
|
|
|
it "doesn't return non-proxied instance variables" do
|
2014-02-04 13:56:49 -05:00
|
|
|
expect(subject.map(&:to_sym)).not_to include(*excluded)
|
2013-07-25 11:29:34 -04:00
|
|
|
end
|
|
|
|
|
2018-01-14 18:55:16 -05:00
|
|
|
it "preserves the type (String or Symbol) of names on this ruby version" do
|
2014-02-04 13:56:49 -05:00
|
|
|
expect(actual_type_of_names).to eq(expected_type_of_names)
|
2013-07-28 18:20:59 -04:00
|
|
|
end
|
2013-07-25 11:29:34 -04:00
|
|
|
end
|
|
|
|
|
2013-03-31 23:53:44 -04:00
|
|
|
end
|