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 Apr 26, 2020
1 parent 3d4f4ee commit 2cdecbf
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 @@ -90,11 +90,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 @@ -9,7 +9,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 @@ -32,7 +32,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 @@ -52,28 +52,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 @@ -351,7 +351,7 @@ describe('Hexo', () => {
});

it('_generate() - layout', () => {
hexo.theme.setView('test.swig', [
hexo.theme.setView('test.njk', [
'{{ config.title }}',
'{{ page.foo }}',
'{{ layout }}',
Expand All @@ -378,7 +378,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 @@ -418,7 +418,7 @@ describe('Hexo', () => {
it('_generate() - after_route_render filter', () => {
const hook = 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 @@ -442,7 +442,7 @@ describe('Hexo', () => {
});

it('_generate() - validate locals', () => {
hexo.theme.setView('test.swig', [
hexo.theme.setView('test.njk', [
'{{ path }}',
'{{ url }}',
'{{ view_dir }}'
Expand All @@ -464,7 +464,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 @@ -488,7 +488,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 @@ -508,7 +508,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 @@ -526,7 +526,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 @@ -535,12 +535,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 @@ -549,7 +549,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 @@ -608,7 +608,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 @@ -617,7 +617,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 @@ -626,7 +626,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 = highlight(code);

Expand All @@ -651,7 +651,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 @@ -662,7 +662,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 = spy();
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 @@ -100,7 +102,7 @@ describe('Render', () => {
'<title>{{ title }}</title>',
'<body>{{ content }}</body>'
].join('\n'),
engine: 'swig'
engine: 'njk'
}, {
title: 'Hello world',
content: 'foobar'
Expand Down Expand Up @@ -132,7 +134,7 @@ describe('Render', () => {
it('render() - after_render filter', () => {
const data = {
text: ' <strong>123456</strong> ',
engine: 'swig'
engine: 'njk'
};

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

const filter = spy(result => result.trim());
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 @@ -194,7 +194,7 @@ describe('asset', () => {
].join('\n');

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

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

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

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

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

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

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

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

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

const file = newFile({
path: 'hello.swig',
path: 'hello.njk',
type: 'create',
renderable: true
});
Expand Down Expand Up @@ -503,7 +503,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 2cdecbf

Please sign in to comment.