spec/compiler_spec.rb in curly-templates-1.0.1 vs spec/compiler_spec.rb in curly-templates-2.0.0.beta1
- old
+ new
@@ -1,8 +1,10 @@
require 'spec_helper'
describe Curly::Compiler do
+ include CompilationSupport
+
let :presenter_class do
Class.new do
def foo
"FOO"
end
@@ -25,28 +27,27 @@
def dirty
nil
end
+ def square?(width:, height:)
+ width.to_i == height.to_i
+ end
+
def false?
false
end
def true?
true
end
- def parameterized(value)
- value
+ def self.component_available?(method)
+ %w[foo high_yield yield_value dirty false? true? hello? square?].include?(method)
end
- def self.method_available?(method)
- [:foo, :parameterized, :high_yield, :yield_value, :dirty,
- :false?, :true?, :hello?].include?(method)
- end
-
- def self.available_methods
+ def self.available_components
public_instance_methods
end
private
@@ -61,34 +62,26 @@
describe ".compile" do
it "compiles Curly templates to Ruby code" do
evaluate("{{foo}}").should == "FOO"
end
- it "passes on an optional reference parameter to the presenter method" do
- evaluate("{{parameterized.foo.bar}}").should == "foo.bar"
- end
-
- it "passes an empty string to methods that take a parameter when none is provided" do
- evaluate("{{parameterized}}").should == ""
- end
-
it "raises ArgumentError if the presenter class is nil" do
expect do
Curly::Compiler.compile("foo", nil)
end.to raise_exception(ArgumentError)
end
it "makes sure only public methods are called on the presenter object" do
- expect { evaluate("{{bar}}") }.to raise_exception(Curly::InvalidReference)
+ expect { evaluate("{{bar}}") }.to raise_exception(Curly::InvalidComponent)
end
- it "includes the invalid reference when failing to compile" do
+ it "includes the invalid component when failing to compile" do
begin
evaluate("{{bar}}")
fail
- rescue Curly::InvalidReference => e
- e.reference.should == :bar
+ rescue Curly::InvalidComponent => e
+ e.component.should == "bar"
end
end
it "propagates yields to the caller" do
evaluate("{{high_yield}}") { "$$$" }.should == "$$$, motherfucker!"
@@ -139,10 +132,14 @@
it "passes an argument to blocks" do
evaluate("{{#hello.world?}}foo{{/hello.world?}}{{#hello.foo?}}bar{{/hello.foo?}}").should == "foo"
end
+ it "passes attributes to blocks" do
+ evaluate("{{#square? width=2 height=2}}yeah!{{/square?}}").should == "yeah!"
+ end
+
it "gives an error on mismatching blocks" do
expect do
evaluate("test{{#false?}}bar{{/true?}}")
end.to raise_exception(Curly::IncorrectEndingError)
end
@@ -172,11 +169,11 @@
it "returns false if a missing method is referenced" do
validate("Hello, {{i_am_missing}}").should == false
end
it "returns false if an unavailable method is referenced" do
- presenter_class.stub(:available_methods) { [:foo] }
+ presenter_class.stub(:available_components) { [:foo] }
validate("Hello, {{inspect}}").should == false
end
it "returns true with a block" do
validate("Hello {{#true?}}world{{/true?}}").should == true
@@ -187,20 +184,7 @@
end
def validate(template)
Curly.valid?(template, presenter_class)
end
- end
-
- def evaluate(template, &block)
- code = Curly::Compiler.compile(template, presenter_class)
- context = double("context", presenter: presenter)
-
- context.instance_eval(<<-RUBY)
- def self.render
- #{code}
- end
- RUBY
-
- context.render(&block)
end
end