-
Notifications
You must be signed in to change notification settings - Fork 22
/
tests.lua
1763 lines (1625 loc) · 54.1 KB
/
tests.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Copyright 2017-2024 Mitchell. See LICENSE.
-- Unit tests for Scintillua lexers.
package.path = 'lexers/?.lua;' .. package.path
local lexer = require('lexer')
local word_match = lexer.word_match
lpeg = require('lpeg') -- not local for use by lexers in Lua 5.2+
-- Scintilla normally defines these.
lexer.FOLD_BASE, lexer.FOLD_HEADER, lexer.FOLD_BLANK = 0x400, 0x2000, 0x1000
-- Helper assert functions.
-- Asserts the given lexer contains the default Scintillua tags and Scintilla style names, and
-- that those items are correctly numbered. Scintillua tag numbers start at 1 while Scintilla
-- style numbers start at 33.
-- Note: the tag and style tables used are copied from lexer.lua since they are local to that file.
-- @param lex The lexer to style-check.
function assert_default_tags(lex)
local default_tags = {
'whitespace', 'comment', 'string', 'number', 'keyword', 'identifier', 'operator', 'error',
'preprocessor', 'constant', 'variable', 'function', 'class', 'type', 'label', 'regex',
'embedded'
}
for i = 1, #default_tags do
local tag = default_tags[i]
assert(lex._TAGS[tag], string.format("tag '%s' does not exist", tag))
assert(lex._TAGS[tag] == i, 'default styles out of order')
end
local predefined_styles = {
'default', 'line.number', 'brace.light', 'brace.bad', 'control.char', 'indent.guide',
'call.tip', 'fold.display.text'
}
for i = 1, #predefined_styles do
local style = predefined_styles[i]
assert(lex._TAGS[style], string.format("style '%s' does not exist", style))
assert(lex._TAGS[style] == i + 32, 'predefined styles out of order')
end
assert(lex._TAGS['whitespace.' .. lex._name]) -- auto-added by lexer.new()
end
-- Asserts the given lexer contains the given ordered list of rules.
-- @param lex The lexer to rule-check.
-- @param rules The ordered list of rule names the lexer should have.
function assert_rules(lex, rules)
if rules[1] ~= 'whitespace' then table.insert(rules, 1, 'whitespace') end -- auto-added
local j = 1
for i = 1, #lex._rules do
assert(lex._rules[rules[j]], string.format("rule '%s' does not exist", rules[j]))
assert(lex._rules[i] == rules[j], string.format("'%s' ~= '%s'", lex._rules[i], rules[i] or ''))
j = j + 1
end
if #lex._rules ~= #rules then error(string.format("'%s' rule not found", rules[j])) end
end
-- Asserts the given lexer contains the given set of extra tags in addition to its defaults.
-- @param lex The lexer to style-check.
-- @param tags The list of extra tag names the lexer should have.
function assert_extra_tags(lex, tags)
for i = 1, #tags do
assert(lex._TAGS[tags[i]], string.format("'%s' not found", tags[i]))
assert(lex._extra_tags[tags[i]], string.format("'%s' not found", tags[i]))
end
end
-- Asserts the given lexer contains the given set of child lexer names.
-- @param lex The lexer to child-check.
-- @param children The list of child lexer names the lexer should have.
function assert_children(lex, children)
local j = 1
for i = 1, #lex._CHILDREN do
assert(lex._CHILDREN[i]._name == children[j],
string.format("'%s' ~= '%s'", lex._CHILDREN[i]._name, children[j] or ''))
j = j + 1
end
if #lex._CHILDREN ~= #children then error(string.format("child '%s' not found", children[j])) end
end
-- Asserts the given lexer produces the given tags after lexing the given code.
-- @param lex The lexer to use.
-- @param code The string code to lex.
-- @param expected_tags The list of expected tag names followed by the substring of code matched.
-- Whitespace tags are ignored for the sake of simplicity. Do not include them.
-- @param initial_style Optional current style. This is used for determining which language to
-- start in in a multiple-language lexer.
-- @usage assert_lex(lua, "print('hi')", {FUNCTION_BUILTIN, 'print', OPERATOR, '(', STRING, "'hi'",
-- OPERATOR, ')'}})
function assert_lex(lex, code, expected_tags, initial_style)
if lex._lexer then lex = lex._lexer end -- note: lexer.load() does this
local tags = lex:lex(code, initial_style or lex._TAGS['whitespace.' .. lex._name])
local j = 1
for i = 1, #tags, 2 do
if not tags[i]:find('^whitespace') then
local tag = tags[i]
local text = code:sub(tags[i - 1] or 0, tags[i + 1] - 1)
assert(tag == expected_tags[j] and text == expected_tags[j + 1], string.format(
"('%s', '%s') ~= ('%s', '%s')", tag, text, expected_tags[j], expected_tags[j + 1]))
j = j + 2
end
end
if j - 1 ~= #expected_tags then
error(string.format("('%s', '%s') not found", expected_tags[j], expected_tags[j + 1]))
end
end
-- Asserts the given lexer produces the given fold points after lexing the given code.
-- @param lex The lexer to use.
-- @param code The string code to fold.
-- @param expected_fold_points The list of expected fold points from the lexer. Each fold point
-- is just a line number, starting from 1.
-- @param initial_style Optional current style. This is used for determining which language to
-- start in in a multiple-language lexer.
-- @return fold levels for any further analysis
-- @usage assert_fold_points(lua, "if foo then\n bar\nend", {1})
function assert_fold_points(lex, code, expected_fold_points, initial_style)
if lex._lexer then lex = lex._lexer end -- note: lexer.load() does this
-- Since `M.style_at()` is provided by Scintilla and not available for tests, create it,
-- using data from `lexer.lex()`.
local tags = lex:lex(code, initial_style or lex._TAGS['whitespace.' .. lex._name])
lexer.style_at = setmetatable({}, {
__index = function(self, pos)
for i = 2, #tags, 2 do if pos < tags[i] then return tags[i - 1] end end
end
})
if not lexer.property then -- Scintilla normally creates this
lexer.property, lexer.property_int = {}, setmetatable({}, {
__index = function(t, k) return tonumber(lexer.property[k]) or 0 end,
__newindex = function() error('read-only property') end
})
end
lexer.property['fold'] = 1
local levels = lex:fold(code, 1, lexer.FOLD_BASE)
local j = 1
for i = 1, #levels do
local line_num = expected_fold_points[j]
if i == line_num then
assert(levels[i] >= lexer.FOLD_HEADER, string.format("line %i not a fold point", i))
j = j + 1
elseif line_num and i == -line_num then
assert(levels[i] > levels[i + 1] & ~(lexer.FOLD_HEADER | lexer.FOLD_BLANK),
string.format("line %i is not a fold end point", i))
j = j + 1
else
assert(levels[i] <= lexer.FOLD_HEADER, string.format("line %i is a fold point", i))
end
end
assert(j - 1 == #expected_fold_points, string.format("line %i is not a fold point", j))
return levels
end
-- Unit tests.
function test_to_eol()
local code = '#foo\\\nbar\\\nbaz'
assert(lexer.to_eol('#'):match(code) == 6)
assert(lexer.to_eol('#', true):match(code) == #code + 1)
assert(not lexer.to_eol('f'):match(code))
assert(lexer.to_eol():match(code) == 6)
end
function test_range()
assert(lexer.range('"'):match('"foo\\"bar\n"baz') == 12)
assert(lexer.range('"', true):match('"foo\\"bar\n"baz') == 10)
assert(lexer.range('"', false, false):match('"foo\\"bar\n"baz') == 7)
assert(lexer.range('(', ')'):match('(foo\\)bar)baz') == 7)
assert(lexer.range('/*', '*/'):match('/*/*foo*/bar*/baz') == 10)
assert(lexer.range('/*', '*/', false, false, true):match('/*/*foo*/bar*/baz') == 15)
end
function test_starts_line()
assert(lexer.starts_line('#'):match('#foo') == 2)
assert(lexer.starts_line('#'):match('\n#foo', 2) == 3)
assert(not lexer.starts_line('#'):match(' #foo', 2))
assert(lexer.starts_line('#', true):match(' #foo', 2) == 3)
assert(lexer.starts_line('#', true):match('\t#foo', 2) == 3)
assert(lexer.starts_line('#', true):match('\n\t#foo', 3) == 4)
assert(not lexer.starts_line('#', true):match('\n.#foo', 3))
end
function test_after_set()
local set = '=,'
local regex = lexer.range('/')
assert(lexer.after_set(set, regex):match('/foo'))
assert(lexer.after_set(set, regex):match('/foo/'))
assert(lexer.after_set(set, regex):match('foo=/bar/', 5))
assert(lexer.after_set(set, regex):match('foo=\n/bar/', 6))
assert(lexer.after_set(set, regex):match('foo, /bar/', 6))
assert(not lexer.after_set(set, regex):match('foo/bar', 4))
assert(not lexer.after_set(set, regex):match('foo / bar', 5))
assert(lexer.after_set(set .. ' ', regex):match('foo / bar', 5))
end
function test_word_match()
assert(word_match{'foo', 'bar', 'baz'}:match('foo') == 4)
assert(not word_match{'foo', 'bar', 'baz'}:match('foo_bar'))
assert(word_match({'foo!', 'bar?', 'baz.'}, true):match('FOO!') == 5)
assert(word_match{'foo '}:match('foo')) -- spaces not allowed
-- Test string list style.
assert(word_match('foo bar baz'):match('foo') == 4)
assert(not word_match('foo bar baz'):match('foo_bar'))
assert(word_match('foo! bar? baz.', true):match('FOO!') == 5)
end
local DEFAULT = lexer.DEFAULT
local COMMENT = lexer.COMMENT
local STRING = lexer.STRING
local NUMBER = lexer.NUMBER
local KEYWORD = lexer.KEYWORD
local IDENTIFIER = lexer.IDENTIFIER
local OPERATOR = lexer.OPERATOR
local ERROR = lexer.ERROR
local PREPROCESSOR = lexer.PREPROCESSOR
local VARIABLE = lexer.VARIABLE
local FUNCTION = lexer.FUNCTION
local CLASS = lexer.CLASS
local TYPE = lexer.TYPE
local LABEL = lexer.LABEL
local REGEX = lexer.REGEX
local FUNCTION_BUILTIN = lexer.FUNCTION_BUILTIN
local CONSTANT_BUILTIN = lexer.CONSTANT_BUILTIN
local FUNCTION_METHOD = lexer.FUNCTION_METHOD
local TAG = lexer.TAG
local ATTRIBUTE = lexer.ATTRIBUTE
local VARIABLE_BUILTIN = lexer.VARIABLE_BUILTIN
local HEADING = lexer.HEADING
local BOLD = lexer.BOLD
local ITALIC = lexer.ITALIC
local UNDERLINE = lexer.UNDERLINE
local CODE = lexer.CODE
local LINK = lexer.LINK
local REFERENCE = lexer.REFERENCE
local ANNOTATION = lexer.ANNOTATION
local LIST = lexer.LIST
-- Tests a basic lexer with a few simple rules and no custom styles.
function test_basics()
local lex = lexer.new('test')
assert_default_tags(lex)
lex:add_rule('keyword', lex:tag(KEYWORD, word_match('foo bar baz')))
lex:add_rule('string', lex:tag(STRING, lexer.range('"')))
lex:add_rule('number', lex:tag(NUMBER, lexer.integer))
local code = [[foo bar baz "foo bar baz" 123]]
local tags = {
KEYWORD, 'foo', --
KEYWORD, 'bar', --
KEYWORD, 'baz', --
STRING, '"foo bar baz"', --
NUMBER, '123'
}
assert_lex(lex, code, tags)
end
-- Tests that lexer rules are added in an ordered sequence and that modifying rules in place
-- works as expected.
function test_rule_order()
local lex = lexer.new('test')
lex:add_rule('identifier', lex:tag(IDENTIFIER, lexer.word))
lex:add_rule('keyword', lex:tag(KEYWORD, lpeg.P('foo')))
local code = [[foo bar]]
local tags = {IDENTIFIER, 'foo', IDENTIFIER, 'bar'}
assert_lex(lex, code, tags)
-- Modify the identifier rule to not catch keywords.
lex:modify_rule('identifier', -lpeg.P('foo') * lex:tag(IDENTIFIER, lexer.word))
tags = {KEYWORD, 'foo', IDENTIFIER, 'bar'}
assert_lex(lex, code, tags)
end
-- Tests a basic lexer with a couple of simple rules and a custom tag.
function test_add_tag()
local lex = lexer.new('test')
assert_default_tags(lex)
lex:add_rule('keyword', lex:tag('custom', word_match('foo bar baz')))
assert_default_tags(lex)
local code = [[foo bar baz]]
local tags = {'custom', 'foo', 'custom', 'bar', 'custom', 'baz'}
assert_lex(lex, code, tags)
end
-- Tests word lists.
function test_word_list()
local lex = lexer.new('test')
lex:add_rule('keyword', lex:tag(KEYWORD, lex:word_match(KEYWORD)))
lex:add_rule('identifier', lex:tag(IDENTIFIER, lexer.word))
lex:add_rule('operator', lex:tag(OPERATOR, '.'))
local code = [[foo bar.baz quux]]
local tags = {
IDENTIFIER, 'foo', --
IDENTIFIER, 'bar', --
OPERATOR, '.', --
IDENTIFIER, 'baz', --
IDENTIFIER, 'quux'
}
assert_lex(lex, code, tags)
lex:set_word_list(KEYWORD, 'foo quux')
tags[1 * 2 - 1], tags[1 * 2] = KEYWORD, 'foo'
tags[5 * 2 - 1], tags[5 * 2] = KEYWORD, 'quux'
assert_lex(lex, code, tags)
lex:set_word_list(KEYWORD, 'bar', true) -- append
tags[2 * 2 - 1], tags[2 * 2] = KEYWORD, 'bar'
assert_lex(lex, code, tags)
lex:set_word_list(KEYWORD, {'bar.baz'})
tags = {IDENTIFIER, 'foo', KEYWORD, 'bar.baz', IDENTIFIER, 'quux'}
assert_lex(lex, code, tags)
end
-- Tests a simple parent lexer embedding a simple child lexer.
-- Ensures the child's custom tags are also copied over.
function test_embed()
-- Create the parent lexer.
local parent = lexer.new('parent')
assert_default_tags(parent)
parent:add_rule('identifier', parent:tag('parent', lexer.word))
-- Create the child lexer.
local child = lexer.new('child')
assert_default_tags(child)
child:add_rule('number', child:tag('child', lexer.integer))
-- Assert the child's tags are not embedded in the parent yet.
assert(not parent._TAGS['whitespace.' .. child._name])
assert(not parent._extra_tags['whitespace.' .. child._name])
assert(not parent._TAGS['child'])
assert(not parent._extra_tags['child'])
-- Embed the child into the parent and verify the child's tags were copied over.
local start_rule = parent:tag('transition', lpeg.P('['))
local end_rule = parent:tag('transition', lpeg.P(']'))
parent:embed(child, start_rule, end_rule)
assert_default_tags(parent)
-- Lex some parent -> child -> parent code.
local code = [[foo [1, 2, 3] bar]]
local tags = {
'parent', 'foo', --
'transition', '[', --
'child', '1', DEFAULT, ',', 'child', '2', DEFAULT, ',', 'child', '3', --
'transition', ']', --
'parent', 'bar'
}
assert_lex(parent, code, tags)
-- Lex some child -> parent code, starting from within the child.
code = [[2, 3] bar]]
tags = {
'child', '2', DEFAULT, ',', 'child', '3', 'transition', ']', --
'parent', 'bar'
}
local initial_style = parent._TAGS['whitespace.' .. child._name]
assert_lex(parent, code, tags, initial_style)
end
-- Tests a simple child lexer embedding itself within a simple parent lexer.
-- Ensures the child's custom tags are also copied over.
function test_embed_into()
-- Create the child lexer.
local child = lexer.new('child')
child:add_rule('number', child:tag('child', lexer.integer))
-- Create the parent lexer.
local parent = lexer.new('parent')
parent:add_rule('identifier', parent:tag('parent', lexer.word))
-- Embed the child within the parent and verify the child's custom tags were copied over.
local start_rule = parent:tag('transition', lpeg.P('['))
local end_rule = parent:tag('transition', lpeg.P(']'))
parent:embed(child, start_rule, end_rule)
assert_default_tags(parent)
-- Verify any subsequent fold point additions to the child are copied to the parent.
child:add_fold_point('transition', '[', ']')
assert(parent._fold_points['transition']['['] == 1)
assert(parent._fold_points['transition'][']'] == -1)
-- Lex some parent -> child -> parent code.
local code = [[foo [1, 2, 3] bar]]
local tags = {
'parent', 'foo', --
'transition', '[', --
'child', '1', DEFAULT, ',', 'child', '2', DEFAULT, ',', 'child', '3', --
'transition', ']', --
'parent', 'bar'
}
assert_lex(child, code, tags)
-- Lex some child -> parent code, starting from within the child.
code = [[2, 3] bar]]
tags = {
'child', '2', DEFAULT, ',', 'child', '3', 'transition', ']', --
'parent', 'bar'
}
local initial_style = parent._TAGS['whitespace.' .. child._name]
assert_lex(child, code, tags, initial_style)
-- Fold some code.
code = [[
foo [
1, 2, 3
] bar
baz
]]
local folds = {1, -3}
local levels = assert_fold_points(child, code, folds)
assert(levels[3] > levels[4]) -- verify ']' is fold end point
end
-- Tests a proxy lexer that inherits from a simple parent lexer and embeds a simple child lexer.
-- Ensures both the proxy's and child's custom tags are also copied over.
function test_proxy()
-- Create the parent lexer.
local parent = lexer.new('parent')
parent:add_rule('identifier', parent:tag('parent', lexer.word))
-- Create the child lexer.
local child = lexer.new('child')
child:add_rule('number', child:tag('child', lexer.integer))
-- Create the proxy lexer.
local proxy = lexer.new('proxy', {inherit = parent})
-- Embed the child into the parent and verify the proxy's custom tag was copied over.
local start_rule = proxy:tag('transition', lpeg.P('['))
local end_rule = proxy:tag('transition', lpeg.P(']'))
proxy:embed(child, start_rule, end_rule)
-- Lex some parent -> child -> parent code.
local code = [[foo [1, 2, 3] bar]]
local tags = {
'parent', 'foo', --
'transition', '[', --
'child', '1', DEFAULT, ',', 'child', '2', DEFAULT, ',', 'child', '3', --
'transition', ']', --
'parent', 'bar'
}
assert_lex(proxy, code, tags)
-- Lex some child -> parent code, starting from within the child.
code = [[ 2, 3] bar]]
tags = {
'child', '2', DEFAULT, ',', 'child', '3', 'transition', ']', --
'parent', 'bar'
}
local initial_style = parent._TAGS['whitespace.' .. child._name]
assert_lex(proxy, code, tags, initial_style)
-- Verify any subsequent fold point additions to the proxy are copied to the parent.
proxy:add_fold_point('transition', '[', ']')
assert(parent._fold_points['transition']['['] == 1)
assert(parent._fold_points['transition'][']'] == -1)
-- Fold some code.
code = [[
foo [
1, 2, 3
] bar
baz
]]
local folds = {1, -3}
local levels = assert_fold_points(proxy, code, folds)
assert(levels[3] > levels[4]) -- verify ']' is fold end point
end
-- Tests a lexer that inherits from another one.
function test_inherits_rules()
local lex = lexer.new('test')
lex:add_rule('keyword', lex:tag(KEYWORD, word_match('foo bar baz')))
-- Verify inherited rules are used.
local sublexer = lexer.new('test2', {inherit = lex})
local code = [[foo bar baz]]
local tags = {KEYWORD, 'foo', KEYWORD, 'bar', KEYWORD, 'baz'}
assert_lex(sublexer, code, tags)
-- Verify subsequently added rules are also used.
sublexer:add_rule('keyword2', sublexer:tag(KEYWORD, lpeg.P('quux')))
code = [[foo bar baz quux]]
tags = {KEYWORD, 'foo', KEYWORD, 'bar', KEYWORD, 'baz', KEYWORD, 'quux'}
assert_lex(sublexer, code, tags)
end
-- Tests that fold words are folded properly, even if fold words are substrings of others
-- (e.g. "if" and "endif").
function test_fold_words()
local lex = lexer.new('test')
lex:add_rule('keyword', lex:tag(KEYWORD, word_match('if endif')))
lex:add_fold_point(KEYWORD, 'if', 'endif')
local code = [[
if foo
bar
endif
ifbaz
quuxif
]]
local folds = {1, -3}
local levels = assert_fold_points(lex, code, folds)
assert(levels[2] == lexer.FOLD_BASE + 1)
assert(levels[3] == lexer.FOLD_BASE + 1)
assert(levels[4] == lexer.FOLD_BASE)
end
-- Tests folding by indentation.
function test_fold_by_indentation()
local lex = lexer.new('test', {fold_by_indentation = true})
local code = [[
if foo:
bar
else:
baz
]]
lexer.fold_level = {lexer.FOLD_BASE} -- Scintilla normally creates this
local folds = {1, -2, 3}
assert_fold_points(lex, code, folds)
end
-- Tests that all lexers load and lex text.
function test_loads()
local p = io.popen('ls -1 lexers/*.lua')
local files = p:read('*a')
p:close()
for file in files:gmatch('[^\n]+') do
local lex_name = file:match('^lexers/(.+)%.lua$')
if lex_name ~= 'lexer' then
local lex = lexer.load(lex_name, nil, true)
assert_default_tags(lex)
local tags = lex:lex('test')
assert(#tags >= 2)
end
end
end
function test_names()
lexer.property['scintillua.lexers'] = ''
local names = lexer.names()
assert(#names > 0)
local lua_found = false
for _, name in ipairs(names) do
assert(name ~= 'lexer')
if name == 'lua' then lua_found = true end
end
assert(lua_found)
local names2 = lexer.names('lexers')
assert(#names == #names2)
lexer.property['scintillua.lexers'] = 'lexers'
local names3 = lexer.names()
assert(#names == #names3)
end
-- Tests the Lua lexer.
function test_lua()
local lua = lexer.load('lua')
assert(lua._name == 'lua')
assert_default_tags(lua)
local rules = {
'keyword', 'function', 'constant', 'identifier', 'string', 'comment', 'number', 'label',
'attribute', 'operator'
}
assert_rules(lua, rules)
local tags = {
STRING .. '.longstring', --
'whitespace.lua' -- language-specific whitespace for multilang lexers
}
assert_extra_tags(lua, tags)
-- Lexing tests.
local code = [=[
--[[ Comment. ]]--
::begin::
local a <const> = -1 + 2.0e3 - 0x40
local b = "two"..[[three]] .. 'four\''
c ={_G.print, type = foo{math.pi}}
print(string.upper'a', b:upper())
]=]
tags = {
COMMENT, '--[[ Comment. ]]', COMMENT, '--', --
LABEL, '::begin::', --
KEYWORD, 'local', IDENTIFIER, 'a', ATTRIBUTE, '<const>', --
OPERATOR, '=', --
NUMBER, '-1', OPERATOR, '+', NUMBER, '2.0e3', OPERATOR, '-', NUMBER, '0x40', --
KEYWORD, 'local', IDENTIFIER, 'b', --
OPERATOR, '=', --
STRING, '"two"', --
OPERATOR, '..', --
STRING .. '.longstring', '[[three]]', --
OPERATOR, '..', --
STRING, "'four\\''", --
IDENTIFIER, 'c', --
OPERATOR, '=', --
OPERATOR, '{', --
CONSTANT_BUILTIN, '_G', OPERATOR, '.', IDENTIFIER, 'print', --
OPERATOR, ',', --
IDENTIFIER, 'type', --
OPERATOR, '=', --
FUNCTION, 'foo', OPERATOR, '{', CONSTANT_BUILTIN, 'math.pi', OPERATOR, '}', OPERATOR, '}', --
FUNCTION_BUILTIN, 'print', --
OPERATOR, '(', --
FUNCTION_BUILTIN, 'string.upper', STRING, "'a'", --
OPERATOR, ',', --
IDENTIFIER, 'b', OPERATOR, ':', FUNCTION_METHOD, 'upper', OPERATOR, '(', OPERATOR, ')', --
OPERATOR, ')'
}
assert_lex(lua, code, tags)
-- Folding tests.
code = [=[
if foo then
bar
end
for k, v in pairs(foo) do
bar
end
function foo(bar)
baz
end
repeat
foo
until bar
--[[
foo
]]
(foo,
bar,
baz)
{foo,
bar,
baz}
]=]
local folds = {1, -3, 4, -6, 7, -9, 10, -12, 13, -15, 16, -18, 19}
assert_fold_points(lua, code, folds)
-- Test overriding keywords.
lua:set_word_list(KEYWORD, '')
assert_lex(lua, 'if', {IDENTIFIER, 'if'})
-- Test adding to built-in functions.
lua:set_word_list(FUNCTION_BUILTIN, 'module', true) -- from Lua 5.1
assert_lex(lua, 'dofile(', {FUNCTION_BUILTIN, 'dofile', OPERATOR, '('})
assert_lex(lua, 'module(', {FUNCTION_BUILTIN, 'module', OPERATOR, '('})
end
-- Tests the C lexer.
function test_c()
local c = lexer.load('c')
assert(c._name == 'c')
assert_default_tags(c)
-- Lexing tests.
local code = ([[
/* Comment. */
#include <stdio.h>
#include "lua.h"
# define INT_MAX_ 1
int main(int argc, char **argv) {
begin:
if (NULL) // comment
printf("%ld %f %s %i", 1l, 1.0e-1f, L"foo", INT_MAX);
foo.free, foo->free(), free(foo);
return 0x0?argc:0;
}
]]):gsub(' ', '') -- strip indent
local tags = {
COMMENT, '/* Comment. */', --
PREPROCESSOR, '#include', STRING, '<stdio.h>', --
PREPROCESSOR, '#include', STRING, '"lua.h"', --
PREPROCESSOR, '# define', IDENTIFIER, 'INT_MAX_', NUMBER, '1', --
TYPE, 'int', FUNCTION, 'main', --
OPERATOR, '(', --
TYPE, 'int', IDENTIFIER, 'argc', --
OPERATOR, ',', --
TYPE, 'char', OPERATOR, '*', OPERATOR, '*', IDENTIFIER, 'argv', --
OPERATOR, ')', --
OPERATOR, '{', --
LABEL, 'begin:', --
KEYWORD, 'if', OPERATOR, '(', CONSTANT_BUILTIN, 'NULL', OPERATOR, ')', COMMENT, '// comment', --
FUNCTION_BUILTIN, 'printf', --
OPERATOR, '(', --
STRING, '"%ld %f %s %i"', --
OPERATOR, ',', --
NUMBER, '1l', --
OPERATOR, ',', --
NUMBER, '1.0e-1f', --
OPERATOR, ',', --
STRING, 'L"foo"', --
OPERATOR, ',', --
CONSTANT_BUILTIN, 'INT_MAX', --
OPERATOR, ')', --
OPERATOR, ';', --
IDENTIFIER, 'foo', OPERATOR, '.', IDENTIFIER, 'free', --
OPERATOR, ',', --
IDENTIFIER, 'foo', --
OPERATOR, '-', OPERATOR, '>', --
FUNCTION_METHOD, 'free', OPERATOR, '(', OPERATOR, ')', --
OPERATOR, ',', --
FUNCTION_BUILTIN, 'free', OPERATOR, '(', IDENTIFIER, 'foo', OPERATOR, ')', --
OPERATOR, ';', --
KEYWORD, 'return', --
NUMBER, '0x0', --
OPERATOR, '?', --
IDENTIFIER, 'argc', -- should not be a label
OPERATOR, ':', --
NUMBER, '0', --
OPERATOR, ';', --
OPERATOR, '}'
}
assert_lex(c, code, tags)
-- Folding tests.
code = ([[
if (foo) {
bar;
}
/**
* foo
*/
#ifdef foo
bar;
#endif
]]):gsub(' ', '') -- strip indent
local folds = {1, -3, 4, -6, 7}
assert_fold_points(c, code, folds)
end
-- Tests the HTML lexer and its embedded languages.
function test_html()
local html = lexer.load('html')
assert(html._name == 'html')
assert_default_tags(html)
local rules = {
'comment', 'doctype', 'tag', 'tag_close', 'attribute', -- 'equals',
'string', 'number', 'entity'
}
assert_rules(html, rules)
local tags = {
TAG .. '.doctype', TAG .. '.unknown', ATTRIBUTE .. '.unknown', --
'whitespace.html', -- HTML
'property', 'whitespace.css', -- CSS
'whitespace.javascript', -- JS
'whitespace.coffeescript' -- CoffeeScript
}
assert_extra_tags(html, tags)
assert_children(html, {'css', 'css.style', 'javascript', 'coffeescript'})
-- Lexing tests.
local code = [[
<!DOCTYPE html>
<!-- Comment. -->
<html>
<HEAD>
<style type="text/css">
@charset "utf8"
/* Another comment. */
h1:hover, h2::first-line {
color: red;
border: 1.5px solid #0000FF;
background: url("/images/image.jpg");
}
table.class {}
</style>
<script type="text/javascript">
/* A third comment. */
var a = 1 + 2.0e3 - 0x40;
var b = "two" + `three`;
var c = /pattern/i;
foo(eval(arguments), bar.baz(), Object);
</script>
</HEAD>
<bod clss = "unknown">
<hr tabindex=1/> ©
<div style="float: right">
</html>
]]
local tag_chars = TAG .. '.chars'
tags = {
TAG .. '.doctype', '<!DOCTYPE html>', --
COMMENT, '<!-- Comment. -->', --
tag_chars, '<', TAG, 'html', tag_chars, '>', --
tag_chars, '<', TAG, 'HEAD', tag_chars, '>', --
tag_chars, '<', TAG, 'style', --
ATTRIBUTE, 'type', OPERATOR, '=', STRING, '"text/css"', --
tag_chars, '>', --
PREPROCESSOR, '@charset', STRING, '"utf8"', --
COMMENT, '/* Another comment. */', --
TAG, 'h1', 'pseudoclass', ':hover', --
OPERATOR, ',', --
TAG, 'h2', 'pseudoelement', '::first-line', --
OPERATOR, '{', --
'property', 'color', OPERATOR, ':', CONSTANT_BUILTIN, 'red', OPERATOR, ';', --
'property', 'border', --
OPERATOR, ':', --
NUMBER, '1.5px', CONSTANT_BUILTIN, 'solid', NUMBER, '#0000FF', --
OPERATOR, ';', --
'property', 'background', --
OPERATOR, ':', --
FUNCTION_BUILTIN, 'url', OPERATOR, '(', STRING, '"/images/image.jpg"', OPERATOR, ')', --
OPERATOR, ';', --
OPERATOR, '}', --
TAG, 'table', OPERATOR, '.', IDENTIFIER, 'class', OPERATOR, '{', OPERATOR, '}', --
tag_chars, '</', TAG, 'style', tag_chars, '>', --
tag_chars, '<', TAG, 'script', --
ATTRIBUTE, 'type', OPERATOR, '=', STRING, '"text/javascript"', --
tag_chars, '>', --
COMMENT, '/* A third comment. */', --
KEYWORD, 'var', IDENTIFIER, 'a', --
OPERATOR, '=', --
NUMBER, '1', OPERATOR, '+', NUMBER, '2.0e3', OPERATOR, '-', NUMBER, '0x40', --
OPERATOR, ';', --
KEYWORD, 'var', IDENTIFIER, 'b', --
OPERATOR, '=', --
STRING, '"two"', OPERATOR, '+', STRING, '`three`', --
OPERATOR, ';', --
KEYWORD, 'var', IDENTIFIER, 'c', OPERATOR, '=', REGEX, '/pattern/i', OPERATOR, ';', --
FUNCTION, 'foo', --
OPERATOR, '(', --
FUNCTION_BUILTIN, 'eval', OPERATOR, '(', CONSTANT_BUILTIN, 'arguments', OPERATOR, ')', --
OPERATOR, ',', --
IDENTIFIER, 'bar', OPERATOR, '.', FUNCTION_METHOD, 'baz', OPERATOR, '(', OPERATOR, ')', --
OPERATOR, ',', --
TYPE, 'Object', --
OPERATOR, ')', --
OPERATOR, ';', --
tag_chars, '</', TAG, 'script', tag_chars, '>', --
tag_chars, '</', TAG, 'HEAD', tag_chars, '>', --
tag_chars, '<', TAG .. '.unknown', 'bod', --
ATTRIBUTE .. '.unknown', 'clss', OPERATOR, '=', STRING, '"unknown"', --
tag_chars, '>', --
tag_chars, '<', TAG .. '.single', 'hr', --
ATTRIBUTE, 'tabindex', OPERATOR, '=', NUMBER, '1', --
tag_chars, '/>', --
CONSTANT_BUILTIN .. '.entity', '©', --
tag_chars, '<', TAG, 'div', --
ATTRIBUTE, 'style', OPERATOR, '=', STRING, '"', --
'property', 'float', OPERATOR, ':', CONSTANT_BUILTIN, 'right', --
STRING, '"', tag_chars, '>', --
tag_chars, '</', TAG, 'html', tag_chars, '>'
}
assert_lex(html, code, tags)
-- Folding tests.
local symbols = {'<', '<!--', '-->', '{', '}', '/*', '*/'}
for i = 1, #symbols do assert(html._fold_points._symbols[symbols[i]]) end
assert(html._fold_points[TAG .. '.chars']['<'])
assert(html._fold_points[COMMENT]['<!--'])
assert(html._fold_points[COMMENT]['-->'])
assert(html._fold_points[OPERATOR]['{'])
assert(html._fold_points[OPERATOR]['}'])
assert(html._fold_points[COMMENT]['/*'])
assert(html._fold_points[COMMENT]['*/'])
code = [[
<html>
foo
</html>
<body/>
<style type="text/css">
h1 {
foo;
}
</style>
<script type="text/javascript">
function foo() {
bar;
}
</script>
h1 {
foo;
}
function foo() {
bar;
}
]]
local folds = {1, -3, 5, 6, -8, -9, 10, 11, -13}
local levels = assert_fold_points(html, code, folds)
assert(levels[3] > levels[4]) -- </html> is ending fold point
end
-- Tests the PHP lexer.
function test_php()
local php = lexer.load('php')
assert(php._name == 'php')
assert_default_tags(php)
assert_extra_tags(php, {'whitespace.php'})
-- Lexing tests
-- Starting in HTML.
local code = [[<h1><?php echo "hi" . PHP_OS . foo() . bar->baz(); ?></h1>]]
local tag_chars = TAG .. '.chars'
local tags = {
tag_chars, '<', TAG, 'h1', tag_chars, '>', --
PREPROCESSOR, '<?php ', --
KEYWORD, 'echo', --
STRING, '"hi"', --
OPERATOR, '.', --
CONSTANT_BUILTIN, 'PHP_OS', --
OPERATOR, '.', --
FUNCTION, 'foo', OPERATOR, '(', OPERATOR, ')', --
OPERATOR, '.', --
IDENTIFIER, 'bar', --
OPERATOR, '-', OPERATOR, '>', --
FUNCTION_METHOD, 'baz', OPERATOR, '(', OPERATOR, ')', --
OPERATOR, ';', --
PREPROCESSOR, '?>', --
tag_chars, '</', TAG, 'h1', tag_chars, '>'
}
local initial_style = php._TAGS['whitespace.html']
assert_lex(php, code, tags, initial_style)
initial_style = php._TAGS['default'] -- also test non-ws init style
assert_lex(php, code, tags, initial_style)
initial_style = php._TAGS['default'] -- also test non-ws init style
assert_lex(php, code, tags, initial_style)
-- Starting in PHP.
code = [[echo "hi";]]
initial_style = php._TAGS['whitespace.php']
tags = {KEYWORD, 'echo', STRING, '"hi"', OPERATOR, ';'}
assert_lex(php, code, tags, initial_style)
-- Folding tests.
local symbols = {'<?', '?>', '/*', '*/', '{', '}', '(', ')'}
for i = 1, #symbols do assert(php._fold_points._symbols[symbols[i]]) end
assert(php._fold_points[PREPROCESSOR]['<?'])
assert(php._fold_points[PREPROCESSOR]['?>'])
assert(php._fold_points[COMMENT]['/*'])
assert(php._fold_points[COMMENT]['*/'])
assert(php._fold_points[OPERATOR]['{'])
assert(php._fold_points[OPERATOR]['}'])
assert(php._fold_points[OPERATOR]['('])
assert(php._fold_points[OPERATOR][')'])
end
-- Tests the Ruby lexer.
function test_ruby()
local ruby = lexer.load('ruby')
-- Lexing tests.
local code = [[
# Comment.
require "foo"
$a = 1 + 2.0e3 - 0x4_0 if true
b = "two" + %q[three] + <<-FOUR
four
FOUR
puts :c, foo.puts
]]
local tags = {
COMMENT, '# Comment.', --
FUNCTION_BUILTIN, 'require', STRING, '"foo"', --
VARIABLE, '$a', --
OPERATOR, '=', --
NUMBER, '1', OPERATOR, '+', NUMBER, '2.0e3', OPERATOR, '-', NUMBER, '0x4_0', --
KEYWORD, 'if', KEYWORD, 'true', --
IDENTIFIER, 'b', --
OPERATOR, '=', --
STRING, '"two"', --
OPERATOR, '+', --
STRING, '%q[three]', --
OPERATOR, '+', --
STRING, '<<-FOUR\n four\n FOUR', --
FUNCTION_BUILTIN, 'puts', STRING .. '.symbol', ':c', --
OPERATOR, ',', --
IDENTIFIER, 'foo', OPERATOR, '.', IDENTIFIER, 'puts'
}
assert_lex(ruby, code, tags)
-- Folding tests.
local fold_keywords = {
begin = 1, class = 1, def = 1, ['do'] = 1, ['for'] = 1, module = 1, case = 1,
['if'] = function() end, ['while'] = function() end, unless = function() end,
['until'] = function() end, ['end'] = -1
}
for k, v in pairs(fold_keywords) do
assert(ruby._fold_points._symbols[k])
if type(v) == 'number' then
assert(ruby._fold_points[KEYWORD][k] == v)
else
assert(type(ruby._fold_points[KEYWORD][k]) == 'function')
end
end
local fold_operators = {'(', ')', '[', ']', '{', '}'}
for i = 1, #fold_operators do
assert(ruby._fold_points._symbols[fold_operators[i]])
assert(ruby._fold_points[OPERATOR][fold_operators[i]])
end
code = [=[
class Foo
bar
end
foo.each do |v|
bar
end
def foo(bar)
baz
end
=begin
foo
=end
(foo,
bar,