blob: 934afdec1b7b4e723ce34bb8d44d1c96beffe975 [file] [log] [blame]
Bram Moolenaard7a06b12013-05-21 13:05:15 +02001Test for regexp patterns with multi-byte support, using utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002See test64 for the non-multi-byte tests.
3
4A pattern that gives the expected result produces OK, so that we know it was
5actually tried.
6
7STARTTEST
8:so small.vim
9:so mbyte.vim
Bram Moolenaar1d814752013-05-24 20:25:33 +020010:set nocp encoding=utf-8 viminfo+=nviminfo nomore
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020011:" tl is a List of Lists with:
Bram Moolenaar3f1682e2013-05-26 14:32:05 +020012:" 2: test auto/old/new 0: test auto/old 1: test auto/new
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020013:" regexp pattern
14:" text to test the pattern on
15:" expected match (optional)
16:" expected submatch 1 (optional)
17:" expected submatch 2 (optional)
18:" etc.
19:" When there is no match use only the first two items.
20:let tl = []
Bram Moolenaar901e58c2015-08-11 18:33:48 +020021:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020022:"""" Multi-byte character tests. These will fail unless vim is compiled
23:"""" with Multibyte (FEAT_MBYTE) or BIG/HUGE features.
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020024:call add(tl, [2, '[[:alpha:][=a=]]\+', '879 aiaãâaiuvna ', 'aiaãâaiuvna'])
25:call add(tl, [2, '[[=a=]]\+', 'ddaãâbcd', 'aãâ']) " equivalence classes
26:call add(tl, [2, '[^ม ]\+', 'มม oijasoifjos ifjoisj f osij j มมมมม abcd', 'oijasoifjos'])
27:call add(tl, [2, ' [^ ]\+', 'start มabcdม ', ' มabcdม'])
28:call add(tl, [2, '[ม[:alpha:][=a=]]\+', '879 aiaãมâมaiuvna ', 'aiaãมâมaiuvna'])
Bram Moolenaar901e58c2015-08-11 18:33:48 +020029:
Bram Moolenaar0fabe3f2013-05-21 12:34:17 +020030:" this is not a normal "i" but 0xec
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020031:call add(tl, [2, '\p\+', 'ìa', 'ìa'])
Bram Moolenaarac7c33e2013-07-21 17:06:00 +020032:call add(tl, [2, '\p*', 'aあ', 'aあ'])
Bram Moolenaar901e58c2015-08-11 18:33:48 +020033:
Bram Moolenaard2470e92013-05-21 13:30:21 +020034:"""" Test recognition of some character classes
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020035:call add(tl, [2, '\i\+', '&*¨xx ', 'xx'])
36:call add(tl, [2, '\f\+', '&*Ÿfname ', 'fname'])
Bram Moolenaar901e58c2015-08-11 18:33:48 +020037:
Bram Moolenaar3c577f22013-05-24 21:59:54 +020038:"""" Test composing character matching
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020039:call add(tl, [2, '.ม', 'xม่x yมy', 'yม'])
40:call add(tl, [2, '.ม่', 'xม่x yมy', 'xม่'])
41:call add(tl, [2, "\u05b9", " x\u05b9 ", "x\u05b9"])
42:call add(tl, [2, ".\u05b9", " x\u05b9 ", "x\u05b9"])
43:call add(tl, [2, "\u05b9\u05bb", " x\u05b9\u05bb ", "x\u05b9\u05bb"])
44:call add(tl, [2, ".\u05b9\u05bb", " x\u05b9\u05bb ", "x\u05b9\u05bb"])
Bram Moolenaar3f1682e2013-05-26 14:32:05 +020045:call add(tl, [2, "\u05bb\u05b9", " x\u05b9\u05bb ", "x\u05b9\u05bb"])
46:call add(tl, [2, ".\u05bb\u05b9", " x\u05b9\u05bb ", "x\u05b9\u05bb"])
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020047:call add(tl, [2, "\u05b9", " y\u05bb x\u05b9 ", "x\u05b9"])
48:call add(tl, [2, ".\u05b9", " y\u05bb x\u05b9 ", "x\u05b9"])
Bram Moolenaar3f1682e2013-05-26 14:32:05 +020049:call add(tl, [2, "\u05b9", " y\u05bb\u05b9 x\u05b9 ", "y\u05bb\u05b9"])
50:call add(tl, [2, ".\u05b9", " y\u05bb\u05b9 x\u05b9 ", "y\u05bb\u05b9"])
51:call add(tl, [1, "\u05b9\u05bb", " y\u05b9 x\u05b9\u05bb ", "x\u05b9\u05bb"])
52:call add(tl, [2, ".\u05b9\u05bb", " y\u05bb x\u05b9\u05bb ", "x\u05b9\u05bb"])
Bram Moolenaar6082bea2014-05-13 18:04:00 +020053:call add(tl, [2, "a", "ca\u0300t"])
Bram Moolenaarcaca6462014-05-13 20:15:37 +020054:call add(tl, [2, "ca", "ca\u0300t"])
Bram Moolenaar6082bea2014-05-13 18:04:00 +020055:call add(tl, [2, "a\u0300", "ca\u0300t", "a\u0300"])
Bram Moolenaarcaca6462014-05-13 20:15:37 +020056:call add(tl, [2, 'a\%C', "ca\u0300t", "a\u0300"])
57:call add(tl, [2, 'ca\%C', "ca\u0300t", "ca\u0300"])
58:call add(tl, [2, 'ca\%Ct', "ca\u0300t", "ca\u0300t"])
Bram Moolenaar901e58c2015-08-11 18:33:48 +020059:
60:
Bram Moolenaar1d814752013-05-24 20:25:33 +020061:"""" Test \Z
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020062:call add(tl, [2, 'ú\Z', 'x'])
63:call add(tl, [2, 'יהוה\Z', 'יהוה', 'יהוה'])
64:call add(tl, [2, 'יְהוָה\Z', 'יהוה', 'יהוה'])
65:call add(tl, [2, 'יהוה\Z', 'יְהוָה', 'יְהוָה'])
66:call add(tl, [2, 'יְהוָה\Z', 'יְהוָה', 'יְהוָה'])
67:call add(tl, [2, 'יְ\Z', 'וְיַ', 'יַ'])
68:call add(tl, [2, "ק\u200d\u05b9x\\Z", "xק\u200d\u05b9xy", "ק\u200d\u05b9x"])
69:call add(tl, [2, "ק\u200d\u05b9x\\Z", "xק\u200dxy", "ק\u200dx"])
70:call add(tl, [2, "ק\u200dx\\Z", "xק\u200d\u05b9xy", "ק\u200d\u05b9x"])
71:call add(tl, [2, "ק\u200dx\\Z", "xק\u200dxy", "ק\u200dx"])
Bram Moolenaar3451d662013-05-26 15:14:55 +020072:call add(tl, [2, "\u05b9\\Z", "xyz"])
73:call add(tl, [2, "\\Z\u05b9", "xyz"])
74:call add(tl, [2, "\u05b9\\Z", "xy\u05b9z", "y\u05b9"])
75:call add(tl, [2, "\\Z\u05b9", "xy\u05b9z", "y\u05b9"])
76:call add(tl, [1, "\u05b9\\+\\Z", "xy\u05b9z\u05b9 ", "y\u05b9z\u05b9"])
77:call add(tl, [1, "\\Z\u05b9\\+", "xy\u05b9z\u05b9 ", "y\u05b9z\u05b9"])
Bram Moolenaar901e58c2015-08-11 18:33:48 +020078:
Bram Moolenaard2470e92013-05-21 13:30:21 +020079:"""" Combining different tests and features
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020080:call add(tl, [2, '[^[=a=]]\+', 'ddaãâbcd', 'dd'])
Bram Moolenaar901e58c2015-08-11 18:33:48 +020081:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020082:"""" Run the tests
Bram Moolenaar901e58c2015-08-11 18:33:48 +020083:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020084:"
85:for t in tl
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020086: let re = t[0]
87: let pat = t[1]
88: let text = t[2]
89: let matchidx = 3
90: for engine in [0, 1, 2]
Bram Moolenaar3f1682e2013-05-26 14:32:05 +020091: if engine == 2 && re == 0 || engine == 1 && re == 1
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +020092: continue
93: endif
94: let &regexpengine = engine
Bram Moolenaar16299b52013-05-30 18:45:23 +020095: try
96: let l = matchlist(text, pat)
97: catch
Bram Moolenaarcaca6462014-05-13 20:15:37 +020098: $put ='ERROR ' . engine . ': pat: \"' . pat . '\", text: \"' . text . '\", caused an exception: \"' . v:exception . '\"'
Bram Moolenaar16299b52013-05-30 18:45:23 +020099: endtry
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200100:" check the match itself
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +0200101: if len(l) == 0 && len(t) > matchidx
Bram Moolenaarcaca6462014-05-13 20:15:37 +0200102: $put ='ERROR ' . engine . ': pat: \"' . pat . '\", text: \"' . text . '\", did not match, expected: \"' . t[matchidx] . '\"'
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +0200103: elseif len(l) > 0 && len(t) == matchidx
Bram Moolenaarcaca6462014-05-13 20:15:37 +0200104: $put ='ERROR ' . engine . ': pat: \"' . pat . '\", text: \"' . text . '\", match: \"' . l[0] . '\", expected no match'
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +0200105: elseif len(t) > matchidx && l[0] != t[matchidx]
Bram Moolenaarcaca6462014-05-13 20:15:37 +0200106: $put ='ERROR ' . engine . ': pat: \"' . pat . '\", text: \"' . text . '\", match: \"' . l[0] . '\", expected: \"' . t[matchidx] . '\"'
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +0200107: else
108: $put ='OK ' . engine . ' - ' . pat
109: endif
110: if len(l) > 0
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200111:" check all the nine submatches
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +0200112: for i in range(1, 9)
113: if len(t) <= matchidx + i
114: let e = ''
115: else
116: let e = t[matchidx + i]
117: endif
118: if l[i] != e
Bram Moolenaarcaca6462014-05-13 20:15:37 +0200119: $put ='ERROR ' . engine . ': pat: \"' . pat . '\", text: \"' . text . '\", submatch ' . i . ': \"' . l[i] . '\", expected: \"' . e . '\"'
Bram Moolenaarfdb6dc12013-05-25 23:15:27 +0200120: endif
121: endfor
122: unlet i
123: endif
124: endfor
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200125:endfor
126:unlet t tl e l
Bram Moolenaar901e58c2015-08-11 18:33:48 +0200127:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +0200128:" check that 'ambiwidth' does not change the meaning of \p
129:set regexpengine=1 ambiwidth=single
130:$put ='eng 1 ambi single: ' . match(\"\u00EC\", '\p')
131:set regexpengine=1 ambiwidth=double
132:$put ='eng 1 ambi double: ' . match(\"\u00EC\", '\p')
133:set regexpengine=2 ambiwidth=single
134:$put ='eng 2 ambi single: ' . match(\"\u00EC\", '\p')
135:set regexpengine=2 ambiwidth=double
136:$put ='eng 2 ambi double: ' . match(\"\u00EC\", '\p')
Bram Moolenaar901e58c2015-08-11 18:33:48 +0200137:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200138:/\%#=1^Results/,$wq! test.out
139ENDTEST
140
141Results of test95: