def test_escape(self): t1, t2 = self._tokenize(r'\[\n') self.assertEqual( (t1.type, t1.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('['))) self.assertEqual( (t2.type, t2.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('\n')))
def test_deadend(self): rx = RegularExpression.concat( RegularExpression.fromClass(LitteralCharacterClass( self.t('<')[0])), RegularExpression.fromClass(LitteralCharacterClass( self.t('=')[0]))) rx.start() rx.feed(self.t('<')[0]) # byte/char self.assertFalse(rx.isDeadEnd())
def test_middle(self): t1, t2, t3 = self._tokenize( r's{symbol}e'.format(symbol=self.symbol[1])) self.assertEqual( (t1.type, t1.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('s'))) self.assertEqual((t2.type, t2.value), self.symbol) self.assertEqual( (t3.type, t3.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('e')))
def test_concat(self): rx = RegularExpression.concat( RegularExpression.fromClass(LitteralCharacterClass( self.t('a')[0])), RegularExpression.fromClass(LitteralCharacterClass( self.t('b')[0])), RegularExpression.fromClass(LitteralCharacterClass( self.t('c')[0]))) self.assertTrue(rx.match(self.t('abc'))) self.assertFalse(rx.match(self.t('ab')))
def test_concat(self): t1, t2, t3 = self._tokenize('abc') self.assertEqual( (t1.type, t1.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('a'))) self.assertEqual( (t2.type, t2.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('b'))) self.assertEqual( (t3.type, t3.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('c')))
def test_closure(self): t1, t2 = self._tokenize('a+') self.assertEqual( (t1.type, t1.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('a'))) self.assertEqual((t2.type, t2.value), (RegexTokenizer.TOK_EXPONENT, ExponentToken(1, None)))
def test_interval(self): t1, t2 = self._tokenize('a{13-15}') self.assertEqual( (t1.type, t1.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('a'))) self.assertEqual((t2.type, t2.value), (RegexTokenizer.TOK_EXPONENT, ExponentToken(13, 15)))
def test_single_value(self): t1, t2 = self._tokenize('a{42}') self.assertEqual( (t1.type, t1.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('a'))) self.assertEqual((t2.type, t2.value), (RegexTokenizer.TOK_EXPONENT, ExponentToken(42, 42)))
def test_any(self): tok1, tok2 = self._tokenize('a.') self.assertEqual( (tok1.type, tok1.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('a'))) self.assertEqual((tok2.type, tok2.value), (RegexTokenizer.TOK_CLASS, AnyCharacterClass()))
def test_exponent_null(self): rx = RegularExpression.exponent( RegularExpression.fromClass(LitteralCharacterClass( self.t('a')[0])), 0, 1) self.assertTrue(rx.match(self.t(''))) self.assertTrue(rx.match(self.t('a'))) self.assertFalse(rx.match(self.t('aa')))
def test_kleene(self): rx = RegularExpression.kleene( RegularExpression.fromClass(LitteralCharacterClass( self.t('a')[0]))) self.assertTrue(rx.match(self.t(''))) self.assertTrue(rx.match(self.t('a'))) self.assertTrue(rx.match(self.t('aa'))) self.assertFalse(rx.match(self.t('ab')))
def test_start(self): t1, t2 = self._tokenize(r'{symbol}s'.format(symbol=self.symbol[1])) self.assertEqual((t1.type, t1.value), self.symbol) self.assertEqual( (t2.type, t2.value), (RegexTokenizer.TOK_CLASS, LitteralCharacterClass('s')))
def test_newline(self): rx = RegularExpression.fromClass( LitteralCharacterClass(self.t('\n')[0])) self.assertTrue(rx.match(self.t('\n')))