Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
drakedevel committed Jan 19, 2024
1 parent 1542fdf commit a8ab08a
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions tests/basic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
import {Template} from '..';

describe('Template', () => {
test('constructor works', () => {
expect(() => {
new Template('test');
}).not.toThrow();
let template: Template;

beforeEach(() => {
template = new Template('test_template');
});

// TODO: clone

test('#definedTemplates works', () => {
expect(template.definedTemplates()).toBe('');
template.parse('{{define "foo"}}{{ end }}');
expect(template.definedTemplates()).toMatch(/^; defined templates are: ("foo", "test_template"|"test_template", "foo")$/);
});

// TODO: delims

test('#executeString works', () => {
template.parse('{{ .foo }}, {{ .bar }}');
expect(template.executeString({foo: 'hello', bar: 'world'})).toBe('hello, world');
});

test('#executeTemplateString works', () => {
template.parse('{{ define "inner" }}inner {{ .param }}{{ end }}outer');
expect(template.executeTemplateString('inner', {param: 'param'})).toBe('inner param');
});

// TODO: funcs

test('#lookup works', () => {
template.parse('{{ define "foo" }}{{ end }}');
const fooTemplate = template.lookup('foo');
expect(fooTemplate).toBeInstanceOf(Template);
expect(fooTemplate?.name()).toBe('foo');
});

test('#name works', () => {
expect(template.name()).toBe('test_template');
});

test('#new works', () => {
const inner = template.new('inner').parse('new {{ .param }}');
expect(inner).toBeInstanceOf(Template);
expect(inner.name()).toBe('inner');
expect(template.executeTemplateString('inner', {param: 'param'})).toBe('new param');
});

// TODO: option, parse, parseFiles, parseGlob, templates
});

0 comments on commit a8ab08a

Please sign in to comment.