function executeDir($directory)
 {
     $iterator = new DirectoryIterator($directory);
     while ($iterator->valid()) {
         $entry = $iterator->getFilename();
         $path = $directory . '/' . $entry;
         $iterator->next();
         if ($entry[0] == '.') {
             continue;
         }
         if (is_file($path)) {
             if (substr($entry, -4) != '.php') {
                 continue;
             }
             if (ctype_upper($entry[0])) {
                 $test = new DocTest($path);
                 if ($test->failed()) {
                     echo $test->toString();
                     $this->fail('Doc test failed.');
                 } else {
                     if ($test->numOfPassed()) {
                         echo ',';
                     } else {
                         echo ' ';
                     }
                 }
             }
         } elseif (is_dir($path)) {
             $this->executeDir($path);
         }
     }
 }
 function testBadExample()
 {
     $this->needFile('example_bad.php');
     $test = new DocTest('example_bad.php');
     $this->assertTrue($test->failed());
     $this->assertEquals(10, $test->getLineNumber());
     $this->assertEquals('42', $test->getExpected());
     $this->assertEquals('440', $test->getResult());
 }
Example #3
0
 */
require_once DOCTEST_PATH . 'Parser.php';
/**
 * Import the DocTest_Finder class.
 */
require_once DOCTEST_PATH . 'Finder.php';
/**
 * Import the DocTest_OutputChecker class.
 */
require_once DOCTEST_PATH . 'OutputChecker.php';
/**
 * Import the DocTest_TestResults class.
 */
require_once DOCTEST_PATH . 'TestResults.php';
/**
 * Import the DocTest_Runner class.
 */
require_once DOCTEST_PATH . 'Runner.php';
/**
 * Import the DocTest_DebugRunner class.
 */
require_once DOCTEST_PATH . 'DebugRunner.php';
/**
 * Import the main DocTest class.
 */
require_once DOCTEST_PATH . 'DocTest.php';
/**
 * Register option flag constants.
 */
DocTest::registerOptions();
Example #4
0
 * It must also not be ridiculously large:
 *
 * <code>
 * php > var_dump(factorial(1e100));
 * PHP Fatal error:  Uncaught exception 'OverflowException' with message 
 *     '$n too large'
 * </code>
 */
function factorial($n)
{
    if (!($n >= 0)) {
        throw new InvalidArgumentException('$n must be >= 0');
    }
    if (floor($n) != $n) {
        throw new InvalidArgumentException('$n must be exact integer');
    }
    if ($n + 1 == $n) {
        throw new OverflowException('$n too large');
    }
    $result = 1;
    $factor = 2;
    while ($factor <= $n) {
        $result *= $factor;
        $factor += 1;
    }
    return $result;
}
if (!count(debug_backtrace())) {
    require dirname(__FILE__) . '/../src/DocTest/import.php';
    DocTest::testObj('factorial', null, true);
}
Example #5
0
<?php

// This file is example.php
/**
 * Adds two numbers.
 * Usage example:
 * <code>
 *  echo add(20, 22); /// 42
 * </code>
 */
function add($a, $b)
{
    return $a + $b;
}
if (__FILE__ == realpath($_SERVER['SCRIPT_FILENAME'])) {
    require_once 'DocTest.php';
    $test = new DocTest(__FILE__);
    echo $test->toString();
}
Example #6
0
<?php

error_reporting(E_ALL);
require dirname(__FILE__) . '/../src/DocTest/import.php';
/**
 * Filename relative to this module.
 */
DocTest::testFile('tests.txt', true, null, null, null, true);
/**
 * Relative filename with directory.
 */
//DocTest::testFile('subdir/tests.txt');
/**
 * Absolute path.
 */
//DocTest::testFile('/Users/xdissent/Sites/doctest/examples/tests.txt', false);
/**
 * Use a package base.
 */
//DocTest::testFile('tests.txt', true, null, '/Users/xdissent/Sites/doctest/examples');
/**
 * Use a package base and filename with directory.
 */
//DocTest::testFile('examples/tests.txt', true, null, '/Users/xdissent/Sites/doctest');
/**
 * Get an exception.
 */
try {
    DocTest::testFile('tests.txt', false, null, '/Users/xdissent/Sites/doctest/examples');
} catch (UnexpectedValueException $e) {
}
Example #7
0
 /**
  * Registers available options.
  */
 public static function registerOptions()
 {
     /**
      * Protect against multiple calls.
      */
     if (!is_null(self::$option_flags_by_name)) {
         return;
     }
     /**
      * Initialize options flag array.
      */
     self::$option_flags_by_name = array();
     /**
      * The possible base options.
      */
     $options = array('DONT_ACCEPT_TRUE_FOR_1', 'DONT_ACCEPT_BLANKLINE', 'NORMALIZE_WHITESPACE', 'ELLIPSIS', 'SKIP', 'IGNORE_EXCEPTION_DETAIL', 'REPORT_UDIFF', 'REPORT_NDIFF', 'REPORT_CDIFF', 'REPORT_ONLY_FIRST_FAILURE');
     /**
      * Define a global namespaced constant for each option and add it
      * to the static named options array.
      */
     foreach ($options as $i => $option) {
         $namespaced = 'DOCTEST_' . $option;
         define($namespaced, 1 << $i);
         self::$option_flags_by_name[$option] = constant($namespaced);
     }
     /**
      * Create comparison flags combination.
      */
     $comp_flags = DOCTEST_DONT_ACCEPT_TRUE_FOR_1 | DOCTEST_DONT_ACCEPT_BLANKLINE | DOCTEST_NORMALIZE_WHITESPACE | DOCTEST_ELLIPSIS | DOCTEST_SKIP | DOCTEST_IGNORE_EXCEPTION_DETAIL;
     define('DOCTEST_COMPARISON_FLAGS', $comp_flags);
     /**
      * Create reporting flags combination.
      */
     $rep_flags = DOCTEST_REPORT_UDIFF | DOCTEST_REPORT_CDIFF | DOCTEST_REPORT_NDIFF | DOCTEST_REPORT_ONLY_FIRST_FAILURE;
     define('DOCTEST_REPORTING_FLAGS', $rep_flags);
 }
Example #8
0
 /**
  * Returns a highly visible string used to indicate that an error occurred.
  *
  * @param object $test      The doctest that is running.
  * @param object $example   The current example in the test.
  *
  * @return string
  */
 private function _failureHeader($test, $example)
 {
     $out = array($this->_divider);
     if ($test->filename !== '') {
         if (!is_null($test->lineno) && !is_null($example->lineno)) {
             $lineno = $test->lineno + $example->lineno + 1;
         } else {
             $lineno = '?';
         }
         $out[] = sprintf('File "%s", line %s, in %s', $test->filename, $lineno, $test->name);
     } else {
         $out[] = sprintf('Line %s, in %s', $example->lineno + 1, $test->name);
     }
     $out[] = 'Failed example:';
     $source = $example->source;
     $out[] = DocTest::indent($source);
     return implode("\n", $out);
 }
Example #9
0
 /**
  * Return the differences between expected output and actual output.
  *
  * @param object  $example     The example whose output should be diffed.
  * @param string  $got         The actual output for the example.
  * @param integer $optionflags The options to use when comparing output.
  *
  * @return string
  */
 public function outputDifference($example, $got, $optionflags)
 {
     $want = $example->want;
     /**
      * If <BLANKLINE>s are being used, then replace blank lines
      * with <BLANKLINE> in the actual output string.
      */
     if (!($optionflags & DOCTEST_DONT_ACCEPT_BLANKLINE)) {
         $got = preg_replace('/(?m)^[ ]*(?=\\n)/', DOCTEST_BLANKLINE_MARKER, $got);
     }
     /**
      * Check to see if we should put on our fancy pants.
      */
     if ($this->_doAFancyDiff($want, $got, $optionflags)) {
         throw new Exception('No diff available yet.');
     }
     /**
      * If we're not using diff, then simply list the expected
      * output followed by the actual output.
      */
     if ($want !== '' && $got !== '') {
         return sprintf("Expected:\n%sGot:\n%s", DocTest::indent($want), DocTest::indent($got));
     } elseif ($want !== '') {
         return sprintf("Expected:\n%sGot nothing\n", DocTest::indent($want));
     } elseif ($got !== '') {
         return sprintf("Expected nothing\nGot:\n%s", DocTest::indent($got));
     } else {
         return "Expected nothing\nGot nothing\n";
     }
 }