require 'test/spec' require 'randy' require 'pp' SYMS = [:one, :two] class RenderTo < Test::Spec::CustomShould def initialize(result, ctx=nil) @result = result @ctx = ctx end def assumptions(object) Randy.render(object, @ctx).should.equal(@result) end end context "Randy" do specify "can render Strings" do "".should render_to("") "foo".should render_to("foo") end specify "escapes XML" do "".should render_to("<foo>") "&hate;".should render_to("&hate;") end specify "doesn't escape sanitized strings" do Randy::Sane.new("").should render_to("") Randy::Sane.new("").should.be.kind_of String end specify "can render Arrays" do ["foo", "bar", "baz"].should render_to("foobarbaz") end specify "can render true/false/nil" do true.should render_to("true") false.should render_to("false") nil.should render_to("nil") # ? end specify "knows what it can render" do Randy.renderable?("foo").should.be true Randy.renderable?(["foo", "bar"]).should.be true Randy.renderable?(true).should.be true Randy.renderable?(false).should.be true Randy.renderable?(nil).should.be true Randy.renderable?(:baz).should.be false Randy.renderable?({'a' => 'hash'}).should.be false end specify "can render Randy::Element" do Randy::Element.new("foo").should render_to('') Randy::Element.new("foo", "a" => "b").should render_to('') Randy::Element.new("foo", {"a" => "b"}, ["c"]).should \ render_to('c') Randy::Element.new("foo", {"a" => '"b"'}, ["&love;"]).should \ render_to('&love;') Randy::Element.new("foo", {}, [Randy::Element.new("quux")]).should render_to('') end specify "can render Randy::Comment" do Randy::Comment.new("foo").should render_to("") :comment["foo"].should render_to("") :comment["foo", "bar"].should render_to("") end specify "can render data" do :foo[:data => "one"].should render_to("1", {"one" => "1"}) :foo[:data => "tree"].should render_to("", {"tree" => :bar[]}) end specify "can render with specified renderers" do :foo[:render => lambda { |tag| "data is: #{tag.attrs[:data]}" }, :data => 1]. should render_to("data is: 1") Randy.add_renderer(:blubb) { |tag, ctx| tag["here!"] } :foo[:render => "blubb"].should render_to("here!") end specify "can powerfully render sequences" do :ul[:render => "sequence", :data => "seq"][ :li[:pattern => :item] ].should render_to("
  • foo
  • bar
  • baz
", {"seq" => %w{foo bar baz}}) :ul[:render => "sequence", :data => %w{foo bar baz}][ :li[:pattern => :item], :br[:pattern => :divider], ].should render_to("
  • foo

  • bar

  • baz
", {"seq" => %w{foo bar baz}}) :ul[:render => "sequence", :data => "seq"][ :li[:pattern => :item, :class => "even"], :li[:pattern => :item, :class => "odd"], :br[:pattern => :divider, :class => "a"], :br[:pattern => :divider, :class => "b"], :br[:pattern => :divider, :class => "c"], ].should render_to(%q{
  • foo

  • bar

  • baz

  • quux
}, {"seq" => %w{foo bar baz quux}}) end specify "can render mappings" do :dl[:render => :mapping, :data => {:name => "foo", :nick => "bar"} ][:dt["Name"], :dd[:slot => :name], :dt["Nick"], :dd[:slot => :nick]].should render_to("
Name
foo
Nick
bar
") end def render_to(*a) RenderTo.new(*a) end end context "Randy::Element" do setup do @foo = Randy::Element.new("foo") @bar = Randy::Element.new("bar", {:one => 1, :two => 2}) @quux = Randy::Element.new("quux", {}, [@foo, @bar]) @pat = :foo[:yin[:pattern => "yin"]["this is yin"], :yang[:pattern => "yang"]["this is yang"], :yang[:pattern => "yang"]["this is yang again", :yong[:pattern => :deeper]] ] @slot = :div[:h1["Infos for ", :span[:slot => :name]], :span[:slot => :name], :span[:slot => :nick]] end specify "have a name" do @foo.name.should.equal :foo @bar.name.should.equal :bar end specify "can have attrs" do @foo.attrs.should.be.empty @quux.attrs.should.be.empty @bar.attrs.should.not.be.empty @bar.attrs[:one].should.equal 1 end specify "can have children" do @foo.children.should.be.empty @bar.children.should.be.empty @quux.children.size.should.equal 2 end specify "can conveniently add new children" do @foo.children.size.should.equal 0 @foo[@bar] @foo.children.size.should.equal 1 @foo[@bar] @foo.children.size.should.equal 2 @foo[@bar, @bar] @foo.children.size.should.equal 4 @foo[].should.equal @foo end specify "can have any kind of renderable children" do lambda { @foo["some string"] }.should.not.raise lambda { @foo[["an", "array"]] }.should.not.raise @foo.children.size.should.equal 2 lambda { @foo[lambda{}] }.should.raise(ArgumentError) end specify "can conveniently add attributes" do @bar.attrs.size.should.equal 2 @bar[:blubb => "yum"] @bar["newattr" => "whoo"] @bar.attrs.size.should.equal 4 @bar.attrs[:newattr].should.equal "whoo" @bar.attrs[:blubb].should.equal "yum" @bar.attrs.should.not.include "newattr" @bar.attrs.should.not.include "blubb" end specify "should be convenient to create" do :foo[].should.be.kind_of Randy::Element :foo[].should.equal @foo :bar["one" => 1, "two" => 2].should.equal @bar :quux[:foo[], @bar].should.equal @quux :xml[""].should.be.kind_of Randy::Sane :xml["", ""].should.be.kind_of Randy::Sane :text["foo"].should.be.kind_of Array end specify "should be deeply copyable" do quux2 = @quux.deepdup quux2.should.not.be @quux quux2.children.should.not.be @quux.children end specify "should provide patterns" do @pat.pattern(:yin).children.first.should.equal "this is yin" @pat.pattern(:yang).children.first.should.equal "this is yang" @pat.pattern(:yang, :all).size.should.equal 2 @pat.pattern(:deeper).name.should.equal :yong end specify "should provide slots" do @slot.fill_slot(:name, "foo").fill_slot(:nick, "bar"). should.equal :div[:h1["Infos for ", :span[:render => :data, :data => "foo"]], :span[:render => :data, :data => "foo"], :span[:render => :data, :data => "bar"]] end specify "can be inspected" do @quux.inspect.should.equal ":quux[:foo[], :bar[:one=>1, :two=>2]]" end specify "can be prettyprinted" do PP.pp(@quux, "").should.equal ":quux[:foo[], :bar[:one => 1, :two => 2]]\n" end end