-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaiixtest.php
629 lines (550 loc) · 19.7 KB
/
aiixtest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/usr/bin/env php
<?php
/*
* (C) 2015, AII (Alexey Ilyin).
*/
class AIIXTestContext
{
private $filepath;
private $cwd;
/**
* @var string
*/
public $fname;
/**
* @var array
*/
public $code;
/**
* @var AIIXTestResult
*/
public $return;
/**
* @var array
*/
public $vars;
public $marks = array();
private $forceResultClass;
private $defaultResultClass = 'AIIXTestResult';
public function skipChecking ($resultClass, $force = false) {
$this->defaultResultClass = $resultClass;
$this->forceResultClass = $force;
}
public function setFilepath ($fname) {
if (!$this->filepath = realpath($fname)) {
echo "\n\n***** ERROR: File $this->cwd/$fname not found and so that skipped\n\n";
return false;
}
$this->fname = $fname;
$this->code = file($this->filepath);
return true;
}
public function printCode () {
if (is_array($this->code)) foreach ($this->code as $lnum => $ltxt) {
printf("%4d| %s", $lnum+1, $ltxt);
}
}
public function exec () {
extract(AIIXTest::$SHARED, EXTR_PREFIX_INVALID|EXTR_REFS, 'var_');
try {
$return = include $this->filepath;
}
catch (Exception $e) {
echo "\n$e\n";
$return = $e;
unset($e);
}
$this->return = $this->makeReturn($return);
unset($return);
$this->vars = get_defined_vars();
unset($this->vars['this']); // sic! 'this' could really be there
}
protected function makeReturn ($return) {
$class = $this->defaultResultClass;
if ($this->forceResultClass) {
return new $class($return);
}
return $return instanceof AIIXTestResult ?
$return : new $class($return);
}
public function test () {
return $this->result = $this->return->check($this,
AIIXTest::CACHE.'/'.basename($this->fname).'.txt');
}
public function mark ($key = false) {
$mark = array(microtime(true), memory_get_usage());
if ($key !== false) {
isset($this->marks[$key]) and $key .= count($this->marks);
$this->marks[$key] = $mark;
}
else {
$this->marks[] = $mark;
}
}
}
class AIIXTestResult
{
public $result, $expected;
public function __construct ($result) {
$this->result = $result;
}
public function check (AIIXTestContext $test, $filename) {
$current = serialize($this->result);
if (!file_exists($filename)) {
file_put_contents($filename, $current);
return true;
}
$previous = file_get_contents($filename);
if ($current === $previous) return true;
$this->expected = unserialize($previous);
return false;
}
public function show ($passed) {
return !$passed;
}
}
class AIIXTestResultAssertion extends AIIXTestResult
{
public function check (AIIXTestContext $test, $filename) {
return !array_filter((array)$this->result, array($this, 'isEmpty')); //???
}
protected function isEmpty ($result) {
return empty($result);
}
}
class AIIXTestResultIsTrue extends AIIXTestResultAssertion
{
protected function isEmpty ($result) {
if (is_bool($result)) return $result !== true;
echo("***** is_true(): got ".gettype($result).", not boolean\n"); //TODO
return true;
}
}
class AIIXTestResultReplacePrev extends AIIXTestResult
{
public function check (AIIXTestContext $test, $filename) {
file_put_contents($filename, serialize($this->result));
return true;
}
}
class AIIXTestResultDeletePrev extends AIIXTestResult
{
public function check (AIIXTestContext $test, $filename) {
file_exists($filename) and unlink($filename);
return true;
}
}
class AIIXTest
{
public static function assertion () {
return new AIIXTestResultAssertion(func_get_args());
}
public static function is_true () {
return new AIIXTestResultIsTrue(func_get_args());
}
public static function replace_previous () {
return new AIIXTestResultReplacePrev(func_get_args());
}
public static function delete_previous () {
return new AIIXTestResultDeletePrev(func_get_args());
}
protected $init = array(), $test = array(), $path, $cwd;
protected static $inst;
public static $SHARED = array();
protected function __construct () {
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
$this->prev_assert_bail = assert_options(ASSERT_BAIL, 0);
$this->prev_asrhandler = assert_options(ASSERT_CALLBACK, array($this,'asrhandler'));
$this->prev_errhandler = set_error_handler(array($this,'errhandler'));
//$this->prev_exphandler = set_exception_handler(array($this,'exphandler'));
register_shutdown_function(array($this,'shthandler'));
$this->parseCmdLine();
}
protected function parseCmdLine () {
$this->cwd = getcwd();
global $argv;
$stage = false;
while ($arg = next($argv)) {
switch ($arg) {
case 'init': case 'test':
$stage = $arg;
$arg = next($argv);
break;
}
if ($arg[0] === '-') {
for ($i = 1, $n = strlen($arg); $i < $n; ++$i) {
if (!isset($this->sw[$arg[$i]])) {
echo "unknown switch -{$arg[$i]} skipped\n";
continue;
}
$this->sw[$arg[$i]] = $arg[$i];
}
}
else if (!$stage) {
$this->path
and $this->abort("on $arg - path $this->path is already set");
$this->path = realpath($arg)
or $this->abort("path $arg does not exist");
chdir($this->path)
or $this->abort("cannot chdir($this->path)");
$this->cwd = getcwd();
}
else {
$this->addFile($stage, $arg);
}
}
}
protected function filename ($stage, $fname) {
$file = pathinfo($fname);
isset($file['dirname'])
or $this->abort("no file specified for $stage");
$file['dirname'] !== '.'
or $file['dirname'] = $stage;
empty($file['extension'])
and $file['extension'] = 'php';
return "$file[dirname]/$file[filename].$file[extension]";
}
protected function addFile ($stage, $fname) {
$fname = $this->filename ($stage, $fname);
file_exists($fname)
or $this->abort("file $fname does not exists");
isset($this->{$stage}[$fname])
or $this->{$stage}[$fname] = $this->prepare($fname);
}
protected function getContext ($stage, $fname) {
$fname = $this->filename ($stage, $fname);
if (isset($this->{$stage}[$fname])) {
return $this->{$stage}[$fname];
}
echo ("\n***** file $fname is not loaded\n");
return false;
}
protected function prepare ($fname) {
$context = new AIIXTestContext;
if (!$context->setFilepath($fname)) return false;
$this->evalPragmas('prepare', $context);
if ($this->sw[self::SW_RETURN_DELETE]) {
$context->skipChecking('AIIXTestResultDeletePrev', $this->sw[self::SW_RETURN_FORCE]);
}
elseif ($this->sw[self::SW_RETURN_IGNORE]) {
$context->skipChecking('AIIXTestResultReplacePrev', $this->sw[self::SW_RETURN_FORCE]);
}
return $context;
}
protected function evalPragmas ($phase, AIIXTestContext $context) {
foreach ($context->code as $lnum => $line) {
$lnum++;
$parts = explode('//!', trim($line));
if (count($parts) == 2) {
$code = $parts[1];
}
else if (count($parts) > 2) {
$this->abort("Bad-formed pragma(s) in '$phase' of $context->fname:\n$lnum| {$line}");
}
else continue;
if ($this->evaluate('return $this->'.$phase.'_'.trim($code).';') === false) {
return false;
}
}
}
private function evaluate ($code) {
return eval($code);
}
/**
*
* @param type $path
* @param type $events
* @return AIIXTest
*/
public static function create () {
error_reporting(0);
ini_set('display_errors', false);
ini_set('display_startup_errors', false);
return self::$inst = new AIIXTest();
}
const SLINE =
'--------------------------------------------------------------------------------';
const DLINE =
'o==============================================================================o';
const CACHE = '.cache';
const SW_HIDE_INIT_OUTPUT = 'I';
const SW_HIDE_INIT_VARS = 'W';
const SW_HIDE_TEST_OUTPUT = 'T';
const SW_HIDE_TEST_VARS = 'V';
const SW_HIDE_TEST_CODE = 'C';
const SW_HIDE_TEST_RETURN = 'R';
const SW_SHORT_HEADING = 'H';
const SW_FAILED_ONLY = 'X';
const SW_RETURN_DELETE = 'd';
const SW_RETURN_IGNORE = 'i';
const SW_RETURN_FORCE = 'f';
private $sw = array(
self::SW_HIDE_INIT_OUTPUT => false,
self::SW_HIDE_INIT_VARS => false,
self::SW_HIDE_TEST_OUTPUT => false,
self::SW_HIDE_TEST_VARS => false,
self::SW_HIDE_TEST_CODE => false,
self::SW_HIDE_TEST_RETURN => false,
self::SW_SHORT_HEADING => false,
self::SW_RETURN_DELETE => false,
self::SW_RETURN_IGNORE => false,
self::SW_RETURN_FORCE => false
);
public function start () {
is_dir(self::CACHE) or mkdir(self::CACHE);
echo 'PHP ',PHP_VERSION,"\n";
$this->run('init');
if (empty($this->sw[self::SW_HIDE_INIT_VARS])) {
echo "\nInitial Vars: \n";
$this->printVars(self::$SHARED);
}
echo "\n";
$this->run('test');
}
protected function run ($stage) {
if (empty($this->{$stage})) {
foreach(glob("$stage/*.php") as $fname) {
$this->addFile($stage, $fname);
}
}
foreach ($this->{$stage} as $context) {
if (!$context) continue; // skipped
$method = "print_$stage";
empty($this->sw[self::SW_FAILED_ONLY])
or $method .= 'x';
$this->{$method}($context);
}
}
protected function print_init (AIIXTestContext $init) {
if (empty($this->sw[self::SW_SHORT_HEADING])) {
printf("----%'--72s----\n", "[ $init->fname ]");
}
else {
echo $init->fname;
}
$this->execInit($init, !empty($this->sw[self::SW_HIDE_INIT_OUTPUT]));
if (!empty($this->sw[self::SW_SHORT_HEADING])) {
echo "\n";
}
return $init;
}
protected function print_initx (AIIXTestContext $init) {
$this->execInit($init, true);
return $init;
}
protected function execInit (AIIXTestContext $init, $quiet = false) {
$temp = $this->evalPragmas('return', $init);
unset($init->code);
if ($temp === false)
$init = false;
else {
$quiet and ob_start();
$init->exec();
$quiet and ob_clean(); //TODO on failure ???
self::$SHARED = array_merge(self::$SHARED, $init->vars);
}
}
private function printTestHeading (AIIXTestContext $test) {
if (empty($this->sw[self::SW_SHORT_HEADING])) {
echo "\n\n",self::DLINE,"\n";
printf("| %' -72s |", $test->fname);
echo "\n",self::DLINE,"\n";
return true;
}
else {
echo $test->fname, ' ';
return false;
}
}
protected function print_test (AIIXTestContext $test) {
$short = !$this->printTestHeading($test);
if (empty($this->sw[self::SW_HIDE_TEST_CODE])) {
$short and print "\n".self::SLINE."\n";
$test->printCode();
}
if (!is_null($passed = $this->execTest($test,
!empty($this->sw[self::SW_HIDE_TEST_OUTPUT])))) {
if (empty($this->sw[self::SW_HIDE_TEST_RETURN])) {
if ($test->return->show($passed)) {
$short and print "\n";
echo "RETURNED: ";
var_dump($test->return->result);
}
if (!$passed) {
echo "EXPECTED: ";
var_dump($test->return->expected);
echo self::SLINE,"\n";
}
}
else if (!$passed) {
echo "FAILED!\n";
}
else {
echo "\n";
}
if (empty($this->sw[self::SW_HIDE_TEST_VARS])) {
echo "Vars after:\n";
$this->printVars($test->vars, true);
}
if ($test->marks) {
$this->printMarks($test->marks);
}
}
if (!empty($this->sw[self::SW_SHORT_HEADING])) {
echo "\n";
}
return $test;
}
protected function print_testx (AIIXTestContext $test) {
if (!is_null($passed = $this->execTest($test, true))) {//sic!
if (empty($this->sw[self::SW_HIDE_TEST_RETURN])) {
if ($test->return->show($passed)) {
$short = !$this->printTestHeading($test);
if (empty($this->sw[self::SW_HIDE_TEST_CODE])) {
$short and print "\n".self::SLINE."\n";
$test->printCode();
echo "\n",self::SLINE;
}
echo "\nRETURNED: ";
var_dump($test->return->result);
}
if (!$passed) {
echo "EXPECTED: ";
var_dump($test->return->expected);
echo self::SLINE,"\n";
}
}
else if (!$passed) {
echo $test->fname, " FAILED!\n";
}
}
return $test;
}
protected function execTest (AIIXTestContext $test, $quiet = false) {
if ($this->evalPragmas('result', $test) === false) {
echo "\nSkipped because of dependencies\n";
return null;
}
else {
if ($quiet) ob_start();
else echo "\n",self::SLINE,"\n";
$test->exec();
if ($quiet) ob_clean();
else echo "\n",self::SLINE,"\n";
return $test->test();
}
}
protected function printVars (array $vars, $skip = false) {
foreach ($vars as $key => $value) {
if ($skip && isset($key[0]) && $key[0] === '_') continue;
echo "\n";
isset(self::$SHARED[$key]) and print('shared ');
echo "$$key: ";
var_dump($value);
// var_export($value); echo ";\n";
}
}
protected function printMarks (array $marks) {
echo "\n";
list($time, $mem) = array_shift($marks);
foreach ($marks as $key => $mark) {
list($t, $m) = $mark;
list($tp, $td) = $this->scale($t - $time, 0.1, 10, 1000, array('s', 'ms', 'mks', 'ns', 'ps', 'fs'));
list($mp, $md) = $this->scale($m, 1000, 102.4, 1024, array('b', 'kb', 'mb', 'gb', 'tb', 'pb'));
list($dp, $dd) = $this->scale($m - $mem, 100, 10.24, 1024, array('b', 'kb', 'mb', 'gb', 'tb', 'pb'));
printf("%6.2f %3s; %9.2f %2s (%9.2f %2s) '%s'\n",
$tp, $td, $mp, $md, $dp, $dd, $key
);
$time = $t;
$mem = $m;
}
}
private function scale ($number, $estim, $base, $factor, array $labels) {
// assert('$estim > 0 && $base > 0');
$i = 0;
settype($number, 'double');
settype($base, 'double');
$dir = $estim - $base;
if ($dir > 0) {
$base *= $factor;
while (abs($number) >= $base) {
$number = $number / $factor;
++$i;
}
}
elseif ($dir < 0) {
$base /= $factor;
while (abs($number) <= $base) {
$number = $number * $factor;
++$i;
}
}
return array($number, $labels[$i]);
}
public function prepare_require_init ($fname) {
$this->addFile('init', $fname);
}
public function prepare_require_test ($fname) {
$this->addFile('test', $fname);
}
public function return_require_init ($fname) {
$init = $this->getContext('init', $fname);
return !empty($init->return);
}
public function result_require_test ($fname) {
$test = $this->getContext('test', $fname);
return !empty($test->result);//TODO ???
}
protected function abort ($message) {
exit ("\nAborted: $message\n");
}
protected $prev_errhandler = 'undefined', $prev_asrhandler = 'undefined';
// function exphandler ($exception) {
// $this->errhandler(null, (string)$exception);
// }
function asrhandler($file, $line, $code) {
$msg = $this->errmessage(null, $code, $file, $line);
print("\n***** Assertion failed: $msg\n");
}
function errhandler ($errno, $errstr, $errfile=null, $errline=null, $errcontext=null) {
$msg = $this->errmessage($errno, $errstr, $errfile, $errline, $errcontext);
print("\n***** $msg\n");
return true;
}
function shthandler () {
echo str_repeat('-', 80), "\n";
if ($error = error_get_last()) {
$this->errhandler($error['type'], $error['message'], $error['file'], $error['line']);
exit("ABORTED\n");
}
exit("THE END\n");
}
function errmessage ($errno, $errstr, $errfile = null, $errline = null, $errcontext = null) {
isset($errno) and $errstr = static::$errorNames[$errno]." ($errno): $errstr";
isset($errfile) and $errstr .= " in ".str_replace($this->cwd, '', $errfile);
isset($errline) and $errstr .= " on $errline";
return $errstr;
}
protected static $errorNames = array(
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parse Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Strict',
E_RECOVERABLE_ERROR => 'Recoverable Error',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User Deprecated',
E_ALL => 'All Errors'
);
}
//---=====[ TEST ]=====---//
AIIXTest::create()->start();