def test_is_block(self): from certbot_nginx._internal.parser_obj import Block self.assertFalse(Block.should_parse([])) self.assertFalse(Block.should_parse([''])) self.assertFalse(Block.should_parse(['two', 'words'])) self.assertFalse(Block.should_parse([[[]], []])) self.assertFalse(Block.should_parse([['block_name'], ['hi', []], []])) self.assertFalse(Block.should_parse([['block_name'], 'lol'])) self.assertTrue(Block.should_parse([['block_name'], ['hi', []]])) self.assertTrue(Block.should_parse([['hello'], []])) self.assertTrue(Block.should_parse([['block_name'], [['many'], ['statements'], 'here']])) self.assertTrue(Block.should_parse([['if', ' ', '(whatever)'], ['hi']]))
def setUp(self): from certbot_nginx._internal.parser_obj import Block self.bloc = Block(None) self.name = ['server', 'name'] self.contents = [['thing', '1'], ['thing', '2'], ['another', 'one']] self.bloc.parse([self.name, self.contents])
class BlockTest(unittest.TestCase): def setUp(self): from certbot_nginx._internal.parser_obj import Block self.bloc = Block(None) self.name = ['server', 'name'] self.contents = [['thing', '1'], ['thing', '2'], ['another', 'one']] self.bloc.parse([self.name, self.contents]) def test_iterate(self): # Iterates itself normally self.assertEqual(self.bloc, next(self.bloc.iterate())) # Iterates contents while expanded expected = [self.bloc.dump()] + self.contents for i, elem in enumerate(self.bloc.iterate(expanded=True)): self.assertEqual(expected[i], elem.dump()) def test_iterate_match(self): # can match on contents while expanded from certbot_nginx._internal.parser_obj import Block, Sentence expected = [['thing', '1'], ['thing', '2']] for i, elem in enumerate( self.bloc.iterate(expanded=True, match=lambda x: isinstance(x, Sentence) and 'thing' in x.words)): self.assertEqual(expected[i], elem.dump()) # can match on self self.assertEqual( self.bloc, next( self.bloc.iterate(expanded=True, match=lambda x: isinstance(x, Block) and 'server' in x.names))) def test_parse_with_added_spaces(self): import copy self.bloc.parse([copy.copy(self.name), self.contents], add_spaces=True) self.assertEqual(self.bloc.dump(), [self.name, self.contents]) self.assertEqual(self.bloc.dump(True), [['server', ' ', 'name', ' '], [['thing', ' ', '1'], ['thing', ' ', '2'], ['another', ' ', 'one']]]) def test_bad_parse_raises_error(self): from certbot import errors self.assertRaises(errors.MisconfigurationError, self.bloc.parse, [[[]], [[]]]) self.assertRaises(errors.MisconfigurationError, self.bloc.parse, ['lol']) self.assertRaises(errors.MisconfigurationError, self.bloc.parse, ['fake', 'news']) def test_set_tabs(self): self.bloc.set_tabs() self.assertEqual(self.bloc.names.dump(True)[0], '\n ') for elem in self.bloc.contents.dump(True)[:-1]: self.assertEqual(elem[0], '\n ') self.assertEqual(self.bloc.contents.dump(True)[-1][0], '\n') def test_get_tabs(self): self.bloc.parse([[' \n \t', 'lol'], []]) self.assertEqual(self.bloc.get_tabs(), ' \t')