def test_add_period(self):
        assert not Protected().endswith(('.', '!', '?'))
        assert not textutils.is_terminated(Protected())

        assert Protected().add_period().render_as('latex') == '{}'

        text = Protected("That's all, folks")
        assert text.add_period().render_as('latex') == "{That's all, folks.}"
Esempio n. 2
0
    def test__contains__(self):
        text = Protected('mary ', 'had ', 'a little lamb')
        assert 'mary' in text
        assert 'Mary' not in text
        assert 'had a little' in text

        text = Protected('a', 'b', 'c')
        assert 'abc' in text
Esempio n. 3
0
 def test__len__(self):
     assert len(Protected()) == 0
     assert len(Protected('Never', ' ', 'Knows', ' ',
                          'Best')) == len('Never Knows Best')
     assert len(Protected('Never', ' ', Tag('em', 'Knows', ' '),
                          'Best')) == len('Never Knows Best')
     assert len(
         Protected('Never', ' ', Tag('em', HRef('/', 'Knows'), ' '),
                   'Best')) == len('Never Knows Best')
    def test_startswith(self):
        assert not Protected().startswith('.')
        assert not Protected().startswith(('.', '!'))

        text = Protected('mary ', 'had ', 'a little lamb')
        assert not text.startswith('M')
        assert text.startswith('m')

        text = Protected('a', 'b', 'c')
        assert text.startswith('ab')

        assert Protected('This is good').startswith(('This', 'That'))
        assert not Protected('This is good').startswith(('That', 'Those'))
    def test_endswith(self):
        assert not Protected().endswith('.')
        assert not Protected().endswith(('.', '!'))

        text = Protected('mary ', 'had ', 'a little lamb')
        assert not text.endswith('B')
        assert text.endswith('b')

        text = Protected('a', 'b', 'c')
        assert text.endswith('bc')

        assert Protected('This is good').endswith(('good', 'wonderful'))
        assert not Protected('This is good').endswith(('bad', 'awful'))
Esempio n. 6
0
 def test_join(self):
     assert Protected(' ').join(['a', Protected('b c')]).render_as('latex') == 'a{ b c}'
     assert Protected(nbsp).join(['a', 'b', 'c']).render_as('latex') == 'a{~}b{~}c'
     assert nbsp.join(['a', Protected('b'), 'c']).render_as('latex') == 'a~{b}~c'
     assert String('-').join([Protected('a'), Protected('b'), Protected('c')]).render_as('latex') == '{a}-{b}-{c}'
     result = Protected(' and ').join(['a', 'b', 'c']).render_as('latex')
     assert result == 'a{ and }b{ and }c'
Esempio n. 7
0
 def iter_string_parts(self, level=0, in_math=False):
   while True:
     # there will be no Protected inside Math,
     # since we need to preserve all braces
     if in_math:
       token = self.skip_to([self.DOLLAR])
     else:
       token = self.skip_to([self.LBRACE, self.RBRACE, self.DOLLAR])
     if not token:
       remainder = self.get_remainder()
       if remainder:
         yield String(remainder)
       if level != 0:
         raise PybtexSyntaxError('unbalanced braces', self)
       break
     elif token.pattern is self.DOLLAR:
       if in_math:
         yield String(token.value[:-1])
         if level == 0:
           raise PybtexSyntaxError('unbalanced math', self)
         break
       else:
         yield String(token.value[:-1])
         yield Math(*self.iter_string_parts(level=level + 1, in_math=True))
     elif token.pattern is self.LBRACE:
       yield String(token.value[:-1])
       yield Protected(*self.iter_string_parts(level=level + 1))
     else:  # brace.pattern is self.RBRACE
       yield String(token.value[:-1])
       if level == 0:
         raise PybtexSyntaxError('unbalanced braces', self)
       break
Esempio n. 8
0
    def test__init__(self):
        assert unicode(Protected('a', '', 'c')) == 'ac'
        assert unicode(Protected('a', Text(), 'c')) == 'ac'

        text = Protected(Protected(),
                         Protected('mary ', 'had ', 'a little lamb'))
        assert text == Protected(Protected('mary had a little lamb'))
        assert unicode(text) == 'mary had a little lamb'

        text = unicode(
            Protected('a', Protected('b', 'c'), Tag('em', 'x'), Symbol('nbsp'),
                      'd'))
        assert text == 'abcx<nbsp>d'

        assert_raises(ValueError, Protected, {})
        assert_raises(ValueError, Protected, 0, 0)
Esempio n. 9
0
    def test_add_period(self):
        assert not Protected().endswith(('.', '!', '?'))
        assert not textutils.is_terminated(Protected())

        assert Protected().add_period().render_as('latex') == '{}'

        text = Protected("That's all, folks")
        assert text.add_period().render_as('latex') == "{That's all, folks.}"
Esempio n. 10
0
 def iter_string_parts(self, level=0):
     while True:
         token = self.skip_to([self.LBRACE, self.RBRACE])
         if not token:
             remainder = self.get_remainder()
             if remainder:
                 yield String(remainder)
             if level != 0:
                 raise PybtexSyntaxError('unbalanced braces', self)
             break
         elif token.pattern is self.LBRACE:
             yield String(token.value[:-1])
             yield Protected(*self.iter_string_parts(level=level + 1))
         else:  # brace.pattern is self.RBRACE
             yield String(token.value[:-1])
             if level == 0:
                 raise PybtexSyntaxError('unbalanced braces', self)
             break
Esempio n. 11
0
def process_latex(text):
  parts = text.parts
  for i,part in enumerate(parts):
    if isinstance(part, String):
      # Format \emph as Tag('em')
      if (part.endswith(r'\emph ') and isinstance(parts[i+1], Protected)):
        parts[i] = parts[i][:-6]
        parts[i+1] = Tag('em', *parts[i+1].parts)
      # Handle \textsuperscript
      elif (part.endswith(r'\textsuperscript ') and isinstance(parts[i+1], Protected)):
        parts[i] = parts[i][:-17]
        if (str(parts[i+1]) == '2'):
          parts[i+1] = '²'
    elif isinstance(part, Protected):
      parts[i] = process_latex(part)
  if (isinstance(text, Text)):
    return Text(*parts)
  elif (isinstance(text, Protected)):
    return Protected(*parts)
Esempio n. 12
0
    def test__init__(self):
        assert six.text_type(Protected('a', '', 'c')) == 'ac'
        assert six.text_type(Protected('a', Text(), 'c')) == 'ac'

        text = Protected(Protected(), Protected('mary ', 'had ', 'a little lamb'))
        assert text == Protected(Protected('mary had a little lamb'))
        assert six.text_type(text) == 'mary had a little lamb'

        text = six.text_type(Protected('a', Protected('b', 'c'), Tag('em', 'x'), Symbol('nbsp'), 'd'))
        assert text == 'abcx<nbsp>d'

        with pytest.raises(ValueError):
            Protected({})

        with pytest.raises(ValueError):
            Protected(0, 0)
Esempio n. 13
0
 def test__str__(self):
     assert six.text_type(Protected()) == ''
     assert six.text_type(Protected(u'Чудаки украшают мир')) == u'Чудаки украшают мир'
Esempio n. 14
0
 def test_render_as(self):
     string = Protected('a < b')
     assert string.render_as('latex') == '{a < b}'
     assert string.render_as('html') == '<span class="bibtex-protected">a &lt; b</span>'
Esempio n. 15
0
 def test__unicode__(self):
     assert unicode(Protected()) == ''
     assert unicode(
         Protected(u'Чудаки украшают мир')) == u'Чудаки украшают мир'
Esempio n. 16
0
 def test_split(self):
     assert Protected().split() == [Protected()]
     assert Protected().split('abc') == [Protected()]
     assert Protected('a').split() == [Protected('a')]
     assert Protected('a ').split() == [Protected('a ')]
     assert Protected('   a   ').split() == [Protected('   a   ')]
     assert Protected('a + b').split() == [Protected('a + b')]
     assert Protected('a + b').split(' + ') == [Protected('a + b')]
     assert Protected('abc').split('xyz') == [Protected('abc')]
     assert Protected('---').split('--') == [Protected('---')]
     assert Protected('---').split('-') == [Protected('---')]
Esempio n. 17
0
 def test_render_as(self):
     string = Protected('a < b')
     assert string.render_as('latex') == '{a < b}'
     assert string.render_as('html') == 'a &lt; b'
Esempio n. 18
0
    def test__eq__(self):
        assert Protected() == Protected()
        assert not (Protected() != Protected())

        assert Protected('Cat') == Protected('Cat')
        assert not (Protected('Cat') != Protected('Cat'))
        assert Protected('Cat', ' tail') == Protected('Cat tail')
        assert not (Protected('Cat', ' tail') != Protected('Cat tail'))

        assert Protected('Cat') != Protected('Dog')
        assert not (Protected('Cat') == Protected('Dog'))
 def test_capitalize(self):
     text = Protected('mary ', 'had ', 'a little lamb')
     assert unicode(text.capitalize()) == 'mary had a little lamb'
 def test_upper(self):
     text = Protected('Mary ', 'had ', 'a little lamb')
     assert unicode(text.upper()) == 'Mary had a little lamb'
     text = Protected('mary ', 'had ', 'a little lamb')
     assert unicode(text.upper()) == 'mary had a little lamb'
Esempio n. 21
0
 def test_capfirst(self):
     text = Protected('mary ', 'had ', 'a Little Lamb')
     assert unicode(text.capitalize()) == 'mary had a Little Lamb'
Esempio n. 22
0
 def test_append(self):
     text = Protected('Chuck Norris')
     assert (text + ' wins!').render_as('latex') == '{Chuck Norris} wins!'
     assert text.append(' wins!').render_as('latex') == '{Chuck Norris wins!}'
Esempio n. 23
0
    def test__getitem__(self):
        t = Protected('1234567890')

        with pytest.raises(TypeError):
            1 in t

        assert t == Protected('1234567890')
        assert t[:0] == Protected('')
        assert t[:1] == Protected('1')
        assert t[:3] == Protected('123')
        assert t[:5] == Protected('12345')
        assert t[:7] == Protected('1234567')
        assert t[:10] == Protected('1234567890')
        assert t[:100] == Protected('1234567890')
        assert t[:-100] == Protected('')
        assert t[:-10] == Protected('')
        assert t[:-9] == Protected('1')
        assert t[:-7] == Protected('123')
        assert t[:-5] == Protected('12345')
        assert t[:-3] == Protected('1234567')
        assert t[-100:] == Protected('1234567890')
        assert t[-10:] == Protected('1234567890')
        assert t[-9:] == Protected('234567890')
        assert t[-7:] == Protected('4567890')
        assert t[-5:] == Protected('67890')
        assert t[-3:] == Protected('890')
        assert t[1:] == Protected('234567890')
        assert t[3:] == Protected('4567890')
        assert t[5:] == Protected('67890')
        assert t[7:] == Protected('890')
        assert t[10:] == Protected('')
        assert t[100:] == Protected('')
        assert t[0:10] == Protected('1234567890')
        assert t[0:100] == Protected('1234567890')
        assert t[2:3] == Protected('3')
        assert t[2:4] == Protected('34')
        assert t[3:7] == Protected('4567')
        assert t[4:7] == Protected('567')
        assert t[4:7] == Protected('567')
        assert t[7:9] == Protected('89')
        assert t[100:200] == Protected('')

        t = Protected('123', Protected('456', Protected('789')), '0')
        assert t[:3] == Protected('123')
        assert t[:5] == Protected('123', Protected('45'))
        assert t[:7] == Protected('123', Protected('456', Protected('7')))
        assert t[:10] == Protected('123', Protected('456', Protected('789')), '0')
        assert t[:100] == Protected('123', Protected('456', Protected('789')), '0')
        assert t[:-7] == Protected('123')
        assert t[:-5] == Protected('123', Protected('45'))
        assert t[:-3] == Protected('123', Protected('456', Protected('7')))
Esempio n. 24
0
 def test__add__(self):
     t = Protected('a')
     assert t + 'b' == Text(Protected('a'), 'b')
     assert t + t == Text(Protected('aa'))
Esempio n. 25
0
 def test_capitalize(self):
     text = Protected('mary ', 'had ', 'a little lamb')
     assert six.text_type(text.capitalize()) == 'mary had a little lamb'
 def test_append(self):
     text = Protected('Chuck Norris')
     assert (text + ' wins!').render_as('latex') == '{Chuck Norris} wins!'
     assert text.append(' wins!').render_as('latex') == '{Chuck Norris wins!}'
Esempio n. 27
0
    def test_startswith(self):
        assert not Protected().startswith('.')
        assert not Protected().startswith(('.', '!'))

        text = Protected('mary ', 'had ', 'a little lamb')
        assert not text.startswith('M')
        assert text.startswith('m')

        text = Protected('a', 'b', 'c')
        assert text.startswith('ab')

        assert Protected('This is good').startswith(('This', 'That'))
        assert not Protected('This is good').startswith(('That', 'Those'))
 def test_lower(self):
     text = Protected('Mary ', 'had ', 'a little lamb')
     assert unicode(text.lower()) == 'Mary had a little lamb'
     text = Protected('MARY ', 'HAD ', 'A LITTLE LAMB')
     assert unicode(text.lower()) == 'MARY HAD A LITTLE LAMB'
Esempio n. 29
0
    def test_endswith(self):
        assert not Protected().endswith('.')
        assert not Protected().endswith(('.', '!'))

        text = Protected('mary ', 'had ', 'a little lamb')
        assert not text.endswith('B')
        assert text.endswith('b')

        text = Protected('a', 'b', 'c')
        assert text.endswith('bc')

        assert Protected('This is good').endswith(('good', 'wonderful'))
        assert not Protected('This is good').endswith(('bad', 'awful'))
Esempio n. 30
0
 def test_upper(self):
     text = Protected('Mary ', 'had ', 'a little lamb')
     assert six.text_type(text.upper()) == 'Mary had a little lamb'
     text = Protected('mary ', 'had ', 'a little lamb')
     assert six.text_type(text.upper()) == 'mary had a little lamb'
Esempio n. 31
0
 def test_isalpha(self):
     assert not Protected().isalpha()
     assert not Protected('a b c').isalpha()
     assert Protected('abc').isalpha()
     assert Protected(u'文字').isalpha()
 def test_render_as(self):
     string = Protected('a < b')
     assert string.render_as('latex') == '{a < b}'
     assert string.render_as('html') == 'a &lt; b'
Esempio n. 33
0
 def test_lower(self):
     text = Protected('Mary ', 'had ', 'a little lamb')
     assert six.text_type(text.lower()) == 'Mary had a little lamb'
     text = Protected('MARY ', 'HAD ', 'A LITTLE LAMB')
     assert six.text_type(text.lower()) == 'MARY HAD A LITTLE LAMB'