1# -*- coding: utf-8 -*- 2""" 3 Tests for inheritance in RegexLexer 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5 6 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. 7 :license: BSD, see LICENSE for details. 8""" 9 10from pygments.lexer import RegexLexer, inherit 11from pygments.token import Text 12 13 14class One(RegexLexer): 15 tokens = { 16 'root': [ 17 ('a', Text), 18 ('b', Text), 19 ], 20 } 21 22 23class Two(One): 24 tokens = { 25 'root': [ 26 ('x', Text), 27 inherit, 28 ('y', Text), 29 ], 30 } 31 32 33class Three(Two): 34 tokens = { 35 'root': [ 36 ('i', Text), 37 inherit, 38 ('j', Text), 39 ], 40 } 41 42 43class Beginning(Two): 44 tokens = { 45 'root': [ 46 inherit, 47 ('m', Text), 48 ], 49 } 50 51 52class End(Two): 53 tokens = { 54 'root': [ 55 ('m', Text), 56 inherit, 57 ], 58 } 59 60 61class Empty(One): 62 tokens = {} 63 64 65class Skipped(Empty): 66 tokens = { 67 'root': [ 68 ('x', Text), 69 inherit, 70 ('y', Text), 71 ], 72 } 73 74 75def test_single_inheritance_position(): 76 t = Two() 77 pats = [x[0].__self__.pattern for x in t._tokens['root']] 78 assert ['x', 'a', 'b', 'y'] == pats 79 80 81def test_multi_inheritance_beginning(): 82 t = Beginning() 83 pats = [x[0].__self__.pattern for x in t._tokens['root']] 84 assert ['x', 'a', 'b', 'y', 'm'] == pats 85 86 87def test_multi_inheritance_end(): 88 t = End() 89 pats = [x[0].__self__.pattern for x in t._tokens['root']] 90 assert ['m', 'x', 'a', 'b', 'y'] == pats 91 92 93def test_multi_inheritance_position(): 94 t = Three() 95 pats = [x[0].__self__.pattern for x in t._tokens['root']] 96 assert ['i', 'x', 'a', 'b', 'y', 'j'] == pats 97 98 99def test_single_inheritance_with_skip(): 100 t = Skipped() 101 pats = [x[0].__self__.pattern for x in t._tokens['root']] 102 assert ['x', 'a', 'b', 'y'] == pats 103