Skip to content

Commit

Permalink
Add parser tests (#4)
Browse files Browse the repository at this point in the history
* test: add test for parsing multiple stylesheets
* test: add test for parsing css with expressions
  • Loading branch information
43081j committed Nov 16, 2021
1 parent cc4ba54 commit 12b2bd2
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/test/parse_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Root, Rule, Declaration} from 'postcss';
import {Root, Rule, Declaration, Comment} from 'postcss';
import {assert} from 'chai';
import {createTestAst} from './util.js';

Expand Down Expand Up @@ -59,4 +59,51 @@ describe('parse', () => {
assert.equal(rule.type, 'rule');
assert.equal(colour.type, 'decl');
});

it('should parse multiple stylesheets', () => {
const {source, ast} = createTestAst(`
css\`
.foo { color: hotpink; }
\`;
css\`.bar: { background: lime; }\`;
`);
assert.equal(ast.nodes.length, 2);
const root1 = ast.nodes[0] as Root;
const root2 = ast.nodes[1] as Root;

assert.equal(root1.type, 'root');
assert.equal(root1.raws.codeBefore, '\n css`');
assert.equal(root1.raws.codeAfter, undefined);
assert.equal(root1.parent, ast);
assert.equal(root2.type, 'root');
assert.equal(root2.raws.codeBefore, '`;\n\n css`');
assert.equal(root2.raws.codeAfter, '`;\n ');
assert.equal(root2.parent, ast);

assert.deepEqual(ast.source!.start, {
line: 1,
column: 1,
offset: 0
});
assert.equal(ast.source!.input.css, source);
});

it('should parse CSS containing an expression', () => {
const {source, ast} = createTestAst(`
css\`
.foo { $\{expr}color: hotpink; }
\`;
`);
const root = ast.nodes[0] as Root;
const rule = root.nodes[0] as Rule;
const placeholder = rule.nodes[0] as Comment;
const colour = rule.nodes[1] as Declaration;
assert.equal(ast.type, 'document');
assert.equal(root.type, 'root');
assert.equal(rule.type, 'rule');
assert.equal(placeholder.type, 'comment');
assert.equal(colour.type, 'decl');
assert.equal(ast.source!.input.css, source);
});
});

0 comments on commit 12b2bd2

Please sign in to comment.