|
1 | 1 | import { expect, test } from 'vitest'
|
2 |
| -import { ClassRegexEntry, customClassesIn } from './classes' |
| 2 | +import { ClassMatch, ClassRegexFilter, customClassesIn } from './classes' |
3 | 3 |
|
4 | 4 | interface TestRecord {
|
5 | 5 | name: string,
|
6 |
| - input: string, |
7 |
| - cursor: number, |
8 |
| - regexes: ClassRegexEntry[], |
9 |
| - expected: { classList: string } | null |
| 6 | + text: string, |
| 7 | + cursor: number | null, |
| 8 | + filters: ClassRegexFilter[], |
| 9 | + expected: ClassMatch[] |
10 | 10 | }
|
11 | 11 |
|
12 | 12 | let table: TestRecord[] = [
|
13 | 13 | {
|
14 | 14 | name: 'empty',
|
15 |
| - input: 'test ', |
| 15 | + text: 'test ', |
16 | 16 | cursor: 0,
|
17 |
| - regexes: [], |
18 |
| - expected: null |
| 17 | + filters: [], |
| 18 | + expected: [], |
19 | 19 | },
|
20 | 20 |
|
21 | 21 | // Container regex only
|
22 | 22 | {
|
23 | 23 | name: 'simple (single, matches: yes)',
|
24 |
| - input: 'test ""', |
| 24 | + text: 'test ""', |
25 | 25 | cursor: 5,
|
26 |
| - regexes: [['test (\\S*)']], |
27 |
| - expected: { classList: '' } |
| 26 | + filters: [['test (\\S*)']], |
| 27 | + expected: [{ classList: '', range: [5, 7] }], |
28 | 28 | },
|
29 | 29 |
|
30 | 30 | {
|
31 | 31 | name: 'simple (single, matches: no)',
|
32 |
| - input: 'tron ""', |
| 32 | + text: 'tron ""', |
33 | 33 | cursor: 5,
|
34 |
| - regexes: [['test (\\S*)']], |
35 |
| - expected: null |
| 34 | + filters: [['test (\\S*)']], |
| 35 | + expected: [] |
36 | 36 | },
|
37 | 37 |
|
38 | 38 | {
|
39 | 39 | name: 'simple (multiple, matches: yes)',
|
40 |
| - input: 'tron ""', |
| 40 | + text: 'tron ""', |
41 | 41 | cursor: 5,
|
42 |
| - regexes: [['test (\\S*)'], ['tron (\\S*)']], |
43 |
| - expected: { classList: '' } |
| 42 | + filters: [['test (\\S*)'], ['tron (\\S*)']], |
| 43 | + expected: [{ classList: '', range: [5, 7] }], |
44 | 44 | },
|
45 | 45 |
|
46 | 46 | {
|
47 | 47 | name: 'simple (multiple, matches: no)',
|
48 |
| - input: 'nope ""', |
| 48 | + text: 'nope ""', |
49 | 49 | cursor: 5,
|
50 |
| - regexes: [['test (\\S*)'], ['tron (\\S*)']], |
51 |
| - expected: null |
| 50 | + filters: [['test (\\S*)'], ['tron (\\S*)']], |
| 51 | + expected: [] |
52 | 52 | },
|
53 | 53 |
|
54 | 54 | // Container + class regex
|
55 | 55 | {
|
56 | 56 | name: 'nested (single, matches: yes)',
|
57 |
| - input: 'test ""', |
| 57 | + text: 'test ""', |
58 | 58 | cursor: 6,
|
59 |
| - regexes: [['test (\\S*)', '"([^"]*)"']], |
60 |
| - expected: { classList: '' } |
| 59 | + filters: [['test (\\S*)', '"([^"]*)"']], |
| 60 | + expected: [{ classList: '', range: [6, 6] }], |
61 | 61 | },
|
62 | 62 |
|
63 | 63 | {
|
64 | 64 | name: 'nested (single, matches: no)',
|
65 |
| - input: 'tron ""', |
| 65 | + text: 'tron ""', |
66 | 66 | cursor: 6,
|
67 |
| - regexes: [['test (\\S*)', '"([^"]*)"']], |
68 |
| - expected: null |
| 67 | + filters: [['test (\\S*)', '"([^"]*)"']], |
| 68 | + expected: [] |
69 | 69 | },
|
70 | 70 |
|
71 | 71 | {
|
72 | 72 | name: 'nested (multiple, matches: yes)',
|
73 |
| - input: 'tron ""', |
| 73 | + text: 'tron ""', |
74 | 74 | cursor: 6,
|
75 |
| - regexes: [['test (\\S*)', '"([^"]*)"'], ['tron (\\S*)', '"([^"]*)"']], |
76 |
| - expected: { classList: '' } |
| 75 | + filters: [['test (\\S*)', '"([^"]*)"'], ['tron (\\S*)', '"([^"]*)"']], |
| 76 | + expected: [{ classList: '', range: [6, 6] }], |
77 | 77 | },
|
78 | 78 |
|
79 | 79 | {
|
80 | 80 | name: 'nested (multiple, matches: no)',
|
81 |
| - input: 'nope ""', |
| 81 | + text: 'nope ""', |
82 | 82 | cursor: 6,
|
83 |
| - regexes: [['test (\\S*)', '"([^"]*)"'], ['tron (\\S*)', '"([^"]*)"']], |
84 |
| - expected: null |
| 83 | + filters: [['test (\\S*)', '"([^"]*)"'], ['tron (\\S*)', '"([^"]*)"']], |
| 84 | + expected: [] |
85 | 85 | },
|
86 | 86 |
|
87 | 87 | // Cursor position validation
|
88 | 88 | {
|
89 | 89 | name: 'cursor, container: inside #1',
|
90 |
| - input: `<div class="text-" /> <div class="bg-" />`, |
| 90 | + text: `<div class="text-" /> <div class="bg-" />`, |
91 | 91 | cursor: 17,
|
92 |
| - regexes: [['class="([^"]*)"']], |
93 |
| - expected: { classList: 'text-' } |
| 92 | + filters: [['class="([^"]*)"']], |
| 93 | + expected: [{ classList: 'text-', range: [12, 17] }], |
94 | 94 | },
|
95 | 95 |
|
96 | 96 | {
|
97 | 97 | name: 'cursor, container: inside #2',
|
98 |
| - input: `<div class="text-" /> <div class="bg-" />`, |
| 98 | + text: `<div class="text-" /> <div class="bg-" />`, |
99 | 99 | cursor: 37,
|
100 |
| - regexes: [['class="([^"]*)"']], |
101 |
| - expected: { classList: 'bg-' } |
| 100 | + filters: [['class="([^"]*)"']], |
| 101 | + expected: [{ classList: 'bg-', range: [34, 37] }], |
102 | 102 | },
|
103 | 103 |
|
104 | 104 | {
|
105 | 105 | name: 'cursor, container: outside',
|
106 |
| - input: `<div class="text-" /> <div class="bg-" />`, |
| 106 | + text: `<div class="text-" /> <div class="bg-" />`, |
107 | 107 | cursor: 11,
|
108 |
| - regexes: [['class="([^"]*)"']], |
109 |
| - expected: null |
| 108 | + filters: [['class="([^"]*)"']], |
| 109 | + expected: [] |
110 | 110 | },
|
111 | 111 |
|
112 | 112 | {
|
113 | 113 | name: 'cursor, container: inside #1, class: inside #1',
|
114 |
| - input: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
| 114 | + text: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
115 | 115 | cursor: 23,
|
116 |
| - regexes: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
117 |
| - expected: { classList: 'text-' } |
| 116 | + filters: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
| 117 | + expected: [{ classList: 'text-', range: [18, 23] }], |
118 | 118 | },
|
119 | 119 |
|
120 | 120 | {
|
121 | 121 | name: 'cursor, container: inside #1, class: inside #2',
|
122 |
| - input: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
| 122 | + text: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
123 | 123 | cursor: 38,
|
124 |
| - regexes: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
125 |
| - expected: { classList: 'decoration-' } |
| 124 | + filters: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
| 125 | + expected: [{ classList: 'decoration-', range: [27, 38] }], |
126 | 126 | },
|
127 | 127 |
|
128 | 128 | {
|
129 | 129 | name: 'cursor, container: inside #2, class: inside #1',
|
130 |
| - input: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
| 130 | + text: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
131 | 131 | cursor: 66,
|
132 |
| - regexes: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
133 |
| - expected: { classList: 'bg-' } |
| 132 | + filters: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
| 133 | + expected: [{ classList: 'bg-', range: [63, 66] }], |
134 | 134 | },
|
135 | 135 |
|
136 | 136 | {
|
137 | 137 | name: 'cursor, container: inside #1, class: outside',
|
138 |
| - input: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
| 138 | + text: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
139 | 139 | cursor: 17,
|
140 |
| - regexes: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
141 |
| - expected: null, |
| 140 | + filters: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
| 141 | + expected: [], |
142 | 142 | },
|
143 | 143 |
|
144 | 144 | {
|
145 | 145 | name: 'cursor, container: inside #2, class: outside',
|
146 |
| - input: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
| 146 | + text: `<div class={clsx("text-", "decoration-")} /> <div class={clsx("bg-")} />`, |
147 | 147 | cursor: 62,
|
148 |
| - regexes: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
149 |
| - expected: null, |
| 148 | + filters: [['clsx\\(([^)]*)\\)', '"([^"]*)"']], |
| 149 | + expected: [], |
| 150 | + }, |
| 151 | + |
| 152 | + // No cursor = multiple results |
| 153 | + { |
| 154 | + name: 'cursor, container: inside #1', |
| 155 | + text: `<div class="text-" /> <div class="bg-" />`, |
| 156 | + cursor: null, |
| 157 | + filters: [['class="([^"]*)"']], |
| 158 | + expected: [{ classList: 'text-', range: [12, 17] }, { classList: 'bg-', range: [34, 37] }], |
150 | 159 | },
|
151 | 160 |
|
152 | 161 | // Edge cases
|
153 | 162 | {
|
154 | 163 | name: 'regex matches empty string',
|
155 |
| - input: `let _ = ""`, |
| 164 | + text: `let _ = ""`, |
156 | 165 | cursor: 9,
|
157 |
| - regexes: [['(?<=")(\\w*)(?=")']], |
158 |
| - expected: { classList: '' }, |
| 166 | + filters: [['(?<=")(\\w*)(?=")']], |
| 167 | + expected: [{ classList: '', range: [9, 9] }], |
159 | 168 | },
|
160 | 169 | ]
|
161 | 170 |
|
162 |
| -test.each(table)('customClassesIn: $name', ({ input, cursor, regexes, expected }) => { |
163 |
| - expect(customClassesIn(input, cursor, regexes)).toStrictEqual(expected) |
| 171 | +test.each(table)('customClassesIn: $name', ({ text, cursor, filters, expected }) => { |
| 172 | + expect(Array.from(customClassesIn({ text, filters, cursor }))).toStrictEqual(expected) |
164 | 173 | })
|
0 commit comments