From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- addons/web/static/tests/qweb_tests.js | 100 ++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 addons/web/static/tests/qweb_tests.js (limited to 'addons/web/static/tests/qweb_tests.js') diff --git a/addons/web/static/tests/qweb_tests.js b/addons/web/static/tests/qweb_tests.js new file mode 100644 index 00000000..fda72e23 --- /dev/null +++ b/addons/web/static/tests/qweb_tests.js @@ -0,0 +1,100 @@ +odoo.define('web.qweb_tests', function (require) { +"use strict"; + +var qwebPath = '/web/static/lib/qweb/'; + +function trim(s) { + return s.replace(/(^\s+|\s+$)/g, ''); +} + +/** + * Loads the template file, and executes all the test template in a + * qunit module $title + */ +function loadTest(assert, template, context) { + var done = assert.async(); + assert.expect(1); + + var qweb = new window.QWeb2.Engine(); + var prom = new Promise(function (resolve, reject) { + qweb.add_template(qwebPath + template, function (error, doc) { + if (error) { + return reject(error); + } + resolve({ + qweb: qweb, + doc: doc + }); + }); + }); + + prom.then(function (r) { + var qweb = r.qweb; + var doc = r.doc; + assert.expect(doc.querySelectorAll('result').length); + + var templates = qweb.templates; + for (var template in templates) { + try { + if (!templates.hasOwnProperty(template)) { + continue; + } + // ignore templates whose name starts with _, they're + // helpers/internal + if (/^_/.test(template)) { + continue; + } + + var params = doc.querySelector('params#' + template); + var args = params ? JSON.parse(params.textContent) : (context ? _.clone(context) : {}); + + var results = doc.querySelector('result#' + template); + + assert.equal( + trim(qweb.render(template, args)), + trim(results.textContent.replace(new RegExp(String.fromCharCode(13), 'g'), '')), + template); + } catch (error) { + assert.notOk(error.stack || error, 'Rendering error'); + } + } + done(); + }); + return prom; +} + +QUnit.module('QWeb', {}, function () { + QUnit.test('Output', function (assert) { + loadTest(assert, 'qweb-test-output.xml'); + }); + QUnit.test('Context-setting', function (assert) { + loadTest(assert, 'qweb-test-set.xml'); + }); + QUnit.test('Conditionals', function (assert) { + loadTest(assert, 'qweb-test-conditionals.xml'); + }); + QUnit.test('Attributes manipulation', function (assert) { + loadTest(assert, 'qweb-test-attributes.xml'); + }); + QUnit.test('Template calling (to the faraway pages)', function (assert) { + loadTest(assert, 'qweb-test-call.xml', {True: true}); + }); + QUnit.test('Foreach', function (assert) { + loadTest(assert, 'qweb-test-foreach.xml'); + }); + QUnit.test('Global', function (assert) { + // test use python syntax + var WORD_REPLACEMENT = window.QWeb2.WORD_REPLACEMENT; + window.QWeb2.WORD_REPLACEMENT = _.extend({not: '!', None: 'undefined'}, WORD_REPLACEMENT); + loadTest(assert, 'qweb-test-global.xml', {bool: function (v) { return !!v ? 'True' : 'False';}}) + .then(function () { + window.QWeb2.WORD_REPLACEMENT = WORD_REPLACEMENT; + }, function () { + window.QWeb2.WORD_REPLACEMENT = WORD_REPLACEMENT; + }); + }); + QUnit.test('Template inheritance', function (assert) { + loadTest(assert, 'qweb-test-extend.xml'); + }); +}); +}); -- cgit v1.2.3