Skip to content

Commit 436b8a7

Browse files
committed
Implemented phpunit test for language agnostic test cases
Reorganised test folder Fixed string argv input bug to allow new test cases to run
1 parent 82dca31 commit 436b8a7

7 files changed

Lines changed: 155 additions & 56 deletions

File tree

src/docopt.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,14 @@ class TokenStream extends \ArrayIterator
694694

695695
public function __construct($source, $error)
696696
{
697-
if (!is_array($source))
698-
$source = preg_split('/\s+/', trim($source));
699-
697+
if (!is_array($source)) {
698+
$source = trim($source);
699+
if ($source)
700+
$source = preg_split('/\s+/', $source);
701+
else
702+
$source = array();
703+
}
704+
700705
parent::__construct($source);
701706

702707
$this->error = $error;

test/config.php

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/lib/LanguageAgnosticTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
namespace Docopt\Test;
3+
4+
class LanguageAgnosticTest implements \PHPUnit_Framework_Test, \PHPUnit_Framework_SelfDescribing
5+
{
6+
public static function createSuite($testFile)
7+
{
8+
if (!file_exists($testFile))
9+
throw new \InvalidArgumentException("Test file $testFile does not exist");
10+
11+
$suite = new \PHPUnit_Framework_TestSuite;
12+
13+
$raw = file_get_contents($testFile);
14+
$raw = trim(preg_replace("/#.*$/m", "", $raw));
15+
if (strpos($raw, '"""')===0) {
16+
$raw = substr($raw, 3);
17+
}
18+
19+
$idx = 1;
20+
foreach (explode('r"""', $raw) as $fixture) {
21+
if (!$fixture) {
22+
continue;
23+
}
24+
25+
$name = '';
26+
$nIdx = 1;
27+
28+
list ($doc, $body) = explode('"""', $fixture, 2);
29+
$cases = array();
30+
foreach (array_slice(explode('$', $body), 1) as $case) {
31+
$case = trim($case);
32+
list ($argv, $expect) = explode("\n", $case, 2);
33+
$expect = json_decode($expect, true);
34+
35+
$argx = explode(' ', $argv, 2);
36+
$prog = $argx[0];
37+
$argv = isset($argx[1]) ? $argx[1] : "";
38+
39+
$tName = $name ? ($name.$nIdx) : 'unnamed'.$idx;
40+
$suite->addTest(new static($tName, $doc, $prog, $argv, $expect));
41+
$idx++;
42+
}
43+
}
44+
45+
return $suite;
46+
}
47+
48+
public function __construct($name, $doc, $prog, $argv, $expect)
49+
{
50+
$this->doc = $doc;
51+
$this->name = $name;
52+
$this->prog = $prog;
53+
$this->argv = $argv;
54+
55+
if ($expect == "user-error")
56+
$expect = array('user-error');
57+
58+
$this->expect = $expect;
59+
}
60+
61+
public function run(\PHPUnit_Framework_TestResult $result=null)
62+
{
63+
if (!$result)
64+
$result = new \PHPUnit_Framework_TestResult();
65+
66+
$opt = null;
67+
68+
\PHP_Timer::start();
69+
$result->startTest($this);
70+
71+
try {
72+
$opt = \Docopt::handle($this->doc, array('argv'=>$this->argv, 'exit'=>false));
73+
}
74+
catch (\Exception $ex) {
75+
// gulp
76+
}
77+
78+
$found = null;
79+
if ($opt) {
80+
if (!$opt->success) {
81+
$found = array('user-error');
82+
}
83+
elseif (empty($opt->args)) {
84+
$found = [];
85+
}
86+
else {
87+
$found = $opt->args;
88+
}
89+
}
90+
91+
$time = \PHP_Timer::stop();
92+
try {
93+
\PHPUnit_Framework_Assert::assertEquals($this->expect, $found);
94+
}
95+
catch (\PHPUnit_Framework_AssertionFailedError $e) {
96+
$result->addFailure($this, $e, $time);
97+
}
98+
99+
$result->endTest($this, $time);
100+
101+
return $result;
102+
}
103+
104+
public function count()
105+
{
106+
return 1;
107+
}
108+
109+
public function toString()
110+
{
111+
return __CLASS__.'::'.$this->name.' - "'.$this->prog.($this->argv ? ' '.$this->argv : '').'"';
112+
}
113+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Docopt\TokenStream;
1313
use Docopt\Command;
1414

15-
class DocoptTest extends \PHPUnit_Framework_TestCase
15+
class PythonPortedTest extends \PHPUnit_Framework_TestCase
1616
{
1717
/**
1818
* The arguments from the docopt test file are the other way around.

test/phpunit.xml

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/run.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
$basePath = __DIR__;
3+
require $basePath.'/../src/docopt.php';
4+
require_once 'PHPUnit/Autoload.php';
5+
6+
require $basePath.'/lib/PythonPortedTest.php';
7+
require $basePath.'/lib/LanguageAgnosticTest.php';
8+
9+
$options = array(
10+
'py'=>getenv('DOCOPT_PYTHON_PATH'),
11+
'filter'=>null,
12+
);
13+
$options = array_merge(
14+
$options,
15+
getopt('', array('py:', 'filter:'))
16+
);
17+
18+
if (!$options['py']) {
19+
die(
20+
"Please ensure the --py option or the DOCOPT_PYTHON_PATH environment\n".
21+
"variable point to the path of the python port"
22+
);
23+
}
24+
25+
$suite = new PHPUnit_Framework_TestSuite();
26+
$suite->addTest(new PHPUnit_Framework_TestSuite('Docopt\Test\PythonPortedTest'));
27+
$suite->addTest(Docopt\Test\LanguageAgnosticTest::createSuite($options['py'].'/testcases.docopt'));
28+
29+
$runner = new PHPUnit_TextUI_TestRunner();
30+
$runner->doRun($suite, array(
31+
'filter'=>$options['filter'],
32+
'strict'=>true,
33+
));

test/testee.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)