1 package chroma
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func makeDelegationTestLexers() (lang Lexer, root Lexer) {
10 return MustNewLexer(nil, Rules{
11 "root": {
12 {`\<\?`, CommentPreproc, Push("inside")},
13 {`.`, Other, nil},
14 },
15 "inside": {
16 {`\?\>`, CommentPreproc, Pop(1)},
17 {`\bwhat\b`, Keyword, nil},
18 {`\s+`, Whitespace, nil},
19 },
20 }),
21 MustNewLexer(nil, Rules{
22 "root": {
23 {`\bhello\b`, Keyword, nil},
24 {`\b(world|there)\b`, Name, nil},
25 {`\s+`, Whitespace, nil},
26 },
27 })
28 }
29
30 func TestDelegate(t *testing.T) {
31 testdata := []struct {
32 name string
33 source string
34 expected []Token
35 }{
36 {"SourceInMiddle", `hello world <? what ?> there`, []Token{
37 {Keyword, "hello"},
38 {TextWhitespace, " "},
39 {Name, "world"},
40 {TextWhitespace, " "},
41
42 {CommentPreproc, "<?"},
43 {Whitespace, " "},
44 {Keyword, "what"},
45 {Whitespace, " "},
46 {CommentPreproc, "?>"},
47
48 {TextWhitespace, " "},
49 {Name, "there"},
50 }},
51 {"SourceBeginning", `<? what ?> hello world there`, []Token{
52 {CommentPreproc, "<?"},
53 {TextWhitespace, " "},
54 {Keyword, "what"},
55 {TextWhitespace, " "},
56 {CommentPreproc, "?>"},
57 {TextWhitespace, " "},
58 {Keyword, "hello"},
59 {TextWhitespace, " "},
60 {Name, "world"},
61 {TextWhitespace, " "},
62 {Name, "there"},
63 }},
64 {"SourceEnd", `hello world <? what there`, []Token{
65 {Keyword, "hello"},
66 {TextWhitespace, " "},
67 {Name, "world"},
68 {TextWhitespace, " "},
69
70 {CommentPreproc, "<?"},
71 {Whitespace, " "},
72 {Keyword, "what"},
73 {TextWhitespace, " "},
74 {Error, "there"},
75 }},
76 {"SourceMultiple", "hello world <? what ?> hello there <? what ?> hello", []Token{
77 {Keyword, "hello"},
78 {TextWhitespace, " "},
79 {Name, "world"},
80 {TextWhitespace, " "},
81 {CommentPreproc, "<?"},
82 {TextWhitespace, " "},
83 {Keyword, "what"},
84 {TextWhitespace, " "},
85 {CommentPreproc, "?>"},
86 {TextWhitespace, " "},
87 {Keyword, "hello"},
88 {TextWhitespace, " "},
89 {Name, "there"},
90 {TextWhitespace, " "},
91 {CommentPreproc, "<?"},
92 {TextWhitespace, " "},
93 {Keyword, "what"},
94 {TextWhitespace, " "},
95 {CommentPreproc, "?>"},
96 {TextWhitespace, " "},
97 {Keyword, "hello"},
98 }},
99 }
100 lang, root := makeDelegationTestLexers()
101 delegate := DelegatingLexer(root, lang)
102 for _, test := range testdata {
103
104 t.Run(test.name, func(t *testing.T) {
105 it, err := delegate.Tokenise(nil, test.source)
106 assert.NoError(t, err)
107 actual := it.Tokens()
108 assert.Equal(t, test.expected, actual)
109 })
110 }
111 }
112
View as plain text