Browse files

Add Scope#subjects

  • Loading branch information...
1 parent 37dfc72 commit fb08b7baa0f29b8fd6bec470d419cf9f53f3044f @blambeau committed Jun 13, 2012
View
12 lib/wlang/scope.rb
@@ -56,8 +56,20 @@ def evaluate(expr, *default)
end
end
+ def subjects
+ arr = []
+ visit(:top_down){|s| arr << s.subject}
+ arr
+ end
+
protected
+ def visit(mode = :top_down, &visitor)
+ visitor.call(self) unless mode == :top_down
+ parent.visit(mode, &visitor) if parent
+ visitor.call(self) if mode == :top_down
+ end
+
def append(x)
x.prepend(self)
end
View
4 lib/wlang/scope/null_scope.rb
@@ -10,6 +10,10 @@ def fetch(key)
block_given? ? yield : throw(:fail)
end
+ def subjects
+ []
+ end
+
def inspect
"NullScope"
end
View
8 spec/assumptions/test_core.rb
@@ -0,0 +1,8 @@
+require 'spec_helper'
+describe "Core assumptions" do
+
+ specify 'Hash does not respond to #<<' do
+ {}.should_not respond_to(:<<)
+ end
+
+end
View
4 spec/unit/scope/test_null_scope.rb
@@ -26,6 +26,10 @@ class Scope
scope.pop.should be_nil
end
+ it 'returns an empty array of subjects' do
+ scope.subjects.should eq([])
+ end
+
end # describe NullScope
end # class Scope
end # module WLang
View
9 spec/unit/scope/test_push.rb
@@ -14,6 +14,7 @@ module WLang
x_scope.parent.should be_nil
x_scope.subject.should eq(:x)
x_scope.root.should eq(x_scope)
+ x_scope.subjects.should eq([:x])
end
context 'when pushing a simple value' do
@@ -30,6 +31,10 @@ module WLang
it 'returns the correct root scope' do
subject.root.should eq(x_scope)
end
+
+ it 'has the correct subjects' do
+ subject.subjects.should eq([:x, :y])
+ end
end
context 'when pushing another scope' do
@@ -55,6 +60,10 @@ module WLang
pushed.parent.parent.should be_nil
pushed.root.should eq(pushed.parent)
end
+
+ it 'has the correct subjects' do
+ subject.subjects.should eq([:x, :y, :z])
+ end
end
end
View
90 spec/unit/template/test_call_args_conventions.rb
@@ -0,0 +1,90 @@
+require 'spec_helper'
+module WLang
+ describe Template, 'call_args_conventions' do
+
+ let(:template){ Template.new("Hello ${who}!") }
+
+ subject{ template.send(:call_args_conventions, args.dup) }
+
+ let(:scope) { subject.first }
+ let(:buffer){ subject.last }
+
+ context 'without any argument' do
+ let(:args){ [] }
+ it 'sets a default NullScope' do
+ scope.should eq(Scope.null)
+ end
+ it 'uses a string buffer' do
+ buffer.should eq('')
+ end
+ end
+
+ context 'with only a String buffer' do
+ let(:args){ [ '' ] }
+ it 'sets a default NullScope' do
+ scope.should eq(Scope.null)
+ end
+ it 'uses the provided buffer' do
+ buffer.should eq(args.first)
+ buffer.object_id.should eq(args.first.object_id)
+ end
+ end
+
+ context 'with only a StringIO buffer' do
+ let(:args){ [ StringIO.new ] }
+ it 'sets a default NullScope' do
+ scope.should eq(Scope.null)
+ end
+ it 'uses the provided buffer' do
+ buffer.should eq(args.first)
+ buffer.object_id.should eq(args.first.object_id)
+ end
+ end
+
+ context 'with only a Hash' do
+ let(:args){ [ {} ] }
+ it 'sets a default ObjectScope' do
+ scope.should be_a(Scope::ObjectScope)
+ scope.subject.should eq(args.first)
+ end
+ it 'uses a string buffer' do
+ buffer.should eq('')
+ end
+ end
+
+ context 'with multiple scoping arguments' do
+ let(:args){ [ 12, {} ] }
+ it 'builds a chain of scoping arguments' do
+ scope.should be_a(Scope::ObjectScope)
+ scope.subject.should eq(args.last)
+ scope.parent.subject.should eq(args.first)
+ scope.root.subject.should eq(args.first)
+ end
+ it 'uses a string buffer' do
+ buffer.should eq('')
+ end
+ end
+
+ context 'with both a Hash and a String' do
+ let(:args){ [ {}, '' ] }
+ it 'sets a default ObjectScope' do
+ scope.should be_a(Scope::ObjectScope)
+ scope.subject.should eq(args.first)
+ end
+ it 'uses a string buffer' do
+ buffer.should eq(args.last)
+ buffer.object_id.should eq(args.last.object_id)
+ end
+ end
+
+ context 'when the template has locals and no arguments' do
+ let(:template){ Template.new(hello_with_data_path) }
+ let(:args){ [] }
+ it 'uses template locals in Scope' do
+ scope.should be_a(Scope::ObjectScope)
+ scope.subject.should eq("who" => "world")
+ end
+ end
+
+ end
+end

0 comments on commit fb08b7b

Please sign in to comment.