Skip to content

Commit 2a2ecab

Browse files
authored
Adds CSS test file (#1)
- Adds test file - Uses fs to read test file - Updates tests for line length - Updates index for line length by breaking repeated regex into variable
1 parent 0858442 commit 2a2ecab

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module.exports = postcss.plugin('postcss-comment-annotation', (opts) => {
44
opts = opts || {};
55

66
const prefix = opts.prefix || '@';
7-
const matchComment = `(\\${prefix}(?:\\r?\\n|.)+?)(?=\\${prefix}|$)`;
8-
const matchKeyVal =
9-
`\\${prefix}(\\w*)\\s((?:\\r?\\n|.)*?)(?=\\r?\\n\\r?\\n|$)`;
7+
const newLine = '(?:\\r?\\n|.)*?)';
8+
const matchComment = `(\\${prefix}${newLine}(?=\\${prefix}|$)`;
9+
const matchKeyVal = `\\${prefix}(\\w*)\\s(${newLine}(?=\\r?\\n\\r?\\n|$)`;
1010
const matchBool = `\\${prefix}(\\w+)\\s*(?=\\n|$)`;
1111

1212
return (root, result) => {

index.test.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Foo bar */
2+
3+
/*
4+
@foo Bar
5+
6+
@baz
7+
8+
@qux
9+
Foo bar
10+
baz qux
11+
*/
12+
13+
.foo {
14+
color: pink; /* Baz qux */
15+
}
16+
17+
/* Foo bar */
18+
19+
/*
20+
#foo Bar
21+
#baz Qux
22+
*/

index.test.js

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,57 @@
11
const postcss = require('postcss');
2+
const fs = require('fs');
23
const plugin = require('./');
3-
4-
const css = `
5-
/*
6-
@define Link
7-
8-
@boolean
9-
10-
@description
11-
Pink anchors
12-
are super hip
13-
*/
14-
a { color: blue; }
15-
/* #define Empty comment block #boolean*/
16-
/* @define Empty comment block @boolean*/
17-
`;
18-
4+
const css = fs.readFileSync('index.test.css', 'utf-8');
195
const opts = {
206
prefix: '#'
217
};
228

239
it('should not modify any output CSS', () => {
24-
return postcss([ plugin ]).process(css)
10+
return postcss([plugin]).process(css)
2511
.then(result => {
2612
expect(result.css).toEqual(css);
2713
});
2814
});
2915

30-
it('should return array of objects for each annotated comment block', () => {
31-
return postcss([ plugin ]).process(css)
16+
it('should only push comments that have annotations', () => {
17+
return postcss([plugin]).process(css)
3218
.then(result => {
33-
expect(result.commentAnnotations).toHaveLength(2);
19+
expect(result.commentAnnotations).toHaveLength(1);
3420
});
3521
});
3622

37-
it('should create annotation property for default prefixed keys', () => {
38-
return postcss([ plugin ]).process(css)
23+
it('should create property for default prefixed keys', () => {
24+
return postcss([plugin]).process(css)
3925
.then(result => {
40-
expect(result.commentAnnotations[0]).toHaveProperty('define');
26+
expect(result.commentAnnotations[0]).toHaveProperty('foo');
4127
});
4228
});
4329

44-
it('should use option prefix to create annotation property', () => {
30+
it('should use option prefix to create properties', () => {
4531
return postcss([plugin(opts)]).process(css)
4632
.then(result => {
47-
expect(result.commentAnnotations[0]).toHaveProperty('define');
33+
expect(result.commentAnnotations[0]).toHaveProperty('foo');
4834
});
4935
});
5036

5137
it('should create a value for single line annotations', () => {
5238
return postcss([plugin]).process(css)
5339
.then(result => {
54-
expect(result.commentAnnotations[0])
55-
.toHaveProperty('define', 'Link');
40+
expect(result.commentAnnotations[0]).toHaveProperty('foo', 'Bar');
5641
});
5742
});
5843

5944
it('should create a value for multi line annotations', () => {
6045
return postcss([plugin]).process(css)
6146
.then(result => {
6247
expect(result.commentAnnotations[0])
63-
.toHaveProperty('description', 'Pink anchors\nare super hip');
48+
.toHaveProperty('qux', 'Foo bar\nbaz qux');
6449
});
6550
});
6651

6752
it('should take key without value as boolean', () => {
6853
return postcss([plugin]).process(css)
6954
.then(result => {
70-
expect(result.commentAnnotations[0])
71-
.toHaveProperty('boolean', true);
55+
expect(result.commentAnnotations[0]).toHaveProperty('baz', true);
7256
});
7357
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"author": "Matthew Ferry <[email protected]>",
1313
"license": "MIT",
1414
"repository": "matthewferry/postcss-comment-annotation",
15+
"files": [
16+
"index.js"
17+
],
1518
"bugs": {
1619
"url": "https://github.com/matthewferry/postcss-comment-annotation/issues"
1720
},

0 commit comments

Comments
 (0)