Skip to content

Commit

Permalink
checks that all snippets are executable
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Sep 11, 2016
1 parent e3e9164 commit f6e4003
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 55 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -3,6 +3,7 @@ language: php
php: 5.4

script:
- curl -o reflectivedoc.php http://www.monperrus.net/martin/reflectivedoc.php.txt
- curl -o gakowiki-syntax.php http://www.monperrus.net/martin/gakowiki-syntax.php.txt
- phpunit bibtexbrowser-test.php

Expand Down
34 changes: 29 additions & 5 deletions bibtexbrowser-test.php
Expand Up @@ -10,22 +10,44 @@
(be sure that xdebug is enabled: /etc/php5/cli/conf.d# ln -s ../../mods-available/xdebug.ini)
*/

$_GET['library']=1;
function exception_error_handler($severity, $message, $file, $line) {
if ($severity != E_ERROR) {
//trigger_error($message);
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
error_reporting(E_ALL);

@copy('bibtexbrowser.local.php','bibtexbrowser.local.php.bak');
@unlink('bibtexbrowser.local.php');

require_once ('bibtexbrowser.php');
if(is_file('reflectivedoc.php')) {
set_error_handler("exception_error_handler");
require('reflectivedoc.php');
$_GET['library'] = 1;
foreach(getAllSnippetsInFile('bibtexbrowser.php') as $snippet) {
//echo $snippet;
ob_start();
eval($snippet);
ob_get_clean();
unset($_GET['bib']);
}
restore_error_handler();
} else {
$_GET['library']=1;
require_once ('bibtexbrowser.php');
}

error_reporting(E_ALL);

class BTBTest extends PHPUnit_Framework_TestCase {

function test_checkdoc() {
include('gakowiki-syntax.php');
if(!is_file('gakowiki-syntax.php')) { return; }
if (!function_exists('gk_wiki2html')) { include('gakowiki-syntax.php'); }
create_wiki_parser()->parse(file_get_contents('bibtexbrowser-documentation.wiki'));
}

function createDB() {
return $this->_createDB("@book{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n"
."@book{aKey/withSlash,title={Slash Dangerous for web servers},author={Ap Ache},publisher={Springer},year=2009}\n"
Expand Down Expand Up @@ -578,4 +600,6 @@ function test_removeField() {

} // end class

@copy('bibtexbrowser.local.php.bak','bibtexbrowser.local.php');

?>
102 changes: 52 additions & 50 deletions bibtexbrowser.php
Expand Up @@ -478,7 +478,7 @@ function createMenuManager() { $x = new MenuManager(); return $x;}
<pre>
$delegate = new XMLPrettyPrinter();// or another delegate such as BibDBBuilder
$parser = new StateBasedBibtexParser($delegate);
$parser->parse('foo.bib');
$parser->parse(fopen('bibacid-utf8.bib','r'));
</pre>
notes:
- It has no dependencies, it can be used outside of bibtexbrowser
Expand All @@ -493,6 +493,7 @@ function StateBasedBibtexParser(&$delegate) {
}

function parse($handle) {
if (gettype($handle) == 'string') { throw new Exception('oops'); }
$delegate = &$this->delegate;
// STATE DEFINITIONS
@define('NOTHING',1);
Expand Down Expand Up @@ -714,11 +715,36 @@ function parse($handle) {
} // end function
} // end class

/** is a delegate for StateBasedBibParser.
/** a default empty implementation of a delegate for StateBasedBibtexParser */
class ParserDelegate {

function beginFile() {}

function endFile() {}

function setEntryField($finalkey,$entryvalue) {}

function setEntryType($entrytype) {}

function setEntryKey($entrykey) {}

function beginEntry() {}

function endEntry($entrysource) {}

/** called for each sub parts of type {part} of a field value
* for now, only CURLYTOP and CURLYONE events
*/
function entryValuePart($key, $value, $type) {}

} // end class ParserDelegate


/** is a possible delegate for StateBasedBibParser.
usage:
see snippet of [[#StateBasedBibParser]]
*/
class XMLPrettyPrinter {
class XMLPrettyPrinter extends ParserDelegate {
function beginFile() {
header('Content-type: text/xml;');
print '<?xml version="1.0" encoding="'.OUTPUT_ENCODING.'"?>';
Expand Down Expand Up @@ -764,37 +790,14 @@ function toString() {
} // end class StringEntry


/** a default empty implementation of a delegate for StateBasedBibtexParser */
class ParserDelegate {

function beginFile() {}

function endFile() {}

function setEntryField($finalkey,$entryvalue) {}

function setEntryType($entrytype) {}

function setEntryKey($entrykey) {}

function beginEntry() {}

function endEntry($entrysource) {}

/** called for each sub parts of type {part} of a field value
* for now, only CURLYTOP and CURLYONE events
*/
function entryValuePart($key, $value, $type) {}

} // end class ParserDelegate


/** builds arrays of BibEntry objects from a bibtex file.
usage:
<pre>
$empty_array = array();
$db = new BibDBBuilder(); // see also factory method createBibDBBuilder
$db->build('foo.bib'); // parses foo.bib
$db->build('bibacid-utf8.bib'); // parses bib file
print_r($db->builtdb);// an associated array key -> BibEntry objects
print_r($db->stringdb);// an associated array key -> strings representing @string
</pre>
Expand Down Expand Up @@ -1115,8 +1118,8 @@ function formatAuthor() {
/** represents a bibliographic entry.
usage:
<pre>
$db = zetDB('metrics.bib');
$entry = $db->getEntryByKey('Schmietendorf2000');
$db = zetDB('bibacid-utf8.bib');
$entry = $db->getEntryByKey('classical');
echo bib2html($entry);
</pre>
notes:
Expand Down Expand Up @@ -2631,7 +2634,7 @@ function splitFullName($author){
usage:
<pre>
$_GET['library']=1;
$_GET['bib']='metrics.bib';
$_GET['bib']='bibacid-utf8.bib';
$_GET['all']=1;
include( 'bibtexbrowser.php' );
setDB();
Expand Down Expand Up @@ -2723,7 +2726,7 @@ function javascript_math() {
/** is used for creating menus (by type, by year, by author, etc.).
usage:
<pre>
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$menu = new MenuManager();
$menu->setDB($db);
$menu->year_size=100;// should display all years :)
Expand Down Expand Up @@ -2979,7 +2982,7 @@ function query2title(&$query) {
/** displays the latest modified bibtex entries.
usage:
<pre>
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$d = new NewEntriesDisplay();
$d->setDB($db);
$d->setN(7);// optional
Expand Down Expand Up @@ -3015,7 +3018,7 @@ function display() {
/** displays the entries by year in reverse chronological order.
usage:
<pre>
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$d = new YearDisplay();
$d->setDB($db);
$d->display();
Expand Down Expand Up @@ -3072,7 +3075,7 @@ function display() {
/** displays the summary information of all bib entries.
usage:
<pre>
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$d = new SimpleDisplay();
$d->setDB($db);
$d->display();
Expand Down Expand Up @@ -3237,7 +3240,7 @@ function display() {
/** displays the publication records sorted by publication types (as configured by constant BIBLIOGRAPHYSECTIONS).
usage:
<pre>
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$d = new AcademicDisplay();
$d->setDB($db);
$d->display();
Expand Down Expand Up @@ -3324,8 +3327,8 @@ function display() {
/** displays a single bib entry.
usage:
<pre>
$db = zetDB('metrics.bib');
$dis = new BibEntryDisplay($db->getEntryByKey('Schmietendorf2000'));
$db = zetDB('bibacid-utf8.bib');
$dis = new BibEntryDisplay($db->getEntryByKey('classical'));
$dis->display();
</pre>
notes:
Expand Down Expand Up @@ -3617,7 +3620,7 @@ function metadata_eprints($result) {
usage:
<pre>
$db = new BibDataBase();
$db->load('metrics.bib');
$db->load('bibacid-utf8.bib');
$query = array('author'=>'martin', 'year'=>2008);
foreach ($db->multisearch($query) as $bibentry) { echo $bibentry->getTitle(); }
</pre>
Expand Down Expand Up @@ -4070,9 +4073,9 @@ function bibtexbrowserDefaultCSS() {
/** encapsulates the content of a delegate into full-fledged HTML (&lt;HTML>&lt;BODY> and TITLE)
usage:
<pre>
$db = zetDB('metrics.bib');
$dis = new BibEntryDisplay($db->getEntryByKey('Schmietendorf2000'));
new HTMLTemplate($dis);
$db = zetDB('bibacid-utf8.bib');
$dis = new BibEntryDisplay($db->getEntryByKey('classical'));
HTMLTemplate($dis);
</pre>
* $content: an object with methods
display()
Expand Down Expand Up @@ -4156,9 +4159,9 @@ function HTMLTemplate(&$content) {
/** does nothing but calls method display() on the content.
usage:
<pre>
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$dis = new SimpleDisplay($db);
new NoWrapper($dis);
NoWrapper($dis);
</pre>
*/
function NoWrapper(&$content) {
Expand All @@ -4168,9 +4171,10 @@ function NoWrapper(&$content) {
/** is used to create an subset of a bibtex file.
usage:
<pre>
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$query = array('year'=>2005);
$dis = new BibtexDisplay()->setEntries($db->multisearch($query));
$dis = new BibtexDisplay();
$dis->setEntries($db->multisearch($query));
$dis->display();
</pre>
*/
Expand All @@ -4193,7 +4197,6 @@ function display() {
echo '% '.@$this->title."\n";
echo '% Encoding: '.OUTPUT_ENCODING."\n";
foreach($this->entries as $bibentry) { echo $bibentry->getText()."\n"; }
exit;
}

}
Expand All @@ -4203,7 +4206,7 @@ function display() {
<pre>
$_GET['library']=1;
include( 'bibtexbrowser.php' );
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$pd = new PagedDisplay();
$pd->setEntries($db->bibdb);
$pd->display();
Expand Down Expand Up @@ -4291,7 +4294,7 @@ function menu($less, $more) {
/** is used to create an RSS feed.
usage:
<pre>
$db = zetDB('metrics.bib');
$db = zetDB('bibacid-utf8.bib');
$query = array('year'=>2005);
$rss = new RSSDisplay();
$entries = $db->getLatestEntries(10);
Expand Down Expand Up @@ -4388,8 +4391,7 @@ function display() {
<pre>
$_GET['library']=1;
@require('bibtexbrowser.php');
// simulating ?bib=metrics.bib&year=2009
$_GET['bib']='metrics.bib';
$_GET['bib']='bibacid-utf8.bib';
$_GET['year']='2006';
$x = new Dispatcher();
$x->main();
Expand Down

0 comments on commit f6e4003

Please sign in to comment.