Skip to content

Commit

Permalink
Replace Swig tests with nunjucks
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Parisot committed Jan 17, 2020
1 parent ac9fa60 commit 56dea0b
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 100 deletions.
4 changes: 2 additions & 2 deletions test/scripts/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ describe('generate', () => {
// Add some source files
writeFile(join(hexo.theme_dir, 'source', 'a.txt'), 'a'),
writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'b'),
writeFile(join(hexo.theme_dir, 'source', 'c.swig'), 'c')
writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'c')
]);
await generate();

// Update source file
await Promise.all([
writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'bb'),
writeFile(join(hexo.theme_dir, 'source', 'c.swig'), 'cc')
writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'cc')
]);

// Generate again
Expand Down
8 changes: 4 additions & 4 deletions test/scripts/extend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ describe('Renderer', () => {

r.isRenderableSync('yaml').should.be.false;

r.register('swig', 'html', () => {}, true);
r.register('njk', 'html', () => {}, true);

r.isRenderableSync('swig').should.be.true;
r.isRenderableSync('.swig').should.be.true;
r.isRenderableSync('layout.swig').should.be.true;
r.isRenderableSync('njk').should.be.true;
r.isRenderableSync('.njk').should.be.true;
r.isRenderableSync('layout.njk').should.be.true;
r.isRenderableSync('foo.html').should.be.false;
});

Expand Down
16 changes: 8 additions & 8 deletions test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('partial', () => {
const hexo = new Hexo(pathFn.join(__dirname, 'partial_test'), {silent: true});
const themeDir = pathFn.join(hexo.base_dir, 'themes', 'test');
const viewDir = pathFn.join(themeDir, 'layout') + pathFn.sep;
const viewName = 'article.swig';
const viewName = 'article.njk';

const ctx = {
site: hexo.locals,
Expand All @@ -33,7 +33,7 @@ describe('partial', () => {
fs.writeFile(hexo.config_path, 'theme: test')
]);
await hexo.init();
hexo.theme.setView('widget/tag.swig', 'tag widget');
hexo.theme.setView('widget/tag.njk', 'tag widget');
});

after(() => fs.rmdir(hexo.base_dir));
Expand All @@ -57,28 +57,28 @@ describe('partial', () => {
});

it('locals', () => {
hexo.theme.setView('test.swig', '{{ foo }}');
hexo.theme.setView('test.njk', '{{ foo }}');

partial('test', {foo: 'bar'}).should.eql('bar');
});

it('cache', () => {
hexo.theme.setView('test.swig', '{{ foo }}');
hexo.theme.setView('test.njk', '{{ foo }}');

partial('test', {foo: 'bar'}, {cache: true}).should.eql('bar');
partial('test', {}, {cache: true}).should.eql('bar');
});

it('only', () => {
hexo.theme.setView('test.swig', '{{ foo }}{{ bar }}');
hexo.theme.setView('test.njk', '{{ foo }}{{ bar }}');

partial('test', {bar: 'bar'}, {only: true}).should.eql('bar');
});

it('a partial in another partial', () => {
hexo.theme.setView('partial/a.swig', '{{ partial("b") }}');
hexo.theme.setView('partial/b.swig', '{{ partial("c") }}');
hexo.theme.setView('partial/c.swig', 'c');
hexo.theme.setView('partial/a.njk', '{{ partial("b") }}');
hexo.theme.setView('partial/b.njk', '{{ partial("c") }}');
hexo.theme.setView('partial/c.njk', 'c');

partial('partial/a').should.eql('c');
});
Expand Down
22 changes: 11 additions & 11 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ describe('Hexo', () => {
});

it('_generate() - layout', () => {
hexo.theme.setView('test.swig', [
hexo.theme.setView('test.njk', [
'{{ config.title }}',
'{{ page.foo }}',
'{{ layout }}',
Expand All @@ -381,7 +381,7 @@ describe('Hexo', () => {
});

it('_generate() - layout array', () => {
hexo.theme.setView('baz.swig', 'baz');
hexo.theme.setView('baz.njk', 'baz');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand Down Expand Up @@ -421,7 +421,7 @@ describe('Hexo', () => {
it('_generate() - after_route_render filter', () => {
const hook = sinon.spy(result => result.replace('foo', 'bar'));
hexo.extend.filter.register('after_route_render', hook);
hexo.theme.setView('test.swig', 'foo');
hexo.theme.setView('test.njk', 'foo');
hexo.extend.generator.register('test', () => ({
path: 'test',
layout: 'test'
Expand All @@ -445,7 +445,7 @@ describe('Hexo', () => {
});

it('_generate() - validate locals', () => {
hexo.theme.setView('test.swig', [
hexo.theme.setView('test.njk', [
'{{ path }}',
'{{ url }}',
'{{ view_dir }}'
Expand All @@ -467,7 +467,7 @@ describe('Hexo', () => {
const path = 'bár';
hexo.config.url = 'http://fôo.com';

hexo.theme.setView('test.swig', '{{ url }}');
hexo.theme.setView('test.njk', '{{ url }}');

hexo.extend.generator.register('test', () => ({
path,
Expand All @@ -491,7 +491,7 @@ describe('Hexo', () => {
it('_generate() - reset cache for new route', () => {
let count = 0;

hexo.theme.setView('test.swig', '{{ page.count() }}');
hexo.theme.setView('test.njk', '{{ page.count() }}');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -511,7 +511,7 @@ describe('Hexo', () => {
it('_generate() - cache disabled and use new route', () => {
let count = 0;

hexo.theme.setView('test.swig', '{{ page.count() }}');
hexo.theme.setView('test.njk', '{{ page.count() }}');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -529,7 +529,7 @@ describe('Hexo', () => {
});

it('_generate() - cache disabled & update template', () => {
hexo.theme.setView('test.swig', '0');
hexo.theme.setView('test.njk', '0');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -538,12 +538,12 @@ describe('Hexo', () => {

return hexo._generate({cache: false})
.then(() => checkStream(route.get('test'), '0'))
.then(() => hexo.theme.setView('test.swig', '1'))
.then(() => hexo.theme.setView('test.njk', '1'))
.then(() => checkStream(route.get('test'), '1'));
});

it('_generate() - cache enabled & update template', () => {
hexo.theme.setView('test.swig', '0');
hexo.theme.setView('test.njk', '0');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -552,7 +552,7 @@ describe('Hexo', () => {

return hexo._generate({cache: true})
.then(() => checkStream(route.get('test'), '0'))
.then(() => hexo.theme.setView('test.swig', '1'))
.then(() => hexo.theme.setView('test.njk', '1'))
.then(() => checkStream(route.get('test'), '0')); // should return cached result
});

Expand Down
10 changes: 5 additions & 5 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ describe('Post', () => {
});
});

it('render() - skip render phase if it\'s swig file', () => {
it('render() - skip render phase if it\'s nunjucks file', () => {
const content = [
'{% quote Hello World %}',
'quote content',
Expand All @@ -609,7 +609,7 @@ describe('Post', () => {

return post.render(null, {
content,
engine: 'swig'
engine: 'njk'
}).then(data => {
data.content.trim().should.eql([
'<blockquote><p>quote content</p>\n',
Expand All @@ -618,7 +618,7 @@ describe('Post', () => {
});
});

it('render() - escaping swig blocks with similar names', () => {
it('render() - escaping nunjucks blocks with similar names', () => {
const code = 'alert("Hello world")';
const highlighted = util.highlight(code);

Expand All @@ -643,7 +643,7 @@ describe('Post', () => {
});
});

it('render() - recover escaped swig blocks which is html escaped', () => {
it('render() - recover escaped nunjucks blocks which is html escaped', () => {
const content = '`{% raw %}{{ test }}{% endraw %}`';

return post.render(null, {
Expand All @@ -654,7 +654,7 @@ describe('Post', () => {
});
});

it('render() - recover escaped swig blocks which is html escaped before post_render', () => {
it('render() - recover escaped nunjucks blocks which is html escaped before post_render', () => {
const content = '`{% raw %}{{ test }}{% endraw %}`';

const filter = sinon.spy(result => {
Expand Down
16 changes: 9 additions & 7 deletions test/scripts/hexo/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ describe('Render', () => {
hexo.render.isRenderable('test.html').should.be.true;

// swig
hexo.render.isRenderable('test.swig').should.be.true;
hexo.render.isRenderable('test.swig').should.be.false;
hexo.render.isRenderable('test.njk').should.be.true;

// yaml
hexo.render.isRenderable('test.yml').should.be.true;
Expand All @@ -53,7 +54,8 @@ describe('Render', () => {
hexo.render.isRenderableSync('test.html').should.be.true;

// swig
hexo.render.isRenderableSync('test.swig').should.be.true;
hexo.render.isRenderableSync('test.swig').should.be.false;
hexo.render.isRenderableSync('test.njk').should.be.true;

// yaml
hexo.render.isRenderableSync('test.yml').should.be.true;
Expand All @@ -68,7 +70,7 @@ describe('Render', () => {
hexo.render.getOutput('test.html').should.eql('html');

// swig
hexo.render.getOutput('test.swig').should.eql('html');
hexo.render.getOutput('test.njk').should.eql('html');

// yaml
hexo.render.getOutput('test.yml').should.eql('json');
Expand Down Expand Up @@ -102,7 +104,7 @@ describe('Render', () => {
'<title>{{ title }}</title>',
'<body>{{ content }}</body>'
].join('\n'),
engine: 'swig'
engine: 'njk'
}, {
title: 'Hello world',
content: 'foobar'
Expand Down Expand Up @@ -134,7 +136,7 @@ describe('Render', () => {
it('render() - after_render filter', () => {
const data = {
text: ' <strong>123456</strong> ',
engine: 'swig'
engine: 'njk'
};

const filter = sinon.spy((result, obj) => {
Expand Down Expand Up @@ -226,7 +228,7 @@ describe('Render', () => {
'<title>{{ title }}</title>',
'<body>{{ content }}</body>'
].join('\n'),
engine: 'swig'
engine: 'njk'
}, {
title: 'Hello world',
content: 'foobar'
Expand Down Expand Up @@ -263,7 +265,7 @@ describe('Render', () => {
it('renderSync() - after_render filter', () => {
const data = {
text: ' <strong>123456</strong> ',
engine: 'swig'
engine: 'njk'
};

const filter = sinon.spy((result, obj) => {
Expand Down
22 changes: 11 additions & 11 deletions test/scripts/processors/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('asset', () => {
].join('\n');

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand Down Expand Up @@ -216,7 +216,7 @@ describe('asset', () => {
].join('\n');

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'update',
renderable: true
});
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('asset', () => {

it('page - type: delete', () => {
const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'delete',
renderable: true
});
Expand All @@ -259,7 +259,7 @@ describe('asset', () => {

it('page - use the status of the source file if date not set', () => {
const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand All @@ -282,7 +282,7 @@ describe('asset', () => {

it('page - use the date for updated if use_date_for_updated is set', () => {
const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand Down Expand Up @@ -315,7 +315,7 @@ describe('asset', () => {
].join('\n');

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand All @@ -340,7 +340,7 @@ describe('asset', () => {
].join('\n');

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand All @@ -365,7 +365,7 @@ describe('asset', () => {
].join('\n');

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand Down Expand Up @@ -437,7 +437,7 @@ describe('asset', () => {
].join('\n');

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand All @@ -464,7 +464,7 @@ describe('asset', () => {
].join('\n');

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand Down Expand Up @@ -515,7 +515,7 @@ describe('asset', () => {
].join('\n');

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand Down
Loading

0 comments on commit 56dea0b

Please sign in to comment.