test/unit/test_alias.rb in spontaneous-0.2.0.alpha2 vs test/unit/test_alias.rb in spontaneous-0.2.0.alpha3

- old
+ new

@@ -16,14 +16,17 @@ end context "Aliases:" do setup do @site = setup_site + @template_root = File.expand_path(File.join(File.dirname(__FILE__), "../fixtures/templates/aliases")) + @site.paths.add(:templates, @template_root) + @renderer = S::Output::Template::Renderer.new(false) + S::Output.renderer = @renderer + Content.delete - self.template_root = File.expand_path(File.join(File.dirname(__FILE__), "../fixtures/templates/aliases")) - class ::Page < Spontaneous::Page field :title box :box1 box :box2 end @@ -170,12 +173,12 @@ alias_of :A, :container => Proc.new { S::Site['#thepage'].box1 } end class ::XX < ::Piece alias_of :AA, :container => Proc.new { S::Site['#thepage'].box1 } end - X.targets.should == @page.box1.select { |p| A === p } - XX.targets.should == @page.box1.select { |p| AA === p } + Set.new(X.targets).should == Set.new(@page.box1.select { |p| A === p }) + Set.new(XX.targets).should == Set.new(@page.box1.select { |p| AA === p }) end should "allow for selecting only content from a range of boxes" do class ::X < ::Piece alias_of :A, :container => Proc.new { [S::Site['#thepage'].box1, S::Site['#thepage'].box2] } @@ -217,14 +220,14 @@ assert_same_content XX.targets, @page.content.select { |p| AA === p } + page2.box2.select { |p| AA === p } end should "allow for selecting content only from the content of the owner of the box" do class ::X < ::Piece - alias_of proc { |owner| owner.box1.pieces } + alias_of proc { |owner| owner.box1.contents } end class ::XX < ::Piece - alias_of proc { |owner, box| box.pieces } + alias_of proc { |owner, box| box.contents } end class ::XXX < ::Piece alias_of :A, :container => proc { |owner, box| box } end assert_same_content X.targets(@page), @page.box1.contents @@ -241,10 +244,40 @@ ::X = Class.new(::Piece) do alias_of :A, :filter => _filter end assert_same_content pieces, X.targets end + + should "allow for filtering instances according to current page content" do + @page.box1 << AAA.create + @page.box2 << AAA.create + @page.save.reload + allowable = AAA.all - @page.box1.contents + ::X = Class.new(::Piece) do + alias_of :AAA, :filter => proc { |choice, page, box| !box.include?(choice) } + end + assert_same_content allowable, X.targets(@page, @page.box1) + end + + should "allow for ensuring the uniqueness of the entries" do + aaa = AAA.all + ::X = Class.new(::Piece) do + alias_of :AAA, :unique => true + end + @page.box1 << aaa.first + @page.save.reload + assert_same_content [aaa.last], X.targets(@page, @page.box1) + end + + should "allow for returning an arbitrary list of results generated by a proc" do + results = [mock, mock, mock] + ::X = Class.new(::Piece) do + alias_of proc { results } + end + ::X.targets.should == results + end + end end context "instances" do setup do @@ -255,10 +288,15 @@ should "have their own fields" do @a_alias.field?(:a_alias_field1).should be_true end + should "provide access to their target" do + @a_alias.target.should == @a + end + + # TODO should "reference the aliases fields before the targets" should "present their target's fields as their own" do @a_alias.field?(:a_field1).should be_true @@ -268,17 +306,17 @@ should "have access to their target's fields" do @a_alias.target.a_field1.value.should == @a.a_field1.value end should "have their own styles" do - assert_correct_template(@a_alias, 'a_alias/a_alias_style') + assert_correct_template(@a_alias, @template_root / 'a_alias/a_alias_style') end should "present their target's styles as their own" do @a_alias.style = :a_style - assert_correct_template(@a_alias, 'a/a_style') + assert_correct_template(@a_alias, @template_root / 'a/a_style') end # should "have an independent style setting" should "not delete their target when deleted" do @a_alias.destroy @@ -295,13 +333,46 @@ should "include alias title & icon in serialisation" do @a_alias.export[:alias_title].should == @a.alias_title @a_alias.export[:alias_icon].should == @a.alias_icon_field.export end + end + end + context "Aliases to custom models" do + setup do + @target_id = target_id = 9999 + @target = target = mock() + @target.stubs(:id).returns(@target_id) + @target.stubs(:title).returns("custom object") + @custom_alias_class = Class.new(::Page) do + alias_of proc { [target] }, :lookup => lambda { |id| + return target if id == target_id + nil + }, :slug => lambda { |target| target.title.to_url } + end + end + should "be creatable using a custom initializer" do + a = @custom_alias_class.for_target(@target_id) + + a.target_id.should == @target_id + a.target.should == @target + end + + should "be able to provide a slug for pages" do + a = @custom_alias_class.for_target(@target_id) + a.target.should == @target + a.slug.should == "custom-object" + end + + should "ignore styles if object doesn't provide them" do + a = @custom_alias_class.for_target(@target_id) + a.style.template.call.should == Page.new.style.template.call + end end + context "Piece aliases" do should "be allowed to target pages" do a = BBAlias.create(:target => @bb) a.bb_field1.value.should == "BB"