Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: David JEAN LOUIS <izi <at> php.net>
Subject: cvs: pear /Testing_DocTest/Testing/DocTest/Parser Default.php /Testing_DocTest/tests external_file.doctest test1.php
Newsgroups: gmane.comp.php.cvs.pear
Date: Sunday 1st March 2009 10:14:15 UTC (over 8 years ago)
izi		Sun Mar  1 10:14:15 2009 UTC

  Added files:                 
    /pear/Testing_DocTest/tests	external_file.doctest 

  Modified files:              
    /pear/Testing_DocTest/tests	test1.php 
    /pear/Testing_DocTest/Testing/DocTest/Parser	Default.php 
  Log:
  implemented feature request #15963: ability to include standalone doctest
files (thanks to Sofia Cardita for the patch !)
  
http://cvs.php.net/viewvc.cgi/pear/Testing_DocTest/tests/test1.php?r1=1.1&r2=1.2&diff_format=u
Index: pear/Testing_DocTest/tests/test1.php
diff -u pear/Testing_DocTest/tests/test1.php:1.1
pear/Testing_DocTest/tests/test1.php:1.2
--- pear/Testing_DocTest/tests/test1.php:1.1	Wed Dec 17 16:15:04 2008
+++ pear/Testing_DocTest/tests/test1.php	Sun Mar  1 10:14:15 2009
@@ -24,7 +24,7 @@
  * @author    David JEAN LOUIS 
  * @copyright 2008 David JEAN LOUIS
  * @license   http://opensource.org/licenses/mit-license.php
MIT License 
- * @version   CVS: $Id: test1.php,v 1.1 2008/12/17 16:15:04 izi Exp $
+ * @version   CVS: $Id: test1.php,v 1.2 2009/03/01 10:14:15 izi Exp $
  * @link      http://pear.php.net/package/Testing_DocTest
  * @since     Class available since release 0.1.0
  * @filesource
@@ -494,6 +494,35 @@
 }
 
 /**
+ * A simple function that multiply two int or float and return a float
number.
+ * It throws an exception if arguments given have a wrong type.
+ * 
+ * This example shows the use of an external doctest file.
+ *
+ * 
+ * // test-file: tests/external_file.doctest
+ * 
+ *
+ * @param mixed $a an int or a float
+ * @param mixed $b an int or a float
+ *
+ * @return float the result of the multiplication
+ * @throws Exception if arguments given have a wrong type
+ */
+function multiply2($a, $b)
+{
+    // check first arg type
+    if (!is_int($a) && !is_float($a)) {
+        throw new Exception("Wrong type for first argument.");
+    }
+    // check second arg type
+    if (!is_int($b) && !is_float($b)) {
+        throw new Exception("Wrong type for second argument.");
+    }
+    return (float)($a * $b);
+}
+
+/**
  * A simple function that return a simple or multidimensional array.
  *
  * 
http://cvs.php.net/viewvc.cgi/pear/Testing_DocTest/Testing/DocTest/Parser/Default.php?r1=1.1&r2=1.2&diff_format=u
Index: pear/Testing_DocTest/Testing/DocTest/Parser/Default.php
diff -u pear/Testing_DocTest/Testing/DocTest/Parser/Default.php:1.1
pear/Testing_DocTest/Testing/DocTest/Parser/Default.php:1.2
--- pear/Testing_DocTest/Testing/DocTest/Parser/Default.php:1.1	Wed Dec 17
16:15:04 2008
+++ pear/Testing_DocTest/Testing/DocTest/Parser/Default.php	Sun Mar  1
10:14:15 2009
@@ -16,7 +16,7 @@
  * @author    David JEAN LOUIS 
  * @copyright 2008 David JEAN LOUIS
  * @license   http://opensource.org/licenses/mit-license.php
MIT License 
- * @version   CVS: $Id: Default.php,v 1.1 2008/12/17 16:15:04 izi Exp $
+ * @version   CVS: $Id: Default.php,v 1.2 2009/03/01 10:14:15 izi Exp $
  * @link      http://pear.php.net/package/Testing_DocTest
  * @since     File available since release 0.1.0
  * @filesource
@@ -39,7 +39,7 @@
  * @author    David JEAN LOUIS 
  * @copyright 2008 David JEAN LOUIS
  * @license   http://opensource.org/licenses/mit-license.php
MIT License 
- * @version   Release: @package_version@
+ * @version   Release: 0.3.1
  * @link      http://pear.php.net/package/Testing_DocTest
  * @since     Class available since release 0.1.0
  */
@@ -56,6 +56,11 @@
     // Keywords constants {{{
 
     /**
+     * Keyword for the name of the external doctest file
+     */
+    const KW_DOCTEST_FILE = 'test-file';
+    
+    /**
      * Keyword for the name of the doctest
      */
     const KW_DOCTEST_NAME = 'doctest';
@@ -392,11 +397,19 @@
         $ret = array();
         // extract  blocks, we use preg_match_all because
there 
         // could be more than one code block by docstring
-        $rx =
'/[\s\*]*(<[\?\%](php)?)?(.*?)([\?\%]>)?[\s\*]*<\/code>/si';
+        $rx = '/[\s\*]*(<[\?\%](php)?)?\s*' 
+            . '(.*?)\s*([\?\%]>)?[\s\*]*<\/code>/si';
         preg_match_all($rx, $docstring, $tokens);
         if (isset($tokens[3]) && is_array($tokens[3])) {
             foreach ($tokens[3] as $i => $token) {
-                $token = trim($token);
+                if ($this->_hasStandaloneDoctest($token)) {
+                    $testfile_contents =
$this->_handleStandaloneDoctest($token);
+                    if ($testfile_contents !== false) {
+                        // replace the current doctest code with the
contents
+                        // of the external included file
+                        $token = $testfile_contents;
+                    }
+                }
                 if (!$this->_hasDocTest($token)) {
                     // not a doctest
                     continue;
@@ -408,6 +421,23 @@
     }
 
     // }}}
+    // _hasStandaloneDoctest() {{{
+
+    /**
+     * Return true if the string data provided contains an external
doctest file.
+     *
+     * @param string $data The docstring data
+     *
+     * @return boolean
+     */
+    private function _hasStandaloneDoctest($data)
+    {
+        $p = preg_quote(self::SYNTAX_PREFIX, '/');
+        $k = preg_quote(self::KW_DOCTEST_FILE, '/');
+        return preg_match("/$p\s?$k/m", $data);
+    }
+
+    // }}}
     // _hasDocTest() {{{
 
     /**
@@ -426,6 +456,41 @@
     }
 
     // }}}
+    // _handleStandaloneDoctest() {{{
+
+    /**
+     * Return the contents of the external doctest file.
+     *
+     * @param string $docbloc The docstring data
+     *
+     * @return mixed boolean or string
+     */
+    private function _handleStandaloneDoctest($docbloc)
+    {
+        $p     = preg_quote(self::SYNTAX_PREFIX, '/');
+        $k     = preg_quote(self::KW_DOCTEST_FILE, '/');
+        $lines = preg_split('/(\n|\r\n)/', $docbloc);
+
+        foreach ($lines as $i => $l) {
+            $l = preg_replace('/^\s*\*\s?/', '', $l);
+            $p = preg_quote(self::SYNTAX_PREFIX, '/');
+            if (preg_match("/^\s*$p\s?($k):\s*(.*)$/", $l, $matches)) {
+                $f = trim($matches[2]);
+                if (false === ($contents =
@file_get_contents(realpath($f)))) {
+                    throw new Testing_DocTest_Exception(
+                        "Unable to read standalone doctest file \"$f\""
+                    );
+                }
+                // remove the php tags
+                $rx = '/(<[\?\%](php)?)?(.*?)([\?\%]>)?/si';
+                return preg_replace($rx, '\3', $contents);
+            }
+        }
+        return false;
+            
+    }
+
+    // }}}
     // _handleDoctestLine() {{{
 
     /**

http://cvs.php.net/viewvc.cgi/pear/Testing_DocTest/tests/external_file.doctest?view=markup&rev=1.1
Index: pear/Testing_DocTest/tests/external_file.doctest
+++ pear/Testing_DocTest/tests/external_file.doctest
getMessage() . "\n";
}
try {
    multiply2(3.2, 'foo');
} catch (Exception $exc) {
    echo $exc->getMessage() . "\n";
}
// expects:
// 12.00
// 12.80
// 13.44
// Wrong type for first argument.
// Wrong type for second argument.

?>



-- 
PEAR CVS Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
 
CD: 51ms