From 4026b321af4c6386b287a3698d8f546a0ffa15e3 Mon Sep 17 00:00:00 2001 From: sushuang Date: Mon, 8 Jan 2018 20:10:10 +0800 Subject: [PATCH] test util supports print data table. --- test/lib/reset.css | 17 +++++++++ test/lib/testHelper.js | 83 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/test/lib/reset.css b/test/lib/reset.css index 2a90fc390..86e0f2b36 100644 --- a/test/lib/reset.css +++ b/test/lib/reset.css @@ -8,3 +8,20 @@ body > .main { padding: 0; font-family: arial; } + + +table.test-data-table { + margin: 10px auto; + border: 1px solid #ccc; + border-spacing: 0; +} +table.test-data-table td { + border: 1px solid #ccc; + color: #777; + padding: 3px 5px; + font-size: 13px; +} +table.test-data-table-key td { + font-size: 12px; + color: rgb(69, 162, 238) +} \ No newline at end of file diff --git a/test/lib/testHelper.js b/test/lib/testHelper.js index c46f0d7e5..667dbb33b 100644 --- a/test/lib/testHelper.js +++ b/test/lib/testHelper.js @@ -104,10 +104,91 @@ */ dir: function () { return location.origin + testHelper.resolve(location.pathname, '..'); - } + }, + + /** + * @param {string} elId The data table will be inserted before the el. + * @param {Array|Object} data [[], [], ...] or [{}, {}, ...] or {xx: [...], yy: [...]} + */ + createDataTable: function (elId, data) { + var sourceFormat = detectSourceFormat(data); + var el = document.getElementById(elId); + + if (!el || !sourceFormat) { + return; + } + + var html = ['']; + + if (sourceFormat === 'arrayRows') { + for (var i = 0; i < data.length; i++) { + var line = data[i]; + var htmlLine = ['']; + for (var j = 0; j < line.length; j++) { + var val = line[j]; + htmlLine.push(''); + } + htmlLine.push(''); + html.push(htmlLine.join('')); + } + } + else if (sourceFormat === 'objectRows') { + for (var i = 0; i < data.length; i++) { + var line = data[i]; + var htmlLine = ['']; + for (var key in line) { + if (line.hasOwnProperty(key)) { + htmlLine.push(''); + htmlLine.push(''); + } + } + htmlLine.push(''); + html.push(htmlLine.join('')); + } + } + else if (sourceFormat === 'keyedColumns') { + for (var key in data) { + var htmlLine = ['']; + htmlLine.push(''); + if (data.hasOwnProperty(key)) { + var col = data[key] || []; + for (var i = 0; i < col.length; i++) { + htmlLine.push(''); + } + } + htmlLine.push(''); + html.push(htmlLine.join('')); + } + } + html.push('
' + testHelper.encodeHTML(val) + '
' + testHelper.encodeHTML(key) + '' + testHelper.encodeHTML(line[key]) + '
' + testHelper.encodeHTML(key) + '' + testHelper.encodeHTML(col[i]) + '
'); + var container = document.createElement('div'); + container.innerHTML = html.join(''); + el.parentNode.insertBefore(container, el); + } }; + function detectSourceFormat(data) { + if (data.length) { + for (var i = 0, len = data.length; i < len; i++) { + var item = data[i]; + + if (item == null) { + continue; + } + else if (item.length) { + return 'arrayRows'; + } + else if (typeof data === 'object') { + return 'objectRows'; + } + } + } + else if (typeof data === 'object') { + return 'keyedColumns'; + } + } + // resolves . and .. elements in a path array with directory names there // must be no slashes or device names (c:\) in the array // (so also no leading and trailing slashes - it does not distinguish -- GitLab