-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1542fdf
commit 60057fd
Showing
1 changed file
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,118 @@ | ||
import * as binding from '..'; | ||
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'); | ||
}); | ||
|
||
describe('#funcs', () => { | ||
it('works', () => { | ||
const myFunc = jest.fn((value: string) => `pre-${value}-post`); | ||
template.funcs({ myFunc }); | ||
template.parse('{{ myFunc "hello" }}'); | ||
expect(template.executeString()).toBe('pre-hello-post'); | ||
expect(myFunc).toHaveBeenCalledWith('hello'); | ||
expect(myFunc).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('round-trips JS types', () => { | ||
const myFunc = jest.fn((value: unknown) => value); | ||
template.funcs({ myFunc }); | ||
template.parse('{{ myFunc . }}'); | ||
// expect(template.executeString({param: undefined})).toBe('<no value>'); | ||
expect(template.executeString(null)).toBe('<no value>'); | ||
expect(myFunc).toHaveBeenLastCalledWith(null); | ||
expect(template.executeString(true)).toBe('true'); | ||
expect(myFunc).toHaveBeenLastCalledWith(true); | ||
expect(template.executeString(123)).toBe('123'); | ||
expect(myFunc).toHaveBeenLastCalledWith(123); | ||
expect(template.executeString('param')).toBe('param'); | ||
expect(myFunc).toHaveBeenLastCalledWith('param'); | ||
expect(template.executeString({foo: 'bar'})).toBe('map[foo:bar]'); | ||
expect(myFunc).toHaveBeenLastCalledWith({foo: 'bar'}); | ||
expect(template.executeString(['foo', 'bar'])).toBe('[foo bar]'); | ||
expect(myFunc).toHaveBeenLastCalledWith(['foo', 'bar']); | ||
// TODO: BigInt support when supported | ||
}); | ||
|
||
it('passes Go integers to JS', () => { | ||
const myFunc = jest.fn((value: unknown) => `${value}`); | ||
template.funcs({ myFunc }); | ||
template.parse('{{ myFunc 123 }}'); | ||
expect(template.executeString()).toBe('123'); | ||
expect(myFunc).toHaveBeenCalledWith(123); | ||
}) | ||
}); | ||
|
||
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'); | ||
}); | ||
|
||
describe('#option', () => { | ||
it('works', () => { | ||
template.parse('{{ .param }}'); | ||
expect(template.executeString({})).toBe('<no value>'); | ||
template.option('missingkey=error'); | ||
expect(() => template.executeString({})).toThrowErrorMatchingInlineSnapshot(`"template: test_template:1:3: executing "test_template" at <.param>: map has no entry for key "param""`); | ||
}); | ||
}); | ||
|
||
// TODO: parse, parseFiles, parseGlob, templates | ||
}); | ||
|
||
test('htmlEscapeString works', () => { | ||
expect(binding.htmlEscapeString('<br>')).toBe('<br>'); | ||
}); | ||
|
||
test('htmlEscaper works', () => { | ||
expect(binding.htmlEscaper('foo', '<bar>')).toBe('foo<bar>'); | ||
}); | ||
|
||
test('jsEscapeString works', () => { | ||
expect(binding.jsEscapeString('"foo"')).toBe('\\"foo\\"'); | ||
}); | ||
|
||
test('jsEscaper works', () => { | ||
expect(binding.jsEscaper('foo', '"bar"')).toBe('foo\\"bar\\"'); | ||
}); | ||
|
||
test('urlQueryEscaper works', () => { | ||
expect(binding.urlQueryEscaper('foo', '&bar')).toBe('foo%26bar'); | ||
}); |