Add parameter tests
This commit is contained in:
parent
a4de97c419
commit
4da04e0a05
|
@ -1,7 +1,8 @@
|
||||||
import { validateHbsTemplate } from "../hbs"
|
import { validateHbsTemplate } from "../hbs"
|
||||||
|
import { CodeValidator } from "@/types"
|
||||||
|
|
||||||
describe("hbs validator", () => {
|
describe("hbs validator", () => {
|
||||||
it("can validate empty strings", () => {
|
it("validate empty strings", () => {
|
||||||
const text = ""
|
const text = ""
|
||||||
const validators = {}
|
const validators = {}
|
||||||
|
|
||||||
|
@ -9,7 +10,7 @@ describe("hbs validator", () => {
|
||||||
expect(result).toHaveLength(0)
|
expect(result).toHaveLength(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("can validate strings without hbs expressions", () => {
|
it("validate strings without hbs expressions", () => {
|
||||||
const text = "first line\nand another one"
|
const text = "first line\nand another one"
|
||||||
const validators = {}
|
const validators = {}
|
||||||
|
|
||||||
|
@ -22,14 +23,14 @@ describe("hbs validator", () => {
|
||||||
fieldName: {},
|
fieldName: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
it("can validate valid expressions", () => {
|
it("validate valid expressions", () => {
|
||||||
const text = "{{ fieldName }}"
|
const text = "{{ fieldName }}"
|
||||||
|
|
||||||
const result = validateHbsTemplate(text, validators)
|
const result = validateHbsTemplate(text, validators)
|
||||||
expect(result).toHaveLength(0)
|
expect(result).toHaveLength(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("can validate invalid expressions", () => {
|
it("throws on invalid expressions", () => {
|
||||||
const text = "{{ anotherFieldName }}"
|
const text = "{{ anotherFieldName }}"
|
||||||
|
|
||||||
const result = validateHbsTemplate(text, validators)
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
@ -43,7 +44,7 @@ describe("hbs validator", () => {
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
it("can validate untrimmed invalid expressions", () => {
|
it("throws on untrimmed invalid expressions", () => {
|
||||||
const text = " {{ anotherFieldName }}"
|
const text = " {{ anotherFieldName }}"
|
||||||
|
|
||||||
const result = validateHbsTemplate(text, validators)
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
@ -57,7 +58,7 @@ describe("hbs validator", () => {
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
it("can validate invalid expressions between valid lines", () => {
|
it("throws on invalid expressions between valid lines", () => {
|
||||||
const text =
|
const text =
|
||||||
"literal expression\nthe value is {{ anotherFieldName }}\nanother expression"
|
"literal expression\nthe value is {{ anotherFieldName }}\nanother expression"
|
||||||
|
|
||||||
|
@ -72,4 +73,47 @@ describe("hbs validator", () => {
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("expressions with parameters", () => {
|
||||||
|
const validators: CodeValidator = {
|
||||||
|
helperFunction: {
|
||||||
|
arguments: ["a", "b", "c"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
it("validate valid params", () => {
|
||||||
|
const text = "{{ helperFunction 1 99 'a' }}"
|
||||||
|
|
||||||
|
const result = validateHbsTemplate(text, validators)
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -14,5 +14,6 @@
|
||||||
"assets/*": ["assets/*"],
|
"assets/*": ["assets/*"],
|
||||||
"@/*": ["src/*"]
|
"@/*": ["src/*"]
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"exclude": []
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue