class ConfParserTests(unittest.TestCase): def setUp(self): conf = open('testdata/test.txts.conf') try: self.confparser = ConfParser(conf.readlines()) finally: conf.close() self.expected_styles = [] def tearDown(self): self.confparser = None self.expected_styles = None def expect_regex_style(self, pattern, transforms, apply_to_whole_line=False): self.expected_styles.append(RegexStyle(pattern, transforms, apply_to_whole_line)) def expect_index_style(self, regions, transforms): self.expected_styles.append(IndexStyle(regions, transforms)) def test_example_style(self): styles = self.confparser.get_styles('example') self.expect_regex_style(r'error', ['red'], True) self.expect_regex_style(r'evil\.org', ['red']) self.expect_regex_style(r'\d{4}-\d\d-\d\d', ['green']) self.expect_regex_style(r'\d\d:\d\d:\d\d', ['green', 'bold']) self.expect_regex_style(r'\d+\.\d+\.\d+\.\d+(:\d+)?', ['yellow', 'underline']) self.expect_regex_style(r'\[samplesession\]', ['magenta']) self.expect_regex_style(r'\[[^\]]+\]', ['blue']) self.expect_regex_style(r'\b\d+\b', ['cyan', 'bold']) self.assert_regex_styles(styles) def test_get_first(self): styles = self.confparser.get_styles('first') self.expect_regex_style(r'some error', ['red']) self.expect_regex_style(r'\d\d-\d\d-\d\d\d\d', ['blue']) self.expect_regex_style(r'some pattern', ['green']) self.expect_regex_style(r'\[(xyz.*x+y?z+)\]', ['underline']) self.assert_regex_styles(styles) def test_get_second(self): styles = self.confparser.get_styles('second') self.expect_regex_style('\w+', ['blue']) self.assert_regex_styles(styles) def test_get_third(self): styles = self.confparser.get_styles('third') self.expect_regex_style(r':on-red : \d+', ['on-red']) self.expect_regex_style(r'\\:\\[\s+]foo.*(foo).*bar\\\\', ['grey']) self.expect_regex_style(r': double: quotes', ['yellow']) self.assert_regex_styles(styles) def test_get_fourth(self): styles = self.confparser.get_styles('fourth') assert styles == [] def test_get_fifth(self): self.assert_style_error( 'fifth', 'Invalid style definition: green regex("some pattern")') def test_get_sixth(self): try: styles = self.confparser.get_styles('sixth') self.fail('should fail on invalid style key') except Exception, e: self.assertEqual(e.message, 'Invalid style key: "some-bad-key"')
class ConfParserTests(unittest.TestCase): def setUp(self): conf = open('testdata/test.txts.conf') try: self.confparser = ConfParser(conf.readlines()) finally: conf.close() self.expected_styles = [] def tearDown(self): self.confparser = None self.expected_styles = None def expect_regex_style(self, pattern, transforms, apply_to_whole_line=False): self.expected_styles.append( RegexStyle(pattern, transforms, apply_to_whole_line)) def expect_index_style(self, regions, transforms): self.expected_styles.append(IndexStyle(regions, transforms)) def test_example_style(self): styles = self.confparser.get_styles('example') self.expect_regex_style(r'error', ['red'], True) self.expect_regex_style(r'evil\.org', ['red']) self.expect_regex_style(r'\d{4}-\d\d-\d\d', ['green']) self.expect_regex_style(r'\d\d:\d\d:\d\d', ['green', 'bold']) self.expect_regex_style(r'\d+\.\d+\.\d+\.\d+(:\d+)?', ['yellow', 'underline']) self.expect_regex_style(r'\[samplesession\]', ['magenta']) self.expect_regex_style(r'\[[^\]]+\]', ['blue']) self.expect_regex_style(r'\b\d+\b', ['cyan', 'bold']) self.assert_regex_styles(styles) def test_get_first(self): styles = self.confparser.get_styles('first') self.expect_regex_style(r'some error', ['red']) self.expect_regex_style(r'\d\d-\d\d-\d\d\d\d', ['blue']) self.expect_regex_style(r'some pattern', ['green']) self.expect_regex_style(r'\[(xyz.*x+y?z+)\]', ['underline']) self.assert_regex_styles(styles) def test_get_second(self): styles = self.confparser.get_styles('second') self.expect_regex_style('\w+', ['blue']) self.assert_regex_styles(styles) def test_get_third(self): styles = self.confparser.get_styles('third') self.expect_regex_style(r':on-red : \d+', ['on-red']) self.expect_regex_style(r'\\:\\[\s+]foo.*(foo).*bar\\\\', ['grey']) self.expect_regex_style(r': double: quotes', ['yellow']) self.assert_regex_styles(styles) def test_get_fourth(self): styles = self.confparser.get_styles('fourth') assert styles == [] def test_get_fifth(self): self.assert_style_error( 'fifth', 'Invalid style definition: green regex("some pattern")') def test_get_sixth(self): try: styles = self.confparser.get_styles('sixth') self.fail('should fail on invalid style key') except Exception, e: self.assertEqual(e.message, 'Invalid style key: "some-bad-key"')