Validate bodies
This commit is contained in:
parent
7905df5a4a
commit
e8b6a3c90e
|
@ -98,45 +98,115 @@ describe("hbs validator", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("expressions with parameters", () => {
|
describe("expressions with parameters", () => {
|
||||||
const validators: CodeValidator = {
|
describe("basic expression", () => {
|
||||||
helperFunction: {
|
const validators: CodeValidator = {
|
||||||
arguments: ["a", "b", "c"],
|
helperFunction: {
|
||||||
},
|
arguments: ["a", "b", "c"],
|
||||||
}
|
},
|
||||||
|
}
|
||||||
|
|
||||||
it("validate valid params", () => {
|
it("validate valid params", () => {
|
||||||
const text = "{{ helperFunction 1 99 'a' }}"
|
const text = "{{ helperFunction 1 99 'a' }}"
|
||||||
|
|
||||||
const result = validateHbsTemplate(text, validators)
|
const result = validateHbsTemplate(text, validators)
|
||||||
expect(result).toHaveLength(0)
|
expect(result).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws on too few params", () => {
|
||||||
|
const text = "{{ helperFunction 100 }}"
|
||||||
|
|
||||||
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
message: `Helper "helperFunction" expects 3 parameters (a, b, c), but got 1.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 24,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("throws on too many params", () => {
|
||||||
|
const text = "{{ helperFunction 1 99 'a' 100 }}"
|
||||||
|
|
||||||
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
message: `Helper "helperFunction" expects 3 parameters (a, b, c), but got 4.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 34,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("throws on too few params", () => {
|
describe("body expressions", () => {
|
||||||
const text = "{{ helperFunction 100 }}"
|
const validators: CodeValidator = {
|
||||||
|
bodyFunction: {
|
||||||
const result = validateHbsTemplate(text, validators)
|
arguments: ["a", "b", "c"],
|
||||||
expect(result).toEqual([
|
requiresBlock: true,
|
||||||
{
|
|
||||||
from: 0,
|
|
||||||
message: `Helper "helperFunction" expects 3 parameters (a, b, c), but got 1.`,
|
|
||||||
severity: "error",
|
|
||||||
to: 24,
|
|
||||||
},
|
},
|
||||||
])
|
nonBodyFunction: {
|
||||||
})
|
arguments: ["a", "b"],
|
||||||
|
|
||||||
it("throws on too many params", () => {
|
|
||||||
const text = "{{ helperFunction 1 99 'a' 100 }}"
|
|
||||||
|
|
||||||
const result = validateHbsTemplate(text, validators)
|
|
||||||
expect(result).toEqual([
|
|
||||||
{
|
|
||||||
from: 0,
|
|
||||||
message: `Helper "helperFunction" expects 3 parameters (a, b, c), but got 4.`,
|
|
||||||
severity: "error",
|
|
||||||
to: 34,
|
|
||||||
},
|
},
|
||||||
])
|
}
|
||||||
|
|
||||||
|
it("validate valid params", () => {
|
||||||
|
const text = "{{#bodyFunction 1 99 }}body{{/bodyFunction}}"
|
||||||
|
|
||||||
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
expect(result).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validate empty bodies", () => {
|
||||||
|
const text = "{{#bodyFunction 1 99 }}{{/bodyFunction}}"
|
||||||
|
|
||||||
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
expect(result).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validate too little parameters", () => {
|
||||||
|
const text = "{{#bodyFunction 1 }}{{/bodyFunction}}"
|
||||||
|
|
||||||
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
message: `Helper "bodyFunction" expects 3 parameters (a, b, c), but got 2.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 37,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validate too many parameters", () => {
|
||||||
|
const text = "{{#bodyFunction 1 99 'a' 0 }}{{/bodyFunction}}"
|
||||||
|
|
||||||
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
message: `Helper "bodyFunction" expects 3 parameters (a, b, c), but got 5.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 46,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validate non-supported body usages", () => {
|
||||||
|
const text = "{{#nonBodyFunction 1 99}}{{/nonBodyFunction}}"
|
||||||
|
|
||||||
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
from: 0,
|
||||||
|
message: `Helper "nonBodyFunction" should not contain a body.`,
|
||||||
|
severity: "error",
|
||||||
|
to: 45,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("optional parameters", () => {
|
describe("optional parameters", () => {
|
||||||
|
|
Loading…
Reference in New Issue