-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
warning.test.ts
188 lines (166 loc) · 4.88 KB
/
warning.test.ts
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
import { resolve } from 'path'
import { test } from 'uvu'
import { is, type } from 'uvu/assert'
import { decl, parse, Warning } from '../lib/postcss.js'
test('outputs simple warning', () => {
let warning = new Warning('text')
is(warning.toString(), 'text')
})
test('outputs warning with plugin', () => {
let warning = new Warning('text', { plugin: 'plugin' })
is(warning.toString(), 'plugin: text')
})
test('outputs warning with position', () => {
let root = parse('a{}')
let warning = new Warning('text', { node: root.first })
is(warning.toString(), '<css input>:1:1: text')
})
test('outputs warning with plugin and node', () => {
let file = resolve('a.css')
let root = parse('a{}', { from: file })
let warning = new Warning('text', {
node: root.first,
plugin: 'plugin'
})
is(warning.toString(), `plugin: ${file}:1:1: text`)
})
test('outputs warning with index', () => {
let file = resolve('a.css')
let root = parse('@rule param {}', { from: file })
let warning = new Warning('text', {
index: 7,
node: root.first,
plugin: 'plugin'
})
is(warning.toString(), `plugin: ${file}:1:8: text`)
})
test('outputs warning with word', () => {
let file = resolve('a.css')
let root = parse('@rule param {}', { from: file })
let warning = new Warning('text', {
node: root.first,
plugin: 'plugin',
word: 'am'
})
is(warning.toString(), `plugin: ${file}:1:10: text`)
})
test('generates warning without source', () => {
let node = decl({ prop: 'color', value: 'black' })
let warning = new Warning('text', { node })
is(warning.toString(), '<css input>: text')
})
test('has line and column is undefined by default', () => {
let warning = new Warning('text')
type(warning.line, 'undefined')
type(warning.column, 'undefined')
type(warning.endLine, 'undefined')
type(warning.endColumn, 'undefined')
})
test('gets range from node', () => {
let root = parse('a{}')
let warning = new Warning('text', { node: root.first })
is(warning.line, 1)
is(warning.column, 1)
is(warning.endLine, 1)
is(warning.endColumn, 4)
})
test('gets range from node without end', () => {
let root = parse('a{}')
root.first!.source!.end = undefined
let warning = new Warning('text', { node: root.first })
is(warning.line, 1)
is(warning.column, 1)
is(warning.endLine, 1)
is(warning.endColumn, 2)
})
test('gets range from node with endIndex 3', () => {
let root = parse('a{}')
let warning = new Warning('text', { node: root.first, index: 0, endIndex: 3 })
is(warning.line, 1)
is(warning.column, 1)
is(warning.endLine, 1)
is(warning.endColumn, 4)
})
test('gets range from node with endIndex 0', () => {
let root = parse('a{}')
let warning = new Warning('text', { node: root.first, index: 0, endIndex: 0 })
is(warning.line, 1)
is(warning.column, 1)
is(warning.endLine, 1)
is(warning.endColumn, 2)
})
test('gets range from word', () => {
let root = parse('a b{}')
let warning = new Warning('text', { node: root.first, word: 'b' })
is(warning.line, 1)
is(warning.column, 3)
is(warning.endLine, 1)
is(warning.endColumn, 4)
})
test('gets range from index', () => {
let root = parse('a b{}')
let warning = new Warning('text', { index: 2, node: root.first })
is(warning.line, 1)
is(warning.column, 3)
is(warning.endLine, 1)
is(warning.endColumn, 4)
})
test('gets range from index and endIndex', () => {
let root = parse('a b{}')
let warning = new Warning('text', { endIndex: 3, index: 2, node: root.first })
is(warning.line, 1)
is(warning.column, 3)
is(warning.endLine, 1)
is(warning.endColumn, 4)
})
test('gets range from start', () => {
let root = parse('a b{}')
let warning = new Warning('text', {
node: root.first,
start: { column: 3, line: 1 }
})
is(warning.line, 1)
is(warning.column, 3)
is(warning.endLine, 1)
is(warning.endColumn, 6)
})
test('gets range from end', () => {
let root = parse('a b{}')
let warning = new Warning('text', {
end: { column: 3, line: 1 },
node: root.first
})
is(warning.line, 1)
is(warning.column, 1)
is(warning.endLine, 1)
is(warning.endColumn, 3)
})
test('gets range from start and end', () => {
let root = parse('a b{}')
let warning = new Warning('text', {
end: { column: 4, line: 1 },
node: root.first,
start: { column: 3, line: 1 }
})
is(warning.line, 1)
is(warning.column, 3)
is(warning.endLine, 1)
is(warning.endColumn, 4)
})
test('always returns exclusive ends', () => {
let root = parse('a b{}')
let warning = new Warning('text', { endIndex: 1, index: 1, node: root.first })
is(warning.line, 1)
is(warning.column, 2)
is(warning.endLine, 1)
is(warning.endColumn, 3)
})
test('always returns valid ranges', () => {
let root = parse('a b{}')
let warning = new Warning('text', { endIndex: 1, index: 2, node: root.first })
is(warning.line, 1)
is(warning.column, 3)
is(warning.endLine, 1)
is(warning.endColumn, 4)
})
test.run()