"use strict" const doT = require("..") const assert = require("assert") describe("defines", () => { describe("without parameters", () => { it("should render define", () => { testDef("{{##def.tmp:
{{=it.foo}}
#}}{{#def.tmp}}") }) it("should render define if it is passed to doT.compile", () => { testDef("{{#def.tmp}}", {tmp: "
{{=it.foo}}
"}) }) }) describe("with parameters", () => { it("should render define", () => { testDef("{{##def.tmp:foo:
{{=foo}}
#}}{{ var bar = it.foo; }}{{# def.tmp:bar }}") }) it("should render define multiline params", () => { testDef( "{{##def.tmp:data:{{=data.openTag}}{{=data.foo}}{{=data.closeTag}}#}}\n" + "{{# def.tmp:{\n" + " foo: it.foo,\n" + ' openTag: "
",\n' + ' closeTag: "
"\n' + "} }}" ) }) function compiledDefinesParamTemplate(param) { const tmpl = `{{##def.tmp:input:
{{=input.foo}}
#}}{{#def.tmp:${param}}}` return doT.template(tmpl) } it("should render define with standard parameter", () => { const definesParamCompiled = compiledDefinesParamTemplate("it") assert.equal(definesParamCompiled({foo: "A"}), "
A
") assert.equal(definesParamCompiled({}), "
undefined
") }) it("should render define with property parameter", () => { const definesParamCompiled = compiledDefinesParamTemplate("it.bar") assert.equal(definesParamCompiled({bar: {foo: "B"}}), "
B
") assert.throws(() => { definesParamCompiled({}) }, /TypeError: Cannot read property 'foo' of undefined/) }) it("should render define with square bracket property parameter", () => { const definesParamCompiled = compiledDefinesParamTemplate("it['bar']") assert.equal(definesParamCompiled({bar: {foo: "C"}}), "
C
") assert.throws(() => { definesParamCompiled({}) }, /TypeError: Cannot read property 'foo' of undefined/) }) it("should render define with square bracket property with space parameter", () => { const definesParamCompiled = compiledDefinesParamTemplate("it['bar baz']") assert.equal(definesParamCompiled({"bar baz": {foo: "D"}}), "
D
") assert.throws(() => { definesParamCompiled({}) }, /TypeError: Cannot read property 'foo' of undefined/) }) it("should render define with array index property parameter", () => { const definesParamCompiled = compiledDefinesParamTemplate("it[1]") assert.equal(definesParamCompiled(["not this", {foo: "E"}, "not this"]), "
E
") assert.throws(() => { definesParamCompiled({}) }, /TypeError: Cannot read property 'foo' of undefined/) }) it("should render define with deep properties parameter", () => { const definesParamCompiled = compiledDefinesParamTemplate("it['bar baz'].qux[1]") assert.equal( definesParamCompiled({"bar baz": {qux: ["not this", {foo: "F"}, "not this"]}}), "
F
" ) assert.throws(() => { definesParamCompiled({}) }, /TypeError: Cannot read property 'qux' of undefined/) }) }) function testDef(tmpl, defines) { const fn = doT.compile(tmpl, defines) assert.equal(fn({foo: "http"}), "
http
") assert.equal(fn({foo: "http://abc.com"}), "
http://abc.com
") assert.equal(fn({}), "
undefined
") } })