Skip to content

Commit

Permalink
filters: define simple custom filters for tests
Browse files Browse the repository at this point in the history
Now there's no development dependency on minifiers and whatnot.
  • Loading branch information
matheusmoreira committed Aug 27, 2023
1 parent b17ff27 commit 05cbaf1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 195 deletions.
193 changes: 24 additions & 169 deletions packages/pug-filters/test/__snapshots__/filter-aliases.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,70 +19,23 @@ Object {
"line": 3,
"nodes": Array [
Object {
"attrs": Array [],
"block": Object {
"filename": "filter-aliases.test.js",
"line": 3,
"nodes": Array [
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 4,
"type": "Text",
"val": "function myFunc(foo) {",
},
Object {
"column": 1,
"filename": "filter-aliases.test.js",
"line": 5,
"type": "Text",
"val": "
",
},
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 5,
"type": "Text",
"val": " return foo;",
},
Object {
"column": 1,
"filename": "filter-aliases.test.js",
"line": 6,
"type": "Text",
"val": "
",
},
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 6,
"type": "Text",
"val": "}",
},
],
"type": "Block",
},
"column": 9,
"column": 5,
"filename": "filter-aliases.test.js",
"line": 3,
"name": "minify",
"line": 4,
"type": "Text",
"val": "function myFunc(n) {
return n;
}
",
"val": "Filters can be aliased.",
},
],
"type": "Block",
},
"column": 3,
"filename": "filter-aliases.test.js",
"line": 3,
"name": "cdata",
"name": "aliased",
"type": "Text",
"val": "<![CDATA[function myFunc(n){return n}]]>",
"val": "BEGIN ALIASED
Filters can be aliased.
END ALIASED",
},
],
"type": "Block",
Expand All @@ -91,7 +44,7 @@ Object {
"filename": "filter-aliases.test.js",
"isInline": false,
"line": 2,
"name": "script",
"name": "p",
"selfClosing": false,
"type": "Tag",
},
Expand Down Expand Up @@ -119,144 +72,46 @@ Object {
"line": 3,
"nodes": Array [
Object {
"attrs": Array [],
"block": Object {
"filename": "filter-aliases.test.js",
"line": 3,
"nodes": Array [
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 4,
"type": "Text",
"val": "function myFunc(foo) {",
},
Object {
"column": 1,
"filename": "filter-aliases.test.js",
"line": 5,
"type": "Text",
"val": "
",
},
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 5,
"type": "Text",
"val": " return foo;",
},
Object {
"column": 1,
"filename": "filter-aliases.test.js",
"line": 6,
"type": "Text",
"val": "
",
},
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 6,
"type": "Text",
"val": "}",
},
],
"type": "Block",
},
"column": 9,
"column": 5,
"filename": "filter-aliases.test.js",
"line": 3,
"name": "minify",
"line": 4,
"type": "Text",
"val": "function myFunc(n) {
return n;
}
",
"val": "Will be wrapped.",
},
],
"type": "Block",
},
"column": 3,
"filename": "filter-aliases.test.js",
"line": 3,
"name": "cdata",
"name": "check-options",
"type": "Text",
"val": "<![CDATA[function myFunc(n) {
return n;
}]]>",
"val": "CHECKED
Will be wrapped.
END CHECKED",
},
Object {
"attrs": Array [],
"block": Object {
"filename": "filter-aliases.test.js",
"line": 7,
"line": 5,
"nodes": Array [
Object {
"attrs": Array [],
"block": Object {
"filename": "filter-aliases.test.js",
"line": 7,
"nodes": Array [
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 8,
"type": "Text",
"val": "function myFunc(foo) {",
},
Object {
"column": 1,
"filename": "filter-aliases.test.js",
"line": 9,
"type": "Text",
"val": "
",
},
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 9,
"type": "Text",
"val": " return foo;",
},
Object {
"column": 1,
"filename": "filter-aliases.test.js",
"line": 10,
"type": "Text",
"val": "
",
},
Object {
"column": 5,
"filename": "filter-aliases.test.js",
"line": 10,
"type": "Text",
"val": "}",
},
],
"type": "Block",
},
"column": 9,
"column": 5,
"filename": "filter-aliases.test.js",
"line": 7,
"name": "uglify-js",
"line": 6,
"type": "Text",
"val": "function myFunc(n) {
return n;
}
",
"val": "Will not be wrapped.",
},
],
"type": "Block",
},
"column": 3,
"filename": "filter-aliases.test.js",
"line": 7,
"name": "cdata",
"line": 5,
"name": "aliased",
"type": "Text",
"val": "<![CDATA[function myFunc(n){return n}]]>",
"val": "Will not be wrapped.",
},
],
"type": "Block",
Expand All @@ -277,8 +132,8 @@ Object {
exports[`we do not support chains of aliases 1`] = `
Object {
"code": "PUGNEUM:FILTER_ALIAS_CHAIN",
"message": "filter-aliases.test.js:3:9
"message": "filter-aliases.test.js:3:3
The filter \\"minify-js\\" is an alias for \\"minify\\", which is an alias for \\"uglify-js\\". pugneum does not support chains of filter aliases.",
The filter \\"aliased-again\\" is an alias for \\"aliased\\", which is an alias for \\"alias\\". pugneum does not support chains of filter aliases.",
}
`;
52 changes: 26 additions & 26 deletions packages/pug-filters/test/filter-aliases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ const lex = require('pugneum-lexer');
const parse = require('pugneum-parser');
const handleFilters = require('../').handleFilters;

const customFilters = {};
const filename = require('path').basename(__filename);

const customFilters = {
alias: function(str, options) {
return 'BEGIN ALIASED\n' + str + '\nEND ALIASED';
},
'check-options': function(str, options) {
return options.wrap? 'CHECKED\n' + str + '\nEND CHECKED' : str;
}
};

test('filters can be aliased', () => {
const source = `
script
:cdata:minify
function myFunc(foo) {
return foo;
}
`;
p
:aliased
Filters can be aliased.
`;

const ast = parse(lex(source, {filename}), {
filename,
Expand All @@ -21,7 +27,7 @@ script

const options = {};
const aliases = {
minify: 'uglify-js',
aliased: 'alias',
};

const output = handleFilters(ast, customFilters, options, aliases);
Expand All @@ -30,11 +36,9 @@ script

test('we do not support chains of aliases', () => {
const source = `
script
:cdata:minify-js
function myFunc(foo) {
return foo;
}
p
:aliased-again
Alias chains are not supported.
`;

const ast = parse(lex(source, {filename}), {
Expand All @@ -44,8 +48,8 @@ script

const options = {};
const aliases = {
'minify-js': 'minify',
minify: 'uglify-js',
aliased: 'alias',
'aliased-again': 'aliased',
};

try {
Expand All @@ -63,26 +67,22 @@ script
test('options are applied before aliases', () => {
const source = `
script
:cdata:minify
function myFunc(foo) {
return foo;
}
:cdata:uglify-js
function myFunc(foo) {
return foo;
}
`;
:check-options
Will be wrapped.
:aliased
Will not be wrapped.
`;

const ast = parse(lex(source, {filename}), {
filename,
src: source,
});

const options = {
minify: {output: {beautify: true}},
'check-options': {wrap: true}
};
const aliases = {
minify: 'uglify-js',
aliased: 'check-options',
};

const output = handleFilters(ast, customFilters, options, aliases);
Expand Down

0 comments on commit 05cbaf1

Please sign in to comment.