summaryrefslogtreecommitdiff
path: root/addons/web/static/lib
diff options
context:
space:
mode:
authorXavier Morel <xmo@odoo.com>2017-10-02 16:09:13 +0200
committerXavier Morel <xmo@odoo.com>2017-10-10 14:25:19 +0200
commit1e3c76f41d9b74a25f8f2449efcd791e36f5d744 (patch)
treeee3ddb9738f05526bacb2a4b7331e8383a683d77 /addons/web/static/lib
parent6a291815e7bfc033c7f2d661d4284dd8185552e5 (diff)
[IMP] web: add web tests to regular JS suite
Remove old tests page as it was obviously unmaintained and did not run at all...
Diffstat (limited to 'addons/web/static/lib')
-rw-r--r--addons/web/static/lib/qweb/qweb-test.js.html73
-rw-r--r--addons/web/static/lib/qweb/tests.js77
2 files changed, 77 insertions, 73 deletions
diff --git a/addons/web/static/lib/qweb/qweb-test.js.html b/addons/web/static/lib/qweb/qweb-test.js.html
deleted file mode 100644
index 0ee5f27c3bd..00000000000
--- a/addons/web/static/lib/qweb/qweb-test.js.html
+++ /dev/null
@@ -1,73 +0,0 @@
-<!doctype html>
-<html>
-<head>
- <script src="/web/static/lib/jquery/jquery.js"></script>
- <link rel="stylesheet" href="/web/static/lib/qunit/qunit.css" type="text/css" media="screen"/>
- <script type="text/javascript" src="/web/static/lib/qunit/qunit.js"></script>
-
- <script type="text/javascript" src="qweb2.js"></script>
-
- <script>
- QWeb = new QWeb2.Engine();
- function trim(s) {
- return s.replace(/(^\s+|\s+$)/g, '');
- }
- function render(template, context) {
- return trim(QWeb.render(template, context)).toLowerCase();
- }
-
- /**
- * Loads the template file, and executes all the test template in a
- * qunit module $title
- */
- function test(title, template) {
- QUnit.module(title, {
- setup: function () {
- var self = this;
- this.qweb = new QWeb2.Engine();
- QUnit.stop();
- this.qweb.add_template(template, function (_, doc) {
- self.doc = doc;
- QUnit.start();
- })
- }
- });
- QUnit.test('autotest', function (assert) {
- var templates = this.qweb.templates;
- for (var template in templates) {
- if (!templates.hasOwnProperty(template)) { continue; }
- // ignore templates whose name starts with _, they're
- // helpers/internal
- if (/^_/.test(template)) { continue; }
-
- var params = this.doc.querySelector('params#' + template);
- var args = params ? JSON.parse(params.textContent) : {};
-
- var results = this.doc.querySelector('result#' + template);
- assert.equal(
- trim(this.qweb.render(template, args)),
- trim(results.textContent),
- template);
- }
- });
- }
- $(document).ready(function() {
- test("Output", 'qweb-test-output.xml');
- test("Context-setting", 'qweb-test-set.xml');
- test("Conditionals", 'qweb-test-conditionals.xml');
- test("Attributes manipulation", 'qweb-test-attributes.xml');
- test("Template calling (to the faraway pages)",
- 'qweb-test-call.xml');
- test("Foreach", 'qweb-test-foreach.xml');
- test("Global", 'qweb-test-global.xml');
-
- test('Template inheritance', 'qweb-test-extend.xml');
- });
- </script>
-
-</head>
-<body>
- <div id="qunit"></div>
- <div id="qunit-fixture"></div>
-</body>
-</html>
diff --git a/addons/web/static/lib/qweb/tests.js b/addons/web/static/lib/qweb/tests.js
new file mode 100644
index 00000000000..5b961acd427
--- /dev/null
+++ b/addons/web/static/lib/qweb/tests.js
@@ -0,0 +1,77 @@
+odoo.define('qweb.tests', function () {
+'use strict';
+
+function trim(s) {
+ return s.replace(/(^\s+|\s+$)/g, '');
+}
+
+/**
+ * Generates a QUnit.module hook object loading the specified test file
+ * (from /web/static/lib/qweb) and setting ``this.qweb`` (the qweb
+ * instance for this module) and ``this.doc`` (the loaded XML file).
+ *
+ * Note that test files mix template elements <t t-name>, params elements
+ * <params> and result elements <result>. A result and an optional params
+ * object are linked to the corresponding template via the ``id``
+ * attribute (result and params have the template name as id).
+ */
+function hooks(testfile) {
+ var template = '/web/static/lib/qweb/' + testfile;
+ return {
+ before: function () {
+ var self = this;
+ this.qweb = new QWeb2.Engine();
+ var p = $.Deferred();
+ this.qweb.add_template(template, function (_, doc) {
+ self.doc = doc;
+ p.resolve(doc);
+ });
+ return p.promise();
+ }
+ }
+}
+// can't get generative QUnit.test (e.g. QUnit.test in a for(;;)
+// or Array#forEach() loop) to work, so each test file has a single test,
+// that seems to work
+function runtest() {
+ QUnit.test('', function (assert) {
+ var templates = this.qweb.templates;
+ assert.expect(_.reduce(templates, function (acc, _, k) {
+ return acc + (/^_/.test(k) ? 0 : 1);
+ }, 0));
+ for (var template in templates) {
+ if (!templates.hasOwnProperty(template)) { continue; }
+ // ignore templates whose name starts with _, they're
+ // helpers/internal
+ if (/^_/.test(template)) { continue; }
+
+ var params = this.doc.querySelector('params#' + template);
+ var args = params ? JSON.parse(params.textContent) : {};
+
+ var results = this.doc.querySelector('result#' + template);
+ assert.equal(
+ trim(this.qweb.render(template, args)),
+ trim(results.textContent),
+ template);
+ }
+ });
+}
+
+var TEMPLATES = [
+ ["Output", 'qweb-test-output.xml'],
+ ["Context-setting", 'qweb-test-set.xml'],
+ ["Conditionals", 'qweb-test-conditionals.xml'],
+ ["Attributes manipulation", 'qweb-test-attributes.xml'],
+ ["Template calling (to the faraway pages)", 'qweb-test-call.xml'],
+ ["Foreach", 'qweb-test-foreach.xml'],
+ ["Global", 'qweb-test-global.xml'],
+ ['Template inheritance', 'qweb-test-extend.xml']
+];
+
+QUnit.module('qweb', {}, function () {
+ TEMPLATES.forEach(function (it) {
+ QUnit.module(it[0], hooks(it[1]), runtest);
+ })
+});
+
+});