Debian Codegraph @

test/parse.test.js

raw
1 let { deepStrictEqual, equal, throws } = require('node:assert')
2 let { readdirSync, readFileSync } = require('node:fs')
3 let { extname, join } = require('node:path')
4 let { test } = require('node:test')
5 let { jsonify } = require('postcss-parser-tests')
6
7 let parse = require('../parse')
8
9 test('detects indent', () => {
10 let root = parse('@one\n @two\n @three')
11 equal(root.raws.indent, ' ')
12 })
13
14 test('throws on first indent', () => {
15 throws(() => {
16 parse(' @charset "UTF-8"')
17 }, /<css input>:1:3: First line should not have indent/)
18 })
19
20 test('throws on too big indent', () => {
21 throws(() => {
22 parse('@supports\n @media\n // test')
23 }, /<css input>:3:7: Expected 4 indent, but get 6/)
24 })
25
26 test('throws on wrong indent step', () => {
27 throws(() => {
28 parse('@supports\n @media\n @media')
29 }, /<css input>:3:2: Expected 0 or 2 indent, but get 1/)
30 })
31
32 test('throws on decl without property', () => {
33 throws(() => {
34 parse(': black')
35 }, /<css input>:1:1: Declaration without name/)
36 })
37
38 test('throws on space between property', () => {
39 throws(() => {
40 parse('one two: black')
41 }, /<css input>:1:5: Unexpected separator in property/)
42 })
43
44 test('throws on semicolon in declaration', () => {
45 throws(() => {
46 parse('a\n color: black;')
47 }, /<css input>:2:15: Unnecessary semicolon/)
48 })
49
50 test('throws on semicolon in at-rule', () => {
51 throws(() => {
52 parse('@charset "UTF-8";')
53 }, /<css input>:1:17: Unnecessary semicolon/)
54 })
55
56 test('throws on curly in rule', () => {
57 throws(() => {
58 parse('a {\n color: black')
59 }, /<css input>:1:3: Unnecessary curly bracket/)
60 })
61
62 test('throws on curly in at-rule', () => {
63 throws(() => {
64 parse('@media (screen) {\n color: black')
65 }, /<css input>:1:17: Unnecessary curly bracket/)
66 })
67
68 test('keeps trailing spaces', () => {
69 let root = parse('@media s \n a\n b \n a : \n b \n// a \n \n')
70 equal(root.raws.after, '\n \n')
71 equal(root.first.raws.sssBetween, ' ')
72 equal(root.first.raws.afterName, ' ')
73 equal(root.first.first.raws.sssBetween, ' ')
74 equal(root.first.first.first.raws.between, ' : \n ')
75 equal(root.first.first.first.raws.value.raw, 'b ')
76 equal(root.last.raws.left, ' ')
77 equal(root.last.raws.inlineRight, ' ')
78 })
79
80 test('supports files without last new line', () => {
81 equal(parse('color: black').raws.after, '')
82 })
83
84 test('keeps last new line', () => {
85 equal(parse('color: black\n').raws.after, '\n')
86 })
87
88 test('generates correct source maps on trailing spaces', () => {
89 equal(parse('a: 1 ').first.source.end.line, 1)
90 })
91
92 test('sets end position for root', () => {
93 deepStrictEqual(parse('a\n b: 1\n').source.end, {
94 column: 1,
95 line: 3,
96 offset: 9
97 })
98 })
99
100 let tests = readdirSync(join(__dirname, 'cases')).filter(
101 i => extname(i) === '.sss'
102 )
103
104 function read(file) {
105 return readFileSync(join(__dirname, 'cases', file)).toString()
106 }
107
108 for (let name of tests) {
109 test('parses ' + name, () => {
110 let sss = read(name)
111 let css = read(name.replace(/\.sss/, '.css'))
112 let json = read(name.replace(/\.sss/, '.json'))
113 let root = parse(sss, { from: name })
114 let result = root.toResult({
115 map: {
116 annotation: false,
117 inline: false
118 }
119 })
120 equal(result.css, css)
121 deepStrictEqual(jsonify(root), JSON.parse(json.trim()))
122 })
123 }