home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ruby164.zip / rbemx164.zip / ruby / share / doc / rdtool-0.6.10 / test / test-nonterminal-element.rb < prev    next >
Text File  |  2001-05-02  |  5KB  |  180 lines

  1. require 'runit/testcase'
  2. require 'runit/cui/mytestrunner'
  3. require 'runit/topublic'
  4.  
  5. require 'rd/tree'
  6. require 'rd/element'
  7. require 'rd/loose-struct'
  8. require 'rd/rd-struct'
  9.  
  10. include RD
  11.  
  12. class TestElement < RUNIT::TestCase
  13.   include RUNIT::ToPublic
  14.   
  15.   def test_add_child_under_document_struct
  16.     check_add_child(TextBlock.new, StringElement.new(""))
  17.     check_add_child(Headline.new(1), Emphasis.new)
  18.     check_add_child(ItemList.new, ItemListItem.new)
  19.     check_add_child(ItemListItem.new, TextBlock.new)
  20.     check_add_child(Emphasis.new, Code.new)
  21.  
  22.     assert_exception(ArgumentError) do
  23.       Headline.new.add_child_under_document_struct(Reference.
  24.                          new(Reference::RDLabel.new),
  25.                          DocumentStructure::RD)
  26.       ItemListItem.new.add_child_under_document_struct(Headline.new,
  27.                          DocumentStructure::RD)
  28.     end
  29.   end
  30.  
  31.   def check_add_child(p, c)
  32.     assert(p.add_child_under_document_struct(c, DocumentStructure::RD))
  33.   end
  34.  
  35.   def test_add_children_under_document_struct
  36.     p = to_public(TextBlock).new
  37.     c1 = Code.new
  38.     c2 = Var.new
  39.     c3 = Reference.new(Reference::RDLabel.new(""))
  40.     p.add_children_under_document_struct([c1, c2, c3], DocumentStructure::RD)
  41.     assert_equal([c1, c2, c3], p.children)
  42.     [c1, c2, c3].each do |i|
  43.       assert_equal(p, i.parent)
  44.     end
  45.   end
  46.  
  47.   def test_add_child_under_document_struct2
  48.     p = to_public(TextBlock).new
  49.     assert_equal([], p.children)
  50.  
  51.     c1 = Emphasis.new
  52.     p.add_child_under_document_struct(c1, DocumentStructure::RD)
  53.     assert_equal([c1], p.children)
  54.     assert_equal(p, c1.parent)
  55.  
  56.     c2 = StringElement.new("")
  57.     p.add_child_under_document_struct(c2, DocumentStructure::RD)
  58.     assert_equal([c1, c2], p.children)
  59.     assert_equal(p, c2.parent)
  60.  
  61.     p = to_public(DocumentElement).new
  62.     c1 = Headline.new(1)
  63.     p.add_child_under_document_struct(c1, DocumentStructure::RD)
  64.     assert_equal([c1], p.children)
  65.     assert_equal(p, c1.parent)
  66.  
  67.     c2 = ItemList.new
  68.     p.add_child_under_document_struct(c2, DocumentStructure::RD)
  69.     assert_equal([c1, c2], p.children)
  70.     assert_equal(p, c2.parent)
  71.   end
  72.  
  73.   def test_add_children_without_document_struct
  74.     p = to_public(TextBlock).new
  75.     c1 = StringElement.new "string"
  76.     c2 = Emphasis.new
  77.     c3 = Headline.new(1)
  78.  
  79.     assert_equal([], p.children)
  80.     p.add_children_without_document_struct([c1])
  81.     assert_equal([c1], p.children)
  82.     assert_equal(p, c1.parent)
  83.     p.add_children_without_document_struct([c1, c2])
  84.     assert_equal([c1, c1, c2], p.children)
  85.     assert_equal(p, c2.parent)
  86.     p.add_children_without_document_struct([c3])
  87.     assert_equal([c1, c1, c2, c3], p.children)
  88.     assert_equal(p, c2.parent)
  89.   end
  90.  
  91.   def test_push_to_children
  92.     parent = to_public(TextBlock).new
  93.     child1 = StringElement.new "string"
  94.     parent.push_to_children(child1)
  95.     assert_equal([child1], parent.children)
  96.     assert_equal(parent, child1.parent)
  97.   end
  98.  
  99.   def test_children
  100.     assert_equal([], to_public(DocumentElement).new.children)
  101.     assert_equal([], to_public(Headline).new(1).children)
  102.     assert_equal([], to_public(TextBlock).new.children)
  103.     assert_equal([], to_public(List).new.children)
  104.     assert_equal([], to_public(ListItem).new.children)
  105. #    assert_equal([], to_public(DescListItem).new.children)
  106.     assert_equal([], to_public(DescListItem::Term).new.children)
  107.     assert_equal([], to_public(NonterminalInline).new.children)
  108.   end
  109.  
  110.   def test_build
  111.     testcase = self
  112.     tr = Tree.new_with_document_struct(DocumentStructure::RD)
  113.     de = DocumentElement.new
  114.     tr.root = de
  115.     tb = TextBlock.new
  116.     de.add_child(tb)
  117.     res = tb.build do
  118.       testcase.assert_equal(tb, self)
  119.     end
  120.     assert_equal(tb, res)
  121.   end
  122.  
  123.   def test_build_under_document_struct
  124.     testcase = self
  125.     tb = TextBlock.new
  126.     assert_no_exception do 
  127.       tb.build(DocumentStructure::LOOSE) do
  128.     testcase.assert_equal(DocumentStructure::LOOSE,
  129.                   tb.temporary_document_structure)
  130.     new Headline, 1
  131.       end
  132.     end
  133.     assert_equal(nil, tb.temporary_document_structure)
  134.  
  135.     begin
  136.       tb.build(DocumentStructure::LOOSE) do
  137.     raise RuntimeError
  138.       end
  139.     rescue
  140.       assert_equal(nil, tb.temporary_document_structure)
  141.     end
  142.   end
  143.  
  144.   def test_make_child
  145.     tr = Tree.new_with_document_struct(DocumentStructure::RD)
  146.     de = DocumentElement.new
  147.     tb = TextBlock.new
  148.     tr.root = de
  149.     de.add_child(tb)
  150.     
  151.     tb.make_child(StringElement, "string")
  152.     assert_equal("<RD::TextBlock>\n  <RD::StringElement>", tb.inspect)
  153.  
  154.     assert_exception(ArgumentError) do
  155.       tb.make_child(TextBlock)
  156.     end
  157.  
  158.     tr2 = Tree.new_with_document_struct(DocumentStructure::RD)
  159.     de2 = DocumentElement.new
  160.     tr2.root = de2
  161.  
  162.     de2.build do
  163.       new TextBlock do
  164.     new StringElement, "string"
  165.     new Emphasis do
  166.       new StringElement, "emphais"
  167.     end
  168.       end
  169.     end
  170.     assert_equal("<RD::DocumentElement>\n  <RD::TextBlock>\n" +
  171.          "    <RD::StringElement>\n    <RD::Emphasis>\n      " +
  172.          "<RD::StringElement>", de2.inspect)
  173.   end
  174. end
  175.  
  176. if $0 == __FILE__
  177.   suite = TestElement.suite
  178.   RUNIT::CUI::MyTestRunner.run(suite)
  179. end
  180.