test/unit/test_boxes.rb in spontaneous-0.1.0.alpha1 vs test/unit/test_boxes.rb in spontaneous-0.2.0.alpha1

- old
+ new

@@ -1,15 +1,23 @@ # encoding: UTF-8 -require 'test_helper' +require File.expand_path('../../test_helper', __FILE__) class BoxesTest < MiniTest::Spec + def setup + @site = setup_site + end + + def teardown + teardown_site + end + context "Box definitions" do setup do - S::Schema.reset! + class ::Piece < Spontaneous::Piece; end class ::MyBoxClass < Box; end class ::MyContentClass < Piece; end class ::MyContentClass2 < MyContentClass; end MyContentClass.field :description @@ -35,10 +43,18 @@ MyContentClass.boxes.length.should == 1 MyContentClass.boxes.first.name.should == :images0 MyContentClass.has_boxes?.should be_true end + should "have a boolean test for emptiness" do + MyContentClass.box :images0 + instance = MyContentClass.new + instance.images0.empty?.should be_true + instance.images0 << MyContentClass.new + instance.images0.empty?.should be_false + end + should "always return a symbol for the name" do MyContentClass.box 'images0' MyContentClass.boxes.first.name.should == :images0 end @@ -109,10 +125,40 @@ instance.images1.class.superclass.should == MyBoxClass instance.images2.class.superclass.should == Box instance.boxes.length.should == 2 end + should "know their ordering in the container" do + MyContentClass.box :box1 + MyContentClass.box :box2 + MyContentClass.box :box3 + MyContentClass.box_order :box3, :box2, :box1 + MyContentClass.boxes.box3.position.should == 0 + MyContentClass.boxes.box2.position.should == 1 + MyContentClass.boxes.box1.position.should == 2 + instance = MyContentClass.new + instance.box3.position.should == 0 + instance.box2.position.should == 1 + instance.box1.position.should == 2 + end + + context "instances" do + should "have a connection to their owner" do + MyContentClass.box :box1 + instance = MyContentClass.new + instance.box1.owner.should == instance + instance.box1.parent.should == instance + end + + should "have a link to their container (which is their owner)" do + MyContentClass.box :box1 + instance = MyContentClass.new + instance.box1.container.should == instance + instance.box1.container.should == instance + end + end + context "ranges" do setup do MyContentClass.box :images1 MyContentClass.box :images2 MyContentClass.box :images3 @@ -185,12 +231,11 @@ should "allow overwriting of class definitions using a block" end context "Box classes" do setup do - Schema.reset! - Spontaneous.template_root = File.expand_path('../../fixtures/templates/boxes', __FILE__) + @site.stubs(:template_root).returns(File.expand_path('../../fixtures/templates/boxes', __FILE__)) class ::Piece < Spontaneous::Piece; end class ::MyContentClass < ::Piece; end class ::MyBoxClass < Box; end MyBoxClass.field :title, :string MyBoxClass.field :description, :string @@ -235,88 +280,21 @@ field :name, :string field :logo, :image field :description, :string end instance = MyContentClass.new - instance.partners.name.must_be_instance_of(Spontaneous::FieldTypes::StringField) + assert instance.partners.name.class < Spontaneous::FieldTypes::StringField instance.partners.name = "Howard" instance.partners.description = "Here is Howard" instance.save instance = Content[instance.id] instance.partners.name.value.should == "Howard" instance.partners.description.value.should == "Here is Howard" end - should "default to template in root with the same name" do - end - - # context "with styles" do - # setup do - # MyBoxClass.field :title, :string - # MyBoxClass.style :christy - # class ::InheritedStyleBox < MyBoxClass; end - # class ::WithTemplateBox < Box; end - # class ::WithoutTemplateBox < Box; end - # class ::BlankContent < Content; end - # @content = MyContentClass.new - # @content.images.title = "whisty" - # end - - # teardown do - # Object.send(:remove_const, :InheritedStyleBox) - # Object.send(:remove_const, :WithTemplateBox) - # Object.send(:remove_const, :WithoutTemplateBox) - # Object.send(:remove_const, :BlankContent) - # end - - # should "render using explicit styles" do - # @content.images.render.should == "christy: whisty\\n" - # end - - # should_eventually "allow defining style in definition" do - # BlankContent.box :images do - # style :inline_style - # end - # instance = BlankContent.new - # instance.images.style.filename.should == "inline_style.html.cut" - # end - - # should "render using default template style" do - # BlankContent.box :images, :class => :WithTemplateBox - # instance = BlankContent.new - # instance.images.render.should == "with_template_box.html.cut\\n" - # end - - # should "render using global default box styles" do - # entry = Object.new - # entry.stubs(:render).returns("<entry>") - # BlankContent.box :images, :class => :WithoutTemplateBox - # instance = BlankContent.new - # instance.images.stubs(:pieces).returns([entry]) - # instance.images.render.should == "<entry>" - # end - - # should "find templates named after box in owning classes template dir" do - # BlankContent.box :things - # instance = BlankContent.new - # instance.things.render.should == "blank_content/things.html.cut\\n" - # end - - # should "not use templates with box name found in root template dir" do - # BlankContent.box :thangs - # instance = BlankContent.new - # instance.thangs.render.should == "" - # end - - # should "inherit styles from their superclass" do - # BlankContent.box :images, :class => :InheritedStyleBox - # instance = BlankContent.new - # instance.images.title = "ytsirhc" - # instance.images.render.should == "christy: ytsirhc\\n" - # end - # end - + # true? + should "default to template in root with the same name" end context "Box content" do setup do class ::Piece < Spontaneous::Piece; end @@ -357,11 +335,13 @@ @parent.save child1.images << child3 child1.save @parent = Content[@parent.id] child1.reload; child2.reload; child3.reload + @parent.images.contents.should == [child1] @parent.images.pieces.should == [child1] + @parent.words.contents.should == [child2] @parent.words.pieces.should == [child2] @parent.pieces.should == [child1, child2] child1.images.pieces.should == [child3] child1.pieces.should == [child3] @@ -381,13 +361,29 @@ styled = Content[styled.id] styled.one.pieces.first.style.name.should == :blank2 styled.two.pieces.first.style.name.should == :blank3 end - should "allow selection of subclasses" do + should "be insertable at any position" do + BlankContent.box :box3 + BlankContent.box :box4 + instance = BlankContent.new + count = 4 + [:images, :words, :box3, :box4].map { |name| instance.boxes[name] }.each do |box| + count.times { |n| box << StyledContent.new(:label => n)} + end + instance.box4.insert(1, StyledContent.new(:label => "a")) + instance.box4.contents.map { |e| e.label }.should == ["0", "a", "1", "2", "3"] + instance.box4.insert(5, StyledContent.new(:label => "b")) + instance.box4.contents.map { |e| e.label }.should == ["0", "a", "1", "2", "3", "b"] + instance.box3.insert(2, StyledContent.new(:label => "c")) + instance.box3.contents.map { |e| e.label }.should == ["0", "1", "c", "2", "3"] end + + should "allow selection of subclasses" end + context "Allowed types" do setup do class ::Allowed1 < Content style :frank style :freddy @@ -421,10 +417,12 @@ end class ::Mixed < Box allow_subclasses :Allowed1 end + + end teardown do [:Parent, :Allowed1, :Allowed11, :Allowed111, :Allowed2, :Allowed3, :Allowed4, :ChildClass, :Allowable, :Mixed].each { |k| Object.send(:remove_const, k) } rescue nil end @@ -475,10 +473,28 @@ a.parents.available_styles(c).map { |s| s.name }.should == [:arthur, :lancelot] end should "inherit allowed types from superclass" do ChildClass.allowed.should == Parent.allowed + Allowable.boxes.parents.allowed_types(nil).should == [Allowed1, Allowed2, Allowed3] + class ::AChild < Allowable + box :parents do + allow :Allowed11 + end + end + class ::AChild2 < AChild + box :parents, :title => "Things" do + allow :Allowed111 + end + end + box = AChild.boxes.parents + box.allowed_types(nil).should == [Allowed1, Allowed2, Allowed3, Allowed11] + box = AChild2.boxes.parents + box.title.should == "Things" + box.allowed_types(nil).should == [Allowed1, Allowed2, Allowed3, Allowed11, Allowed111] + Object.send(:remove_const, :AChild) + Object.send(:remove_const, :AChild2) end should "include a subtype's allowed list as well as the supertype's" do ChildClass.allow :Allowed4 ChildClass.allowed.map {|a| a.instance_class }.should == (Parent.allowed.map {|a| a.instance_class } + [Allowed4]) @@ -489,9 +505,64 @@ instance.parents.allowed_types.should == Parent.allowed_types end should "correctly allow addition of subclasses" do Mixed.allowed_types.should == [Allowed11, Allowed111] + end + end + + context "Box groups" do + setup do + class ::A < S::Piece + box_group :inner do + box :a + box :b + end + box_group :outer do + box :c + box :d + end + end + + class ::B < ::A + box_group :outer do + box :e + end + end + + class ::C < ::B + box :f, :group => :inner + end + + @a = ::A.new + @b = ::B.new + @c = ::C.new + [@a, @b, @c].each do |instance| + instance.boxes[:a].stubs(:render).with(anything).returns("[a]") + instance.boxes[:b].stubs(:render).with(anything).returns("[b]") + instance.boxes[:c].stubs(:render).with(anything).returns("[c]") + instance.boxes[:d].stubs(:render).with(anything).returns("[d]") + end + @b.boxes[:e].stubs(:render).with(anything).returns("[e]") + @c.boxes[:e].stubs(:render).with(anything).returns("[e]") + @c.boxes[:f].stubs(:render).with(anything).returns("[f]") + end + + teardown do + Object.send(:remove_const, :A) + Object.send(:remove_const, :B) + Object.send(:remove_const, :C) + end + + should "successfully allocate boxes" do + @a.boxes.inner.render.should == "[a][b]" + @a.boxes.outer.render.should == "[c][d]" + + @b.boxes.inner.render.should == "[a][b]" + @b.boxes.outer.render.should == "[c][d][e]" + + @c.boxes.inner.render.should == "[a][b][f]" + @c.boxes.outer.render.should == "[c][d][e]" end end end