mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
9d99f8936b
Previously, when a template failed to compile because of a syntax error (typically because we don't do any syntax checking when compiling expression, allowing invalid expressions to be transpiled successfully), the error reporting was very minimal: you would only get the error message itself (eg: "Unexpected token") with the stack information of the error pointing to the call to `new Function` in owl, which is not very useful. This commit catches the compilation error and completes it with information about the template name when available, and also adds the generated code to the error message, allowing the user to just copy/paste it in their web console or code editor to get a more precise location for the error.
59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import { TemplateSet } from "../../src/runtime/template_set";
|
|
import { renderToString, snapshotTemplate, TestContext } from "../helpers";
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// basic validation
|
|
// -----------------------------------------------------------------------------
|
|
|
|
describe("basic validation", () => {
|
|
test("error if no template with given name", () => {
|
|
const context = new TemplateSet();
|
|
expect(() => context.getTemplate("invalidname")).toThrow("Missing template");
|
|
});
|
|
|
|
test("cannot add a different template with the same name", () => {
|
|
const context = new TemplateSet();
|
|
context.addTemplate("test", `<t/>`);
|
|
// Same template with the same name is fine
|
|
expect(() => context.addTemplate("test", "<t/>")).not.toThrow();
|
|
// Different template with the same name crashes
|
|
expect(() => context.addTemplate("test", "<div/>")).toThrow("already defined");
|
|
});
|
|
|
|
test("invalid xml", () => {
|
|
const template = "<div>";
|
|
expect(() => snapshotTemplate(template)).toThrow("Invalid XML in template");
|
|
});
|
|
|
|
test("missing template in template set", () => {
|
|
const context = new TestContext();
|
|
const template = `<t t-call="othertemplate" />`;
|
|
|
|
context.addTemplate("template", template);
|
|
expect(() => context.renderToString("template")).toThrowError("Missing");
|
|
});
|
|
|
|
test("error when unknown directive", () => {
|
|
const template = `<div t-best-beer="rochefort 10">test</div>`;
|
|
expect(() => renderToString(template)).toThrow("Unknown QWeb directive: 't-best-beer'");
|
|
});
|
|
|
|
test("compilation error", () => {
|
|
const template = `<div t-att-class="a b">test</div>`;
|
|
expect(() => renderToString(template))
|
|
.toThrow(`Failed to compile anonymous template: Unexpected identifier
|
|
|
|
generated code:
|
|
function(app, bdom, helpers) {
|
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
|
|
|
let block1 = createBlock(\`<div block-attribute-0="class">test</div>\`);
|
|
|
|
return function template(ctx, node, key = "") {
|
|
let attr1 = ctx['a']ctx['b'];
|
|
return block1([attr1]);
|
|
}
|
|
}`);
|
|
});
|
|
});
|