Просмотр файла news/libraries/test.php

Размер файла: 1.48Kb
  1. <?php
  2.  
  3. /*
  4. Copyright (c) 2009-2014 F3::Factory/Bong Cosca, All rights reserved.
  5.  
  6. This file is part of the Fat-Free Framework (http://fatfree.sf.net).
  7.  
  8. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  9. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  11. PURPOSE.
  12.  
  13. Please see the license.txt file for more information.
  14. */
  15.  
  16. //! Unit test kit
  17. class Test {
  18.  
  19. //@{ Reporting level
  20. const
  21. FLAG_False=0,
  22. FLAG_True=1,
  23. FLAG_Both=2;
  24. //@}
  25.  
  26. protected
  27. //! Test results
  28. $data=array();
  29.  
  30. /**
  31. * Return test results
  32. * @return array
  33. **/
  34. function results() {
  35. return $this->data;
  36. }
  37.  
  38. /**
  39. * Evaluate condition and save test result
  40. * @return object
  41. * @param $cond bool
  42. * @param $text string
  43. **/
  44. function expect($cond,$text=NULL) {
  45. $out=(bool)$cond;
  46. if ($this->level==$out || $this->level==self::FLAG_Both) {
  47. $data=array('status'=>$out,'text'=>$text,'source'=>NULL);
  48. foreach (debug_backtrace() as $frame)
  49. if (isset($frame['file'])) {
  50. $data['source']=Base::instance()->
  51. fixslashes($frame['file']).':'.$frame['line'];
  52. break;
  53. }
  54. $this->data[]=$data;
  55. }
  56. return $this;
  57. }
  58.  
  59. /**
  60. * Append message to test results
  61. * @return NULL
  62. * @param $text string
  63. **/
  64. function message($text) {
  65. $this->expect(TRUE,$text);
  66. }
  67.  
  68. /**
  69. * Class constructor
  70. * @return NULL
  71. * @param $level int
  72. **/
  73. function __construct($level=self::FLAG_Both) {
  74. $this->level=$level;
  75. }
  76.  
  77. }