Rearrange legacy tests.

This commit is contained in:
Justin Hileman
2012-02-29 18:54:05 -08:00
parent 1d116d1b77
commit 8710e4b8a4
12 changed files with 130 additions and 247 deletions
@@ -0,0 +1,130 @@
<?php
namespace Mustache\Test\Functional;
use Mustache\Mustache;
class ExamplesTest extends \PHPUnit_Framework_TestCase {
/**
* Test everything in the `examples` directory.
*
* @group examples
* @dataProvider getExamples
*
* @param string $context
* @param string $source
* @param array $partials
* @param string $expected
*/
public function testExamples($context, $source, $partials, $expected) {
$mustache = new Mustache(array(
'partials' => $partials
));
$this->assertEquals($expected, $mustache->loadTemplate($source)->render($context));
}
/**
* Data provider for testExamples method.
*
* Assumes that an `examples` directory exists inside parent directory.
* This examples directory should contain any number of subdirectories, each of which contains
* three files: one Mustache class (.php), one Mustache template (.mustache), and one output file
* (.txt).
*
* This whole mess will be refined later to be more intuitive and less prescriptive, but it'll
* do for now. Especially since it means we can have unit tests :)
*
* @return array
*/
public function getExamples() {
$path = realpath(__DIR__.'/../../../../examples');
$examples = array();
$handle = opendir($path);
while (($file = readdir($handle)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$fullpath = $path.'/'.$file;
if (is_dir($fullpath)) {
$examples[$file] = $this->loadExample($fullpath);
}
}
closedir($handle);
return $examples;
}
/**
* Helper method to load an example given the full path.
*
* @param string $path
* @return array arguments for testExamples
*/
private function loadExample($path) {
$context = null;
$source = null;
$partials = array();
$expected = null;
$handle = opendir($path);
while (($file = readdir($handle)) !== false) {
$fullpath = $path.'/'.$file;
$info = pathinfo($fullpath);
if (is_dir($fullpath) && $info['basename'] == 'partials') {
// load partials
$partials = $this->loadPartials($fullpath);
} elseif (is_file($fullpath)) {
// load other files
switch ($info['extension']) {
case 'php':
require_once($fullpath);
$context = new $info['filename'];
break;
case 'mustache':
$source = file_get_contents($fullpath);
break;
case 'txt':
$expected = file_get_contents($fullpath);
break;
}
}
}
closedir($handle);
return array($context, $source, $partials, $expected);
}
/**
* Helper method to load partials given an example directory.
*
* @param string $path
*
* @return array $partials
*/
private function loadPartials($path) {
$partials = array();
$handle = opendir($path);
while (($file = readdir($handle)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$fullpath = $path.'/'.$file;
$info = pathinfo($fullpath);
if ($info['extension'] === 'mustache') {
$partials[$info['filename']] = file_get_contents($fullpath);
}
}
closedir($handle);
return $partials;
}
}
@@ -1,152 +0,0 @@
<?php
require_once '../Mustache.php';
class MustacheExceptionTest extends PHPUnit_Framework_TestCase {
const TEST_CLASS = 'Mustache';
protected $pickyMustache;
protected $slackerMustache;
public function setUp() {
$this->pickyMustache = new PickyMustache();
$this->slackerMustache = new SlackerMustache();
}
/**
* @group interpolation
* @expectedException MustacheException
*/
public function testThrowsUnknownVariableException() {
$this->pickyMustache->render('{{not_a_variable}}');
}
/**
* @group sections
* @expectedException MustacheException
*/
public function testThrowsUnclosedSectionException() {
$this->pickyMustache->render('{{#unclosed}}');
}
/**
* @group sections
* @expectedException MustacheException
*/
public function testThrowsUnclosedInvertedSectionException() {
$this->pickyMustache->render('{{^unclosed}}');
}
/**
* @group sections
* @expectedException MustacheException
*/
public function testThrowsUnexpectedCloseSectionException() {
$this->pickyMustache->render('{{/unopened}}');
}
/**
* @group partials
* @expectedException MustacheException
*/
public function testThrowsUnknownPartialException() {
$this->pickyMustache->render('{{>impartial}}');
}
/**
* @group pragmas
* @expectedException MustacheException
*/
public function testThrowsUnknownPragmaException() {
$this->pickyMustache->render('{{%SWEET-MUSTACHE-BRO}}');
}
/**
* @group sections
*/
public function testDoesntThrowUnclosedSectionException() {
$this->assertEquals('', $this->slackerMustache->render('{{#unclosed}}'));
}
/**
* @group sections
*/
public function testDoesntThrowUnexpectedCloseSectionException() {
$this->assertEquals('', $this->slackerMustache->render('{{/unopened}}'));
}
/**
* @group partials
*/
public function testDoesntThrowUnknownPartialException() {
$this->assertEquals('', $this->slackerMustache->render('{{>impartial}}'));
}
/**
* @group pragmas
* @expectedException MustacheException
*/
public function testGetPragmaOptionsThrowsExceptionsIfItThinksYouHaveAPragmaButItTurnsOutYouDont() {
$mustache = new TestableMustache();
$mustache->testableGetPragmaOptions('PRAGMATIC');
}
public function testOverrideThrownExceptionsViaConstructorOptions() {
$exceptions = array(
MustacheException::UNKNOWN_VARIABLE,
MustacheException::UNCLOSED_SECTION,
MustacheException::UNEXPECTED_CLOSE_SECTION,
MustacheException::UNKNOWN_PARTIAL,
MustacheException::UNKNOWN_PRAGMA,
);
$one = new TestableMustache(null, null, null, array(
'throws_exceptions' => array_fill_keys($exceptions, true)
));
$thrownExceptions = $one->getThrownExceptions();
foreach ($exceptions as $exception) {
$this->assertTrue($thrownExceptions[$exception]);
}
$two = new TestableMustache(null, null, null, array(
'throws_exceptions' => array_fill_keys($exceptions, false)
));
$thrownExceptions = $two->getThrownExceptions();
foreach ($exceptions as $exception) {
$this->assertFalse($thrownExceptions[$exception]);
}
}
}
class PickyMustache extends Mustache {
protected $_throwsExceptions = array(
MustacheException::UNKNOWN_VARIABLE => true,
MustacheException::UNCLOSED_SECTION => true,
MustacheException::UNEXPECTED_CLOSE_SECTION => true,
MustacheException::UNKNOWN_PARTIAL => true,
MustacheException::UNKNOWN_PRAGMA => true,
);
}
class SlackerMustache extends Mustache {
protected $_throwsExceptions = array(
MustacheException::UNKNOWN_VARIABLE => false,
MustacheException::UNCLOSED_SECTION => false,
MustacheException::UNEXPECTED_CLOSE_SECTION => false,
MustacheException::UNKNOWN_PARTIAL => false,
MustacheException::UNKNOWN_PRAGMA => false,
);
}
class TestableMustache extends Mustache {
public function testableGetPragmaOptions($pragma_name) {
return $this->_getPragmaOptions($pragma_name);
}
public function getThrownExceptions() {
return $this->_throwsExceptions;
}
}
-74
View File
@@ -1,74 +0,0 @@
<?php
require_once '../Mustache.php';
/**
* @group pragmas
*/
class MustachePragmaTest extends PHPUnit_Framework_TestCase {
public function testUnknownPragmaException() {
$m = new Mustache();
try {
$m->render('{{%I-HAVE-THE-GREATEST-MUSTACHE}}');
} catch (MustacheException $e) {
$this->assertEquals(MustacheException::UNKNOWN_PRAGMA, $e->getCode(), 'Caught exception code was not MustacheException::UNKNOWN_PRAGMA');
return;
}
$this->fail('Mustache should have thrown an unknown pragma exception');
}
public function testSuppressUnknownPragmaException() {
$m = new LessWhinyMustache();
try {
$this->assertEquals('', $m->render('{{%I-HAVE-THE-GREATEST-MUSTACHE}}'));
} catch (MustacheException $e) {
if ($e->getCode() == MustacheException::UNKNOWN_PRAGMA) {
$this->fail('Mustache should have thrown an unknown pragma exception');
} else {
throw $e;
}
}
}
public function testPragmaReplace() {
$m = new Mustache();
$this->assertEquals('', $m->render('{{%UNESCAPED}}'), 'Pragma tag not removed');
}
public function testPragmaReplaceMultiple() {
$m = new Mustache();
$this->assertEquals('', $m->render('{{% UNESCAPED }}'), 'Pragmas should allow whitespace');
$this->assertEquals('', $m->render('{{% UNESCAPED foo=bar }}'), 'Pragmas should allow whitespace');
$this->assertEquals('', $m->render("{{%UNESCAPED}}\n{{%UNESCAPED}}"), 'Multiple pragma tags not removed');
$this->assertEquals(' ', $m->render('{{%UNESCAPED}} {{%UNESCAPED}}'), 'Multiple pragma tags not removed');
}
public function testPragmaReplaceNewline() {
$m = new Mustache();
$this->assertEquals('', $m->render("{{%UNESCAPED}}\n"), 'Trailing newline after pragma tag not removed');
$this->assertEquals("\n", $m->render("\n{{%UNESCAPED}}\n"), 'Too many newlines removed with pragma tag');
$this->assertEquals("1\n23", $m->render("1\n2{{%UNESCAPED}}\n3"), 'Wrong newline removed with pragma tag');
}
public function testPragmaReset() {
$m = new Mustache('', array('symbol' => '>>>'));
$this->assertEquals('>>>', $m->render('{{{symbol}}}'));
$this->assertEquals('>>>', $m->render('{{%UNESCAPED}}{{symbol}}'));
$this->assertEquals('>>>', $m->render('{{{symbol}}}'));
}
}
class LessWhinyMustache extends Mustache {
protected $_throwsExceptions = array(
MustacheException::UNKNOWN_VARIABLE => false,
MustacheException::UNCLOSED_SECTION => true,
MustacheException::UNEXPECTED_CLOSE_SECTION => true,
MustacheException::UNKNOWN_PARTIAL => false,
MustacheException::UNKNOWN_PRAGMA => false,
);
}
@@ -1,19 +0,0 @@
<?php
require_once '../Mustache.php';
/**
* @group pragmas
*/
class MustachePragmaUnescapedTest extends PHPUnit_Framework_TestCase {
public function testPragmaUnescaped() {
$m = new Mustache(null, array('title' => 'Bear > Shark'));
$this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{title}}'));
$this->assertEquals('Bear &gt; Shark', $m->render('{{title}}'));
$this->assertEquals('Bear &gt; Shark', $m->render('{{%UNESCAPED}}{{{title}}}'));
$this->assertEquals('Bear > Shark', $m->render('{{{title}}}'));
}
}
-1
View File
@@ -1 +0,0 @@
{{# truthy }}{{ bar }}{{/ truthy }}
-1
View File
@@ -1 +0,0 @@
{{ foo }}