From 925e9f191bbb78441b07d69413c646f846b72a3a Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 14 May 2020 23:26:43 +0300 Subject: [PATCH] more include tests, fix example --- .gitignore | 4 ++++ README.md | 2 +- package.json | 4 ++-- test/fixtures/_header.ejs | 2 ++ test/fixtures/index.ejs | 2 ++ test/fixtures/posts/_foo.ejs | 1 + test/fixtures/posts/post.ejs | 2 ++ test.js => test/test.js | 20 +++++++++++++++++++- 8 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 test/fixtures/_header.ejs create mode 100644 test/fixtures/index.ejs create mode 100644 test/fixtures/posts/_foo.ejs create mode 100644 test/fixtures/posts/post.ejs rename test.js => test/test.js (86%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae9512b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules +*.log +*-lock.* +yarn.lock diff --git a/README.md b/README.md index 9b33bb4..ca25c35 100644 --- a/README.md +++ b/README.md @@ -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 }); diff --git a/package.json b/package.json index 67e60f3..3db03ee 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/fixtures/_header.ejs b/test/fixtures/_header.ejs new file mode 100644 index 0000000..ff1ea58 --- /dev/null +++ b/test/fixtures/_header.ejs @@ -0,0 +1,2 @@ +<%- include('posts/_foo.ejs') %> +

Oh my

diff --git a/test/fixtures/index.ejs b/test/fixtures/index.ejs new file mode 100644 index 0000000..7420bc6 --- /dev/null +++ b/test/fixtures/index.ejs @@ -0,0 +1,2 @@ +<%- include('_header.ejs') %> +

Hello

diff --git a/test/fixtures/posts/_foo.ejs b/test/fixtures/posts/_foo.ejs new file mode 100644 index 0000000..c5619ca --- /dev/null +++ b/test/fixtures/posts/_foo.ejs @@ -0,0 +1 @@ +

(c) Vladimir Agafonkin

diff --git a/test/fixtures/posts/post.ejs b/test/fixtures/posts/post.ejs new file mode 100644 index 0000000..a161e0a --- /dev/null +++ b/test/fixtures/posts/post.ejs @@ -0,0 +1,2 @@ +<%- include('../_header.ejs') %> +

Lorem ipsum

diff --git a/test.js b/test/test.js similarity index 86% rename from test.js rename to test/test.js index d81cb05..3749c0c 100644 --- a/test.js +++ b/test/test.js @@ -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'}]; @@ -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(), '

(c) Vladimir Agafonkin

\n\n

Oh my

\n\n

Hello

\n'); + + const postFile = path.join(__dirname, 'fixtures/posts/post.ejs'); + const post = compile(fs.readFileSync(postFile, 'utf8'), {read, resolve, filename: postFile}); + t.equal(post(), '

(c) Vladimir Agafonkin

\n\n

Oh my

\n\n

Lorem ipsum

\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/);