-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat-with-types.test.ts
More file actions
156 lines (140 loc) · 4.46 KB
/
Copy pathformat-with-types.test.ts
File metadata and controls
156 lines (140 loc) · 4.46 KB
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
import { describe, test, expect } from 'vitest'
import {
format_with_types,
LINE_TYPE_SELECTOR,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
LINE_TYPE_ATRULE,
LINE_TYPE_COMMENT,
LINE_TYPE_EMPTY,
} from '../src/lib/index.js'
describe('format_with_types', () => {
test('simple rule', () => {
let { css, types } = format_with_types('a { color: red; }')
expect(css).toBe('a {\n\tcolor: red;\n}')
expect(types).toEqual([LINE_TYPE_SELECTOR, LINE_TYPE_DECLARATION, LINE_TYPE_BRACKET])
})
test('multiple declarations', () => {
let { css, types } = format_with_types('a { color: red; background: blue; }')
expect(types).toEqual([
LINE_TYPE_SELECTOR,
LINE_TYPE_DECLARATION,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
])
})
test('multi-line selector list', () => {
let { css, types } = format_with_types('a, b { color: red; }')
expect(css).toBe('a,\nb {\n\tcolor: red;\n}')
expect(types).toEqual([
LINE_TYPE_SELECTOR,
LINE_TYPE_SELECTOR,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
])
})
test('atrule with block', () => {
let { css, types } = format_with_types('@media all { a { color: red } }')
expect(css).toBe('@media all {\n\ta {\n\t\tcolor: red;\n\t}\n}')
expect(types).toEqual([
LINE_TYPE_ATRULE,
LINE_TYPE_SELECTOR,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
LINE_TYPE_BRACKET,
])
})
test('atrule without block (@charset)', () => {
let { css, types } = format_with_types('@charset "utf-8";')
expect(css).toBe('@charset "utf-8";')
expect(types).toEqual([LINE_TYPE_ATRULE])
})
test('atrule without block (@import)', () => {
let { css, types } = format_with_types('@import "foo.css";')
expect(css).toBe('@import "foo.css";')
expect(types).toEqual([LINE_TYPE_ATRULE])
})
test('empty line between rules', () => {
let { css, types } = format_with_types('a {} b {}')
expect(css).toBe('a {}\n\nb {}')
expect(types).toEqual([LINE_TYPE_BRACKET, LINE_TYPE_EMPTY, LINE_TYPE_BRACKET])
})
test('single-line comment before rule', () => {
let { css, types } = format_with_types('/* comment */ a { color: red; }')
expect(css).toBe('/* comment */\na {\n\tcolor: red;\n}')
expect(types).toEqual([
LINE_TYPE_COMMENT,
LINE_TYPE_SELECTOR,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
])
})
test('single-line comment inside rule', () => {
let { css, types } = format_with_types('a { /* comment */ color: red; }')
expect(css).toBe('a {\n\t/* comment */\n\tcolor: red;\n}')
expect(types).toEqual([
LINE_TYPE_SELECTOR,
LINE_TYPE_COMMENT,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
])
})
test('multiline comment before rule', () => {
let { css, types } = format_with_types('/* line 1\n line 2 */ a { color: red; }')
expect(css).toBe('/* line 1\n line 2 */\na {\n\tcolor: red;\n}')
expect(types).toEqual([
LINE_TYPE_COMMENT,
LINE_TYPE_COMMENT,
LINE_TYPE_SELECTOR,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
])
})
test('multiline comment inside rule', () => {
let { css, types } = format_with_types(
'a {\n\t/* line 1\n\t line 2 */\n\tcolor: red;\n}',
)
expect(css).toBe('a {\n\t/* line 1\n\t line 2 */\n\tcolor: red;\n}')
expect(types).toEqual([
LINE_TYPE_SELECTOR,
LINE_TYPE_COMMENT,
LINE_TYPE_COMMENT,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
])
})
test('multiline comment spanning three lines', () => {
let { css, types } = format_with_types('/* a\n * b\n * c */ a { color: red; }')
expect(css).toBe('/* a\n * b\n * c */\na {\n\tcolor: red;\n}')
expect(types).toEqual([
LINE_TYPE_COMMENT,
LINE_TYPE_COMMENT,
LINE_TYPE_COMMENT,
LINE_TYPE_SELECTOR,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
])
})
test('comment containing a semicolon is not a declaration', () => {
let { css, types } = format_with_types('a { /* color: red; */ color: blue; }')
expect(types).toEqual([
LINE_TYPE_SELECTOR,
LINE_TYPE_COMMENT,
LINE_TYPE_DECLARATION,
LINE_TYPE_BRACKET,
])
})
test('comment containing a brace is not a bracket', () => {
let { css, types } = format_with_types('/* } */ a { color: red; }')
expect(types[0]).toBe(LINE_TYPE_COMMENT)
})
test('types array length matches number of lines', () => {
let input = '@media all { a, b { color: red; background: blue; } }'
let { css, types } = format_with_types(input)
expect(types).toHaveLength(css.split('\n').length)
})
test('tab_size option is forwarded', () => {
let { css } = format_with_types('a { color: red; }', { tab_size: 2 })
expect(css).toBe('a {\n color: red;\n}')
})
})