Skip to content

Commit

Permalink
more include tests, fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed May 14, 2020
1 parent 3c6ddcf commit 925e9f1
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
*.log
*-lock.*
yarn.lock
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const cache = {};

const template = ejs.compile(`<%- include('../bar.html') %>`, {
filename: 'foo/foo.html',
resolve: (parent, filename) => path.join(parent, filename),
resolve: (parent, filename) => path.join(path.dirname(parent), filename),
read: filename => fs.readFileSync(filename, 'utf8'),
cache
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"tape": "^5.0.0"
},
"scripts": {
"pretest": "eslint index.js test.js",
"test": "node test.js"
"pretest": "eslint index.js test/test.js",
"test": "node test/test.js"
},
"eslintConfig": {
"extends": "mourner",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/_header.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- include('posts/_foo.ejs') %>
<h1>Oh my</h1>
2 changes: 2 additions & 0 deletions test/fixtures/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- include('_header.ejs') %>
<h2>Hello</h2>
1 change: 1 addition & 0 deletions test/fixtures/posts/_foo.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>(c) Vladimir Agafonkin</p>
2 changes: 2 additions & 0 deletions test/fixtures/posts/post.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- include('../_header.ejs') %>
<p>Lorem ipsum</p>
20 changes: 19 additions & 1 deletion test.js → test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

const fs = require('fs');
const path = require('path');
const {test} = require('tape');
const {compile} = require('./index.js');

const {compile} = require('../index.js');

const users = [{name: 'Vlad'}, {name: 'Masha'}, {name: 'Dasha'}];

Expand Down Expand Up @@ -129,6 +132,21 @@ test('includes', (t) => {
t.end();
});

test('includes node', (t) => {
const read = file => fs.readFileSync(file, 'utf8');
const resolve = (parent, file) => path.join(path.dirname(parent), file);

const indexFile = path.join(__dirname, 'fixtures/index.ejs');
const index = compile(fs.readFileSync(indexFile, 'utf8'), {read, resolve, filename: indexFile});
t.equal(index(), '<p>(c) Vladimir Agafonkin</p>\n\n<h1>Oh my</h1>\n\n<h2>Hello</h2>\n');

const postFile = path.join(__dirname, 'fixtures/posts/post.ejs');
const post = compile(fs.readFileSync(postFile, 'utf8'), {read, resolve, filename: postFile});
t.equal(post(), '<p>(c) Vladimir Agafonkin</p>\n\n<h1>Oh my</h1>\n\n<p>Lorem ipsum</p>\n');

t.end();
});

test('async', async (t) => {
t.equal(await compile('<%= await 0 %>', {async: true})(), '0', 'await');
t.throws(() => compile('<% await 0 %>')(), /await is only valid in async function/);
Expand Down

0 comments on commit 925e9f1

Please sign in to comment.