def test__eq__(self): assert String() != '' assert String('') != '' assert String() == String() assert String('') == String() assert String('', '', '') == String() assert String('Wa', '', 'ke', ' ', 'up!') == String('Wake up!')
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
def test_capfirst(self): assert unicode(String('').capitalize()) == '' assert unicode( String('november december').capitalize()) == 'November december' assert unicode( String('November December').capitalize()) == 'November december' assert unicode( String('NOVEMBER DECEMBER').capitalize()) == 'November december'
def test__getitem__(self): symbol = Symbol('nbsp') assert symbol[0] == Symbol('nbsp') assert symbol[0:] == Symbol('nbsp') assert symbol[0:5] == Symbol('nbsp') assert symbol[1:] == String() assert symbol[1:5] == String() assert_raises(IndexError, lambda: symbol[1])
def test__getitem__(self): symbol = Symbol('nbsp') assert symbol[0] == Symbol('nbsp') assert symbol[0:] == Symbol('nbsp') assert symbol[0:5] == Symbol('nbsp') assert symbol[1:] == String() assert symbol[1:5] == String() with pytest.raises(IndexError): symbol[1]
def test_capitalize(self): assert unicode(String('').capitalize()) == '' assert unicode(String('').add_period()) == '' assert unicode(String('').add_period('!')) == '' assert unicode(String('').add_period().add_period()) == '' assert unicode(String('').add_period().add_period('!')) == '' assert unicode(String('').add_period('!').add_period()) == '' assert unicode(String('november').capitalize()) == 'November' assert unicode(String('November').capitalize()) == 'November' assert unicode(String('November').add_period()) == 'November.'
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'
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
def test_join(self): text = Text('From ', Tag('em', 'the very beginning'), ' of things') dashified = String('-').join(text.split()) assert dashified == Text('From-', Tag('em', 'the'), '-', Tag('em', 'very'), '-', Tag('em', 'beginning'), '-of-things') dashified = Tag('em', '-').join(text.split()) assert dashified == Text('From', Tag('em', '-the-very-beginning-'), 'of', Tag('em', '-'), 'things')
def test_join(self): assert six.text_type(Text(' ').join(['a', Text('b c')])) == 'a b c' assert six.text_type(Text(nbsp).join(['a', 'b', 'c'])) == 'a<nbsp>b<nbsp>c' assert six.text_type(nbsp.join(['a', 'b', 'c'])) == 'a<nbsp>b<nbsp>c' assert six.text_type(String('-').join(['a', 'b', 'c'])) == 'a-b-c' result = Tag('em', ' and ').join(['a', 'b', 'c']).render_as('html') assert result == 'a<em> and </em>b<em> and </em>c' result = HRef('/', ' and ').join(['a', 'b', 'c']).render_as('html') assert result == 'a<a href="/"> and </a>b<a href="/"> and </a>c'
def test_add_period(self): assert unicode(String('').add_period()) == '' assert unicode(String('').add_period('!')) == '' assert unicode(String('').add_period().add_period()) == '' assert unicode(String('').add_period().add_period('!')) == '' assert unicode(String('').add_period('!').add_period()) == '' unicode(String('November').add_period()) == 'November.' result = unicode(String('November').add_period().add_period()) assert result == 'November.'
def test_add_period(self): assert six.text_type(String('').add_period()) == '' assert six.text_type(String('').add_period('!')) == '' assert six.text_type(String('').add_period().add_period()) == '' assert six.text_type(String('').add_period().add_period('!')) == '' assert six.text_type(String('').add_period('!').add_period()) == '' six.text_type(String('November').add_period()) == 'November.' result = six.text_type(String('November').add_period().add_period()) assert result == 'November.'
def test_startswith(self): assert not String().startswith('n') assert not String('').startswith('n') assert not String().endswith('n') assert not String('').endswith('n') assert not String('November.').startswith('n') assert String('November.').startswith('N')
def test_join(self): assert String().join([]) == Text() assert String('nothing to see here').join([]) == Text() assert String().join(['a', 'b', 'c']) == Text('abc') assert String(', ').join(['tomatoes']) == Text('tomatoes') assert String(', ').join(['tomatoes', 'cucumbers']) == Text('tomatoes, cucumbers') assert String(', ').join(['tomatoes', 'cucumbers', 'lemons']) == Text('tomatoes, cucumbers, lemons')
def test_capitalize(self): assert six.text_type(String('').capfirst()) == '' assert six.text_type(String('november').capfirst()) == 'November' assert six.text_type(String('November').capfirst()) == 'November' assert six.text_type(String('november december').capfirst()) == 'November december' assert six.text_type(String('November December').capfirst()) == 'November December' assert six.text_type(String('NOVEMBER DECEMBER').capfirst()) == 'NOVEMBER DECEMBER'
def test_endswith(self): assert not String().endswith('.') assert not String().endswith(('.', '!')) assert not String('November.').endswith('r') assert String('November.').endswith('.') assert String('November.').endswith(('.', '!')) assert not String('November.').endswith(('?', '!'))
def test_split(self): assert String().split() == [] assert String().split('abc') == [String('')] assert String('a').split() == [String('a')] assert String('a ').split() == [String('a')] assert String('a + b').split() == [String('a'), String('+'), String('b')] assert String('a + b').split(' + ') == [String('a'), String('b')] assert String('a + b').split(re.compile(r'\s')) == [String('a'), String('+'), String('b')]
def test_upper(self): assert String('').upper() == String() assert String('a').upper() == String('A') assert String('November').upper() == String('NOVEMBER')
def test_lower(self): assert String('').lower() == String() assert String('A').lower() == String('a') assert String('November').lower() == String('november')
def test_append(self): assert String().append('') == Text() text = String('The').append(' Adventures of ').append('Tom Sawyer') assert text == Text('The Adventures of Tom Sawyer')
def test_isalpha(self): assert not String().isalpha() assert not String('a b c').isalpha() assert String('abc').isalpha() assert String(u'文字').isalpha()
def test__unicode__(self): val = u'Detektivbyrån' assert unicode(String(val)) == val
def test__add__(self): assert String('Python') + String(' 3') != 'Python 3' assert String('Python') + String(' 3') == Text('Python 3') assert String('A').lower() == String('a') assert six.text_type(String('Python') + String(' ') + String('3')) == 'Python 3' assert six.text_type(String('Python') + Text(' ') + String('3')) == 'Python 3' assert six.text_type(String('Python') + ' ' + '3') == 'Python 3' assert six.text_type(String('Python').append(' 3')) == 'Python 3'
def test__getitem__(self): digits = String('0123456789') assert digits[0] != '0' assert digits[0] == String('0')
def test__contains__(self): assert '' in String() assert 'abc' not in String() assert '' in String(' ') assert ' + ' in String('2 + 2')
def newline(self): """<br> added twice, need nonempty string else it gets eaten before it's written out""" return tag("br")[String(" ")]
def test_render_as(self): string = String(u'Detektivbyrån & friends') assert string.render_as('text') == u'Detektivbyrån & friends' assert string.render_as('html') == u'Detektivbyrån & friends'
def test__len__(self): assert len(String()) == len(String('')) == 0 val = 'test string' assert len(String(val)) == len(val)
def test__init__(self): assert String().value == '' assert String('').value == '' assert String('Wa', '', 'ke', ' ', 'up!').value == 'Wake up!'
def test_render_as(self): string = String(u'Detektivbyrån & friends') assert string.render_as('text') == u'Detektivbyrån & friends' assert string.render_as('html') == u'Detektivbyrån & friends'
def test__str__(self): val = u'Detektivbyrån' assert six.text_type(String(val)) == val