UNPKG

multiconf

Version:

Work with JSON configs

77 lines (61 loc) 1.83 kB
const assert = require('node:assert'); const test = require('node:test'); const Template = require('../lib/template'); test.describe('Template', () => { test.it('renders plain text without tags', () => { const template = new Template(); assert.strictEqual(template.renderString('hello world'), 'hello world'); }); test.it('renders escaped interpolations from context', () => { const template = new Template(); assert.strictEqual( template.renderString('Hello, <%= name %>!', { name: '<Ada & Bob>' }), 'Hello, &lt;Ada &amp; Bob&gt;!', ); }); test.it('renders raw interpolations without escaping', () => { const template = new Template(); assert.strictEqual( template.renderString('Value: <%- html %>', { html: '<strong>ok</strong>' }), 'Value: <strong>ok</strong>', ); }); test.it('supports execution blocks', () => { const template = new Template(); assert.strictEqual( template.renderString('<% const total = left + right; %><%= total %>', { left: 3, right: 4, }), '7', ); }); test.it('throws for unclosed tags', () => { const template = new Template(); assert.throws( () => template.renderString('Hello, <%= name!'), { name: 'Error', message: 'Unclosed template tag.', }, ); }); test.it('throws for invalid template expressions', () => { const template = new Template(); assert.throws( () => template.renderString('<%= missing( %>'), { name: 'SyntaxError', }, ); }); test.it('throws for invalid execution blocks', () => { const template = new Template(); assert.throws( () => template.renderString('<% const value = %>'), { name: 'SyntaxError', }, ); }); });