Skip to content

Commit df4d6dd

Browse files
committed
Test-group added; Bug-fixes;
1 parent 7bf0fc4 commit df4d6dd

File tree

5 files changed

+151
-30
lines changed

5 files changed

+151
-30
lines changed

src/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private static function final_writer(mixed $value = null, StatusType $status = S
179179
if(Config::$dev_mode) {
180180
$response['meta'] = [];
181181
if(defined(__NAMESPACE__ . '\\__START_TIME__')) {
182-
$response['meta']['exe_time'] = microtime(true) - constant(__NAMESPACE__ . '\\__START_TIME__');
182+
$response['meta']['exe_time'] = round(microtime(true) - constant(__NAMESPACE__ . '\\__START_TIME__'), 7);
183183
}
184184
$response['meta']['mem_peak'] = memory_get_peak_usage();
185185
if(count(self::$debug) > 0) {

test/client/css/common.css

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,33 @@
33
}
44
body {
55
margin: 0;
6-
padding: 0;
6+
padding: 0.5em;
7+
display: grid;
8+
gap: 0.5em;
9+
}
10+
body > section {
11+
display: grid;
12+
gap: 0.5em;
13+
}
14+
body > section > header {
15+
display: grid;
16+
grid-template-columns: max-content max-content;
17+
gap: 0.5em;
18+
justify-content: space-around;
19+
font-family: Consolas, 'Courier New', monospace;
20+
font-size: large;
21+
background: #1a237e;
22+
color: white;
23+
padding: 0.5em 0.75em;
24+
box-shadow: 0 0 5px black;
25+
}
26+
body > section > header span.code {
27+
font-style: italic;
28+
text-decoration: underline;
29+
}
30+
body > section > header.passed {
31+
background: #1a237e;
32+
}
33+
body > section > header.failed {
34+
background: red;
735
}

test/client/js/api-tester.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export class APITester {
6969
meta.net_time = performance.now() - meta.net_time;
7070
meta.api_time = (typeof response.meta?.exe_time === 'number')
7171
? Number((new Number(response.meta.exe_time * 1000)).toPrecision(4))
72-
: 'Unknown';
72+
: null;
7373
meta.api_mem_peak = (typeof response.meta?.mem_peak === 'number')
7474
? Number((new Number(response.meta.mem_peak / 1024)).toPrecision(3))
75-
: 'Unknown';
75+
: null;
7676

7777
let results = {};
7878
for(const t of tests) {

test/client/js/test-report.js renamed to test/client/js/component/test-report.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class TestReport extends HTMLElement {
66
#root;
77
#data = null;
88
#data_set = false;
9+
910
constructor() {
1011
super();
1112
this.#root = this.attachShadow({mode: 'closed'});
@@ -49,28 +50,34 @@ class TestReport extends HTMLElement {
4950
return;
5051
}
5152
this.#data = value;
52-
this.#data.query = TestReport.#dataQuery.bind(this.#data);
53+
const boundQueryData = TestReport.#dataQuery.bind(this.#data);
5354
Object.freeze(this.#data);
5455
this.#data_set = true;
5556

5657
const summ = {
57-
nodes: this.#qs('summary dd', true)
58-
};
59-
summ.setVal = (idx, dataQuery, def, suffix = '') => {
60-
let val = this.#data.query(dataQuery);
61-
summ.nodes[idx].innerText = (val === null) ? def : `${val}${suffix}`;
58+
nodes: this.#qs('summary dd', true),
59+
setVal: (idx, dataQuery, def, suffix = '') => {
60+
let val = boundQueryData(dataQuery);
61+
summ.nodes[idx].innerText = (val === null) ? def : `${val}${suffix}`;
62+
}
6263
};
63-
summ.setVal(0, 'title', 'None');
6464
summ.setVal(2, 'meta.api_time', 'Unknown', 'ms');
6565
summ.setVal(3, 'meta.net_time', 'Unknown', 'ms');
6666
summ.setVal(4, 'meta.api_mem_peak', 'Unknown', 'KB');
67+
68+
{
69+
summ.nodes[0].innerHTML = (boundQueryData('title') ?? def).replaceAll(/'([^']+)'/g, '<span class="code">$1</span>');
70+
}
71+
6772

68-
let result = Object.values(this.#data.results);
69-
result = {total: result.length, passed: result.reduce((c, v) => v ? c + 1 : c, 0)};
70-
result.all = result.total === result.passed;
73+
{
74+
let result = Object.values(this.#data.results);
75+
result = {total: result.length, passed: result.reduce((c, v) => v ? c + 1 : c, 0)};
76+
result.all = result.total === result.passed;
7177

72-
summ.nodes[1].innerText = `[${result.passed}/${result.total}]`;
73-
summ.nodes[1].classList.add(result.all ? 'passed' : 'failed');
78+
summ.nodes[1].innerText = `[${result.passed}/${result.total}]`;
79+
summ.nodes[1].classList.add(result.all ? 'passed' : 'failed');
80+
}
7481

7582
const main = this.#qs('main');
7683
main.appendChild(TestReport.#genSection('Request:', this.#data.request));
@@ -105,28 +112,31 @@ TestReport._meta.template.innerHTML =
105112
margin: 0;
106113
}
107114
dd.passed {
108-
color: blue;
115+
color: #2196f3;
109116
}
110117
dd.failed {
111118
color: red;
112119
}
113120
details {
114121
box-shadow: 0 0 5px black;
115-
padding: 0.5em 0.75em;
116-
font-family: Consolas,'Courier New', monospace;
122+
font-family: Consolas, 'Courier New', monospace;
117123
display: grid;
118124
}
119125
details > summary {
126+
padding: 0.5em 0.75em;
120127
cursor: pointer;
121128
list-style-type: none;
122129
display: grid;
123130
gap: 0.5em;
124131
grid-template-columns: minmax(max-content, 1fr) repeat(4, 1fr);
132+
background: #212121;
133+
color: white;
125134
}
126135
details[open] > summary {
127136
border-bottom: 1px solid black;
128137
}
129138
details > main {
139+
padding: 0.5em 0.75em;
130140
display: grid;
131141
grid-template-columns: 1fr 1fr;
132142
gap: 0.5em;
@@ -141,13 +151,17 @@ TestReport._meta.template.innerHTML =
141151
font-weight: bold;
142152
}
143153
pre {
144-
font-family: Consolas,'Courier New', monospace;
154+
font-family: Consolas, 'Courier New', monospace;
145155
white-space: pre-wrap;
146156
padding: 0;
147157
margin: 0;
148158
}
159+
span.code {
160+
font-style: italic;
161+
text-decoration: underline;
162+
}
149163
</style>
150-
<details open>
164+
<details>
151165
<summary>
152166
<dl><dt>Title:</dt><dd>Test</dd></dl>
153167
<dl><dt>Passed:</dt><dd>Unknown</dd></dl>

test/client/types.html

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,98 @@
66
<link rel="stylesheet" href="css/common.css">
77
</head>
88
<body>
9-
<test-report></test-report>
109
<script type="module">
11-
import './js/test-report';
10+
import './js/component/test-report.js';
1211
import {APITester, ResultTest as RT} from './js/api-tester.js';
1312
const T = new APITester('../server/public', window.location.href);
14-
15-
const report = await T.test('Type-test: Sending int as int', {
16-
func: 'data_type',
17-
param: {
18-
int: 10
13+
14+
async function test_n_report({title, type, sendValue = null, actualValue = null, okStatus = true, testValue = true, tests = [], attachPoint = document.body } = {}) {
15+
let autoTests = [RT.store.statusOK(okStatus)];
16+
if(testValue) {
17+
autoTests.push(RT.store.cmpValue({[type]: actualValue}));
1918
}
20-
}, RT.store.statusOK(), RT.store.cmpValue({int: 10}));
21-
document.querySelector('test-report').data = report;
19+
const node = document.createElement('test-report');
20+
const report = await T.test(`Type-test: ${title}`, {
21+
func: 'data_type',
22+
param: {
23+
[type]: sendValue
24+
}
25+
}, ...autoTests, ...tests);
26+
node.data = report;
27+
attachPoint.appendChild(node);
28+
return report;
29+
}
30+
async function test_group(title, ...tests) {
31+
const node = document.createElement('section');
32+
const result = {
33+
total: 0,
34+
passed: 0
35+
};
36+
for(let t of tests) {
37+
let {results: testResult} = await test_n_report({
38+
...t,
39+
attachPoint: node
40+
});
41+
testResult = Object.values(testResult).reduce((carry, v) => carry && v, true);
42+
result.total++;
43+
if(testResult) {
44+
result.passed++;
45+
}
46+
}
47+
result.all = result.total === result.passed;
48+
const header = document.createElement('header');
49+
header.innerHTML =
50+
`<div>
51+
<strong>Title:</strong>
52+
${title.replaceAll(/'([^']+)'/g, '<span class="code">$1</span>')}
53+
</div>
54+
<div>
55+
<strong>Passed:</strong>
56+
<span>[${result.passed}/${result.total}]</span>
57+
</div>`;
58+
header.classList.add(result.all ? 'passed' : 'failed');
59+
node.insertAdjacentElement('afterbegin', header);
60+
document.body.appendChild(node);
61+
}
62+
test_group("Testing 'int'", {
63+
title: "Sending 'int' as 'int'",
64+
type: 'int',
65+
sendValue: 10,
66+
actualValue: 10
67+
},{
68+
title: "Sending 'int' as 'string'",
69+
type: 'int',
70+
sendValue: '10',
71+
actualValue: 10
72+
},{
73+
title: "Sending 'int' as 'string{hex}'",
74+
type: 'int',
75+
sendValue: '0xef',
76+
actualValue: 239
77+
},{
78+
title: "Sending 'null' insteadof 'int'",
79+
type: 'int',
80+
sendValue: null,
81+
okStatus: false,
82+
testValue: false
83+
},{
84+
title: "Sending 'float' insteadof 'int'",
85+
type: 'int',
86+
sendValue: 1.02,
87+
okStatus: false,
88+
testValue: false
89+
},{
90+
title: "Sending 'false' insteadof 'int'",
91+
type: 'int',
92+
sendValue: false,
93+
okStatus: false,
94+
testValue: false
95+
},{
96+
title: "Sending 'true' insteadof 'int'",
97+
type: 'int',
98+
sendValue: true,
99+
actualValue: 1
100+
});
22101
</script>
23102
</body>
24103
</html>

0 commit comments

Comments
 (0)