Best Python code snippet using lisa_python
test_regex_to_nfa.py
Source:test_regex_to_nfa.py
...9 nfa.add_normal_node('0', '1', 'x')10 nfa.add_normal_node('1', nfa.output_port, 'x')11 new_nfa = repeat_nfa(nfa, 2)12 new_nfa.execute('wxxwxx')13 self.assertEqual(new_nfa.get_matched_str(), 'wxxwxx')14 new_nfa.execute('wxx')15 self.assertEqual(new_nfa.is_matched(), False)16 def test_repeat_or_output_nfa(self):17 nfa = RegexFaConstruction('nfa')18 self.assertRaises(TypeError, lambda: repeat_or_output_nfa(1, 2))19 self.assertRaises(ValueError, lambda: repeat_or_output_nfa(nfa, 2))20 nfa.add_normal_node(nfa.input_port, '0', 'w')21 nfa.add_normal_node('0', '1', 'x')22 nfa.add_normal_node('1', nfa.output_port, 'x')23 new_nfa = repeat_or_output_nfa(nfa, 2)24 new_nfa.execute('wxxwxx')25 self.assertEqual(new_nfa.get_matched_str(), 'wxxwxx')26 new_nfa.execute('')27 self.assertEqual(new_nfa.is_matched(), True)28 def test_nodes_repeat_ge_zero(self):29 nfa = RegexFaConstruction('nfa')30 self.assertRaises(TypeError, lambda: nodes_repeat_ge_zero(1, 2))31 self.assertRaises(ValueError, lambda: nodes_repeat_ge_zero(nfa, 0))32 nfa.add_normal_node(nfa.input_port, nfa.output_port, 'a')33 nfa, inc = nodes_repeat_ge_zero(nfa, 0)34 nfa.execute('')35 self.assertEqual(nfa.is_matched(), True)36 nfa.execute('a')37 self.assertEqual(nfa.is_matched(), True)38 nfa.execute('aaaaaa')39 self.assertEqual(nfa.is_matched(), True)40 def test_nodes_repeat_ge_one(self):41 nfa = RegexFaConstruction('nfa')42 self.assertRaises(TypeError, lambda: nodes_repeat_ge_one(1, 2))43 self.assertRaises(ValueError, lambda: nodes_repeat_ge_one(nfa, 0))44 nfa.add_normal_node(nfa.input_port, nfa.output_port, 'a')45 nfa, inc = nodes_repeat_ge_one(nfa, 0)46 nfa.execute('')47 self.assertEqual(nfa.is_matched(), False)48 nfa.execute('a')49 self.assertEqual(nfa.is_matched(), True)50 nfa.execute('aaaaaa')51 self.assertEqual(nfa.get_matched_str(), 'aaaaaa')52 def test_nodes_repeat_eq(self):53 nfa = RegexFaConstruction('nfa')54 self.assertRaises(TypeError, lambda: nodes_repeat_eq(1, 2))55 self.assertRaises(ValueError, lambda: nodes_repeat_eq(nfa, 3))56 nfa.add_normal_node(nfa.input_port, nfa.output_port, 'a')57 nfa = nodes_repeat_eq(nfa, 3)58 nfa.execute('')59 self.assertEqual(nfa.is_matched(), False)60 nfa.execute('a')61 self.assertEqual(nfa.is_matched(), False)62 nfa.execute('aaa')63 self.assertEqual(nfa.is_matched(), True)64 nfa.execute('aaaa')65 self.assertEqual(nfa.get_matched_str(), 'aaa')66 def test_nodes_repeat_range(self):67 nfa = RegexFaConstruction('nfa')68 self.assertRaises(TypeError, lambda: nodes_repeat_range(1, 0, 2, 5))69 self.assertRaises(ValueError, lambda: nodes_repeat_range(nfa, 0, 1, 3))70 nfa.add_normal_node(nfa.input_port, nfa.output_port, 'a')71 nfa, inc = nodes_repeat_range(nfa, 0, 1, 3)72 nfa.execute('')73 self.assertEqual(nfa.is_matched(), False)74 nfa.execute('a')75 self.assertEqual(nfa.is_matched(), True)76 nfa.execute('aa')77 self.assertEqual(nfa.is_matched(), True)78 nfa.execute('aaa')79 self.assertEqual(nfa.is_matched(), True)80 nfa.execute('aaaa')81 self.assertEqual(nfa.get_matched_str(), 'aaa')82 def test_nodes_prefix(self):83 nfa = RegexFaConstruction('nfa')84 self.assertRaises(TypeError, lambda: nodes_prefix(1, 2))85 self.assertRaises(ValueError, lambda: nodes_prefix(nfa, 0))86 nfa.add_normal_node(nfa.input_port, 'n1', 'a')87 nfa.add_normal_node('n1', nfa.output_port, 'b')88 nfa, inc = nodes_prefix(nfa, 0)89 nfa.execute('ab')90 self.assertEqual(nfa.is_matched(), True)91 nfa.execute('abcd')92 self.assertEqual(nfa.get_matched_str(), 'ab')93 nfa.execute('bb')94 self.assertEqual(nfa.is_matched(), False)95 def test_nodes_postfix(self):96 nfa = RegexFaConstruction('test')97 self.assertRaises(TypeError, lambda: nodes_postfix(1, 2))98 self.assertRaises(ValueError, lambda: nodes_postfix(nfa, 0))99 nfa.add_normal_node(nfa.input_port, 'n1', 'a')100 nfa.add_normal_node('n1', nfa.output_port, 'b')101 nfa, inc = nodes_postfix(nfa, 0)102 nfa.execute('ab')103 self.assertEqual(nfa.is_matched(), True)104 nfa.execute('cdab')105 self.assertEqual(nfa.is_matched(), False)106 def test_regex_to_nfa(self):107 regex = r'^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$'108 self.assertRaises(TypeError, lambda: regex_to_nfa(1))109 nfa = regex_to_nfa(regex)110 nfa.execute('wangxin@hdu.edu.com')111 self.assertEqual(nfa.get_matched_str(), 'wangxin@hdu.edu.com')112if __name__ == '__main__':...
regex.py
Source:regex.py
1from typing import Literal2import re3def get_matched_str(pattern : Literal, string : str) -> str:4 match = re.search(pattern=pattern, string=string)5 if (match == None):6 return None7 return match[0]8def get_pattern_removed(pattern : Literal, string : str) -> str:9 if (string == None):10 return None11 return re.sub(pattern=pattern, repl='', string=string)12def debracket(string : str) -> str:13 return get_pattern_removed(14 pattern=r"{|}",...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!