Guest User

Untitled

a guest
Oct 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. <?php
  2.  
  3. $__TEST__COUNT__ = 0;
  4. $__TEST__SUCCESS__ = 0;
  5. $__TEST__FAILURE__ = 0;
  6. $__EXPECTED__TESTS__ = null;
  7.  
  8. function no_plan() {
  9. global $__EXPECTED__TESTS__;
  10. $__EXPECTED__TESTS__ = 'none';
  11. }
  12.  
  13. function plan($tests) {
  14. global $__EXPECTED__TESTS__;
  15.  
  16. $__EXPECTED__TESTS__ = $tests;
  17. echo "# planned to run $tests tests\n";
  18. echo "1..$tests\n";
  19. }
  20.  
  21. function finalize() {
  22. global $__TEST__COUNT__, $__TEST__SUCCESS__, $__TEST__FAILURE__,
  23. $__EXPECTED__TESTS__;
  24.  
  25. if ($__EXPECTED__TESTS__ == 'none')
  26. exit(0);
  27.  
  28. $cnt = $__TEST__COUNT__;
  29. $pass = $__TEST__SUCCESS__;
  30. $fail = $__TEST__FAILURE__;
  31. $exp = $__EXPECTED__TESTS__;
  32. $all = $cnt === $exp;
  33.  
  34. if ($exp !== null) {
  35. if (!$all)
  36. echo "# planned $exp tests but only executed $cnt\n";
  37. if ($fail)
  38. echo "# $fail tests failed, $pass passed\n";
  39. }
  40. else {
  41. echo "# no test plan - $pass passed, $fail failed\n";
  42. }
  43.  
  44. exit($fail);
  45. }
  46.  
  47. function notate($message) {
  48. $lines = explode("\n", $message);
  49. foreach ($lines as $line) {
  50. echo "# $line\n";
  51. }
  52. }
  53.  
  54. function ok($cond, $message = null) {
  55. global $__TEST__COUNT__, $__TEST__SUCCESS__, $__TEST__FAILURE__;
  56. $__TEST__COUNT__++;
  57.  
  58. if ($message) {
  59. $lines = explode("\n", $message);
  60. $message = " - " . $lines[0];
  61. }
  62.  
  63. if ($cond) {
  64. $__TEST__SUCCESS__++;
  65. echo "ok $__TEST__COUNT__$message\n";
  66. }
  67. else {
  68. $__TEST__FAILURE__++;
  69. echo "not ok $__TEST__COUNT__$message\n";
  70. }
  71. }
  72.  
  73. function not_ok($cond, $message = null) {
  74. ok(!$cond, $message);
  75. }
  76.  
  77. function is($left, $right, $message = null) {
  78. ok($left === $right, $message);
  79. }
  80.  
  81. function is_not($left, $right, $message = null) {
  82. not_ok($left === $right, $message);
  83. }
  84.  
  85. function parse_contents($name, $tests) {
  86. $lines = explode("\n", $tests);
  87. $current = 0;
  88. $pass = 0;
  89. $fail = 0;
  90.  
  91. foreach ($lines as $lineno => $line) {
  92. if ($line == '')
  93. continue;
  94. else if (preg_match('/^(\d+)\.\.(\d+)$/', $line, $matches)) {
  95. $from = $matches[1];
  96. $to = $matches[2];
  97.  
  98. $count = ($to - $from) + 1;
  99. }
  100. else if (preg_match('/^ok (\d+)/', $line, $matches)) {
  101. $current++;
  102. $pass++;
  103.  
  104. $test = $matches[1];
  105. if ($test != $current)
  106. echo "# bad test! $name test $test (expected $current)!\n";
  107. }
  108. else if (preg_match('/^not ok (\d+)/', $line, $matches)) {
  109. $current++;
  110. $fail++;
  111.  
  112. $test = $matches[1];
  113. if ($test != $current)
  114. echo "# bad test! $name test $test (expected $current)!\n";
  115. }
  116. else if (!preg_match('/^\#/', $line)) {
  117. echo "# unknown output format $line ($name:$lineno)\n";
  118. }
  119. }
  120.  
  121. echo "# $name $pass/$count\n";
  122.  
  123. if ($pass > $count)
  124. $pass = $count;
  125.  
  126. return "$pass/$fail/$count";
  127. }
  128.  
  129. register_shutdown_function('finalize');
  130.  
  131. ?>
Add Comment
Please, Sign In to add comment