1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 14:52:40 +01:00

Add some tests for subclasses

Summary: Add some tests to ensure that `ArcanistXHPASTLinterRule` subclasses are properly implemented. This should catch issues such as two linter rules having the same `ID` value. See D13272 for a similar change.

Test Plan: `arc unit`

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D13274
This commit is contained in:
Joshua Spence 2015-06-15 20:00:54 +10:00
parent 956bfa701c
commit fe4856277c
4 changed files with 39 additions and 27 deletions

View file

@ -261,6 +261,7 @@ phutil_register_library_map(array(
'ArcanistXHPASTLintSwitchHook' => 'lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php', 'ArcanistXHPASTLintSwitchHook' => 'lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php',
'ArcanistXHPASTLinter' => 'lint/linter/ArcanistXHPASTLinter.php', 'ArcanistXHPASTLinter' => 'lint/linter/ArcanistXHPASTLinter.php',
'ArcanistXHPASTLinterRule' => 'lint/linter/xhpast/ArcanistXHPASTLinterRule.php', 'ArcanistXHPASTLinterRule' => 'lint/linter/xhpast/ArcanistXHPASTLinterRule.php',
'ArcanistXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/__tests__/ArcanistXHPASTLinterRuleTestCase.php',
'ArcanistXHPASTLinterTestCase' => 'lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php', 'ArcanistXHPASTLinterTestCase' => 'lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php',
'ArcanistXMLLinter' => 'lint/linter/ArcanistXMLLinter.php', 'ArcanistXMLLinter' => 'lint/linter/ArcanistXMLLinter.php',
'ArcanistXMLLinterTestCase' => 'lint/linter/__tests__/ArcanistXMLLinterTestCase.php', 'ArcanistXMLLinterTestCase' => 'lint/linter/__tests__/ArcanistXMLLinterTestCase.php',
@ -533,6 +534,7 @@ phutil_register_library_map(array(
'ArcanistXHPASTLintSwitchHook' => 'Phobject', 'ArcanistXHPASTLintSwitchHook' => 'Phobject',
'ArcanistXHPASTLinter' => 'ArcanistBaseXHPASTLinter', 'ArcanistXHPASTLinter' => 'ArcanistBaseXHPASTLinter',
'ArcanistXHPASTLinterRule' => 'Phobject', 'ArcanistXHPASTLinterRule' => 'Phobject',
'ArcanistXHPASTLinterRuleTestCase' => 'PhutilTestCase',
'ArcanistXHPASTLinterTestCase' => 'ArcanistLinterTestCase', 'ArcanistXHPASTLinterTestCase' => 'ArcanistLinterTestCase',
'ArcanistXMLLinter' => 'ArcanistLinter', 'ArcanistXMLLinter' => 'ArcanistLinter',
'ArcanistXMLLinterTestCase' => 'ArcanistLinterTestCase', 'ArcanistXMLLinterTestCase' => 'ArcanistLinterTestCase',

View file

@ -8,7 +8,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
private $rules = array(); private $rules = array();
public function __construct() { public function __construct() {
$this->rules = $this->getLinterRules(); $this->rules = ArcanistXHPASTLinterRule::loadAllRules();
} }
public function __clone() { public function __clone() {
@ -66,32 +66,6 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
return count($this->rules); return count($this->rules);
} }
public function getLinterRules() {
$rules = array();
$symbols = id(new PhutilSymbolLoader())
->setAncestorClass('ArcanistXHPASTLinterRule')
->loadObjects();
foreach ($symbols as $class => $rule) {
$id = $rule->getLintID();
if (isset($rules[$id])) {
throw new Exception(
pht(
'Two linter rules (`%s`, `%s`) share the same lint ID (%d). '.
'Each linter rule must have a unique ID.',
$class,
get_class($rules[$id]),
$id));
}
$rules[$id] = $rule;
}
return $rules;
}
protected function resolveFuture($path, Future $future) { protected function resolveFuture($path, Future $future) {
$tree = $this->getXHPASTTreeForPath($path); $tree = $this->getXHPASTTreeForPath($path);
if (!$tree) { if (!$tree) {

View file

@ -4,6 +4,32 @@ abstract class ArcanistXHPASTLinterRule extends Phobject {
private $linter = null; private $linter = null;
final public static function loadAllRules() {
$rules = array();
$symbols = id(new PhutilSymbolLoader())
->setAncestorClass(__CLASS__)
->loadObjects();
foreach ($symbols as $class => $rule) {
$id = $rule->getLintID();
if (isset($rules[$id])) {
throw new Exception(
pht(
'Two linter rules (`%s`, `%s`) share the same lint ID (%d). '.
'Each linter rule must have a unique ID.',
$class,
get_class($rules[$id]),
$id));
}
$rules[$id] = $rule;
}
return $rules;
}
final public function getLintID() { final public function getLintID() {
$class = new ReflectionClass($this); $class = new ReflectionClass($this);

View file

@ -0,0 +1,10 @@
<?php
final class ArcanistXHPASTLinterRuleTestCase extends PhutilTestCase {
public function testLoadAllRules() {
ArcanistXHPASTLinterRule::loadAllRules();
$this->assertTrue(true);
}
}