Beispiel #1
0
 def test_elements(self):
     expected = [
         String('Title'),
         Document('Section 1', 'para 1', 'para 2'),
         Document('Section 2', 'para 3', 'para 4')
     ]
     self.assertEqual(expected, list(self.doc.elements()))
Beispiel #2
0
    def test_str(self):
        expected = "MyDoc\n\nOne\n\nfoo bar"
        m = RhetRel('Elaboration', 'foo', 'bar')
        one = Document('One', Paragraph(m))
        d = Document('MyDoc', one)
        descr = str(d)
        self.assertEqual(expected, descr)

        expected = "MyDoc\n\nOne\n\nfoo bar\n\nTwo\n\nbaz bar"
        m2 = RhetRel('Contrast', 'baz', 'bar')
        two = Document('Two', Paragraph(m2))
        d = Document('MyDoc', one, two)
        descr = str(d)
        self.assertEqual(expected, descr)
 def test_lexicalise_document(self):
     """ Test lexicalisation of Document. """
     m1 = RhetRel('Leaf', DummyMsg())
     m2 = RhetRel('Elaboration', DummyMsg(), DummyMsg())
     s1 = Document('Section One', m1)
     s2 = Document('Section Two', m2)
     d = Document('Doc Title', s1, s2)
     tmp = lex.document(d)
     expected = 'Doc Title\n\n' + \
         'Section One' + \
         '\n\nBoris is fast.' + \
         '\n\nSection Two' + \
         '\n\nBoris is fast. Boris is fast.'
     actual = realiser(tmp)
     self.assertEqual(expected, str(actual))
Beispiel #4
0
 def document(self, doc, **kwargs):
     """ Perform aggregation on a document - possibly before lexicalisation. """
     self.logger.debug('Aggregating document.')
     if doc is None: return None
     title = self.aggregate(doc.title, **kwargs)
     sections = [self.aggregate(x, **kwargs) for x in doc.sections if x is not None]
     return Document(title, *sections)
Beispiel #5
0
 def document(self, msg, **kwargs):
     """ Return a copy of a Document with strings. """
     self.logger.debug('Realising document.')
     if msg is None:
         return None
     title = self.realise(msg.title, **kwargs)
     if not kwargs.get('keep_title_punctuation') and title.endswith('.'):
         title = title[:-1]
     sections = [self.realise(x, **kwargs) for x in msg.sections]
     return Document(title, *sections)
Beispiel #6
0
    def test_repr(self):
        expected = """\
<Document: (MyDoc)
<Document: (One)
String('foo bar')>>"""
        m = RhetRel('Elaboration', 'foo', 'bar')
        one = Document('One', Paragraph(m))
        d = Document('MyDoc', one)
        self.assertEqual(expected, repr(d))

        expected = """\
<Document: (MyDoc)
<Document: (One)
String('foo bar')>
<Document: (Two)
String('baz bar')>>"""
        m2 = RhetRel('Contrast', 'baz', 'bar')
        two = Document('Two', Paragraph(m2))
        d = Document('MyDoc', one, two)
        self.assertEqual(expected, repr(d))
Beispiel #7
0
 def test_eq(self):
     d2 = Document('Title', Document('Section 1', 'para 1', 'para 2'),
                   Document('Section 2', 'para 3', 'para 4'))
     self.assertEqual(self.doc, d2)
     self.assertEqual(hash(self.doc), hash(d2))
Beispiel #8
0
 def setUp(self):
     self.doc = Document('Title', Document('Section 1', 'para 1', 'para 2'),
                         Document('Section 2', 'para 3', 'para 4'))
Beispiel #9
0
class TestDocument(unittest.TestCase):
    def setUp(self):
        self.doc = Document('Title', Document('Section 1', 'para 1', 'para 2'),
                            Document('Section 2', 'para 3', 'para 4'))

    def test_init(self):
        self.assertTrue(isinstance(self.doc.title, String))
        self.assertTrue(isinstance(self.doc.sections[0].title, String))
        self.assertTrue(isinstance(self.doc.sections[1].title, String))

    def test_eq(self):
        d2 = Document('Title', Document('Section 1', 'para 1', 'para 2'),
                      Document('Section 2', 'para 3', 'para 4'))
        self.assertEqual(self.doc, d2)
        self.assertEqual(hash(self.doc), hash(d2))

    def test_str(self):
        expected = ('Title\n\n'
                    'Section 1\n\npara 1\n\npara 2\n\n'
                    'Section 2\n\npara 3\n\npara 4')
        self.assertEqual(expected, str(self.doc))

    def test_repr(self):
        expected = """<Document: (Title)
<Document: (Section 1)
String('para 1')
String('para 2')>
<Document: (Section 2)
String('para 3')
String('para 4')>>"""
        self.assertEqual(expected, repr(self.doc))

    def test_elements(self):
        expected = [
            String('Title'),
            Document('Section 1', 'para 1', 'para 2'),
            Document('Section 2', 'para 3', 'para 4')
        ]
        self.assertEqual(expected, list(self.doc.elements()))

    def test_to_xml(self):
        expected = '''\
<document>
  <title>
  <child xsi:type="WordElement" canned="true"  cat="ANY">
    <base>Title</base>
  </child>
  </title>
  <sections>
  <document>
    <title>
    <child xsi:type="WordElement" canned="true"  cat="ANY">
      <base>Section+1</base>
    </child>
    </title>
    <sections>
    <child xsi:type="WordElement" canned="true"  cat="ANY">
      <base>para+1</base>
    </child>
    <child xsi:type="WordElement" canned="true"  cat="ANY">
      <base>para+2</base>
    </child>
    </sections>
  </document>
  <document>
    <title>
    <child xsi:type="WordElement" canned="true"  cat="ANY">
      <base>Section+2</base>
    </child>
    </title>
    <sections>
    <child xsi:type="WordElement" canned="true"  cat="ANY">
      <base>para+3</base>
    </child>
    <child xsi:type="WordElement" canned="true"  cat="ANY">
      <base>para+4</base>
    </child>
    </sections>
  </document>
  </sections>
</document>
'''
        self.assertEqual(expected, self.doc.to_xml())