Skip to content
Browse files

prevent method name dublication in annotated snippets

  • Loading branch information...
1 parent cfd8525 commit f4924b4be53603786cb68bd961e720cf6e4cac56 @everzet everzet committed Jan 4, 2012
View
20 features/annotations/append_snippets.feature
@@ -67,6 +67,8 @@ Feature: Append snippets option
assertInternalType('array', $this->parameters[$key]);
assertEquals(2, count($this->parameters[$key]));
}
+
+ private function doSomethingUndefinedWith() {}
}
"""
And a file named "features/apples.feature" with:
@@ -109,6 +111,10 @@ Feature: Append snippets option
'''
some pystring
'''
+ And pystring 5:
+ '''
+ other pystring
+ '''
And table:
| col1 | col2 |
| val1 | val2 |
@@ -180,18 +186,20 @@ Feature: Append snippets option
assertEquals(2, count($this->parameters[$key]));
}
+ private function doSomethingUndefinedWith() {}
+
/**
* @Given /^do something undefined with \$$/
*/
- public function doSomethingUndefinedWith()
+ public function doSomethingUndefinedWith2()
{
throw new PendingException();
}
/**
* @Given /^do something undefined with \\(\d+)$/
*/
- public function doSomethingUndefinedWith($argument1)
+ public function doSomethingUndefinedWith3($argument1)
{
throw new PendingException();
}
@@ -205,6 +213,14 @@ Feature: Append snippets option
}
/**
+ * @Given /^pystring (\d+):$/
+ */
+ public function pystring2($argument1, PyStringNode $string)
+ {
+ throw new PendingException();
+ }
+
+ /**
* @Given /^table:$/
*/
public function table(TableNode $table)
View
41 src/Behat/Behat/Definition/Proposal/AnnotatedDefinitionProposal.php
@@ -26,11 +26,11 @@
class AnnotatedDefinitionProposal implements DefinitionProposalInterface
{
/**
- * Total count of proposed methods.
+ * Proposed method names.
*
- * @var integer
+ * @var array
*/
- private static $proposedMethodsCount = 0;
+ private static $proposedMethods = array();
/**
* @see Behat\Behat\Definition\Proposal\DefinitionProposalInterface::supports()
@@ -43,8 +43,11 @@ public function supports(ContextInterface $context)
/**
* @see Behat\Behat\Definition\Proposal\DefinitionProposalInterface::propose()
*/
- public function propose(StepNode $step)
+ public function propose(ContextInterface $context, StepNode $step)
{
+ $contextRefl = new \ReflectionObject($context);
+ $contextClass = $contextRefl->getName();
+
$text = $step->getText();
$replacePatterns = array(
'/\'([^\']*)\'/', '/\"([^\"]*)\"/', // Quoted strings
@@ -60,7 +63,8 @@ public function propose(StepNode $step)
),
$regex
);
- $regex = preg_replace('/\'.*(?<!\')/', '\\\\$0', $regex); // Single quotes without matching pair (escape in resulting regex)
+ // Single quotes without matching pair (escape in resulting regex):
+ $regex = preg_replace('/\'.*(?<!\')/', '\\\\$0', $regex);
preg_match('/' . $regex . '/', $text, $matches);
$count = count($matches) - 1;
@@ -72,8 +76,33 @@ public function propose(StepNode $step)
if (0 !== strlen($methodName)) {
$methodName[0] = strtolower($methodName[0]);
} else {
- $methodName = 'stepDefinition' . ++static::$proposedMethodsCount;
+ $methodName = 'stepDefinition1';
+ }
+
+ // get method number from method name
+ $methodNumber = 2;
+ if (preg_match('/(\d+)$/', $methodName, $matches)) {
+ $methodNumber = intval($matches[1]);
+ }
+
+ // check that proposed method name isn't arelady defined in context
+ while ($contextRefl->hasMethod($methodName)) {
+ $methodName = preg_replace('/\d+$/', '', $methodName);
+ $methodName .= $methodNumber++;
+ }
+
+ // check that proposed method name haven't been proposed earlier
+ if (isset(self::$proposedMethods[$contextClass])) {
+ foreach (self::$proposedMethods[$contextClass] as $proposedRegex => $proposedMethod) {
+ if ($proposedRegex !== $regex) {
+ while ($proposedMethod === $methodName) {
+ $methodName = preg_replace('/\d+$/', '', $methodName);
+ $methodName .= $methodNumber++;
+ }
+ }
+ }
}
+ self::$proposedMethods[$contextClass][$regex] = $methodName;
$args = array();
for ($i = 0; $i < $count; $i++) {
View
2 src/Behat/Behat/Definition/Proposal/ClosuredDefinitionProposal.php
@@ -36,7 +36,7 @@ public function supports(ContextInterface $context)
/**
* @see Behat\Behat\Definition\Proposal\DefinitionProposalInterface::propose()
*/
- public function propose(StepNode $step)
+ public function propose(ContextInterface $context, StepNode $step)
{
$text = $step->getText();
$regex = preg_replace('/([\/\[\]\(\)\\\^\$\.\|\?\*\+])/', '\\\\$1', $text);
View
2 src/Behat/Behat/Definition/Proposal/DefinitionProposalDispatcher.php
@@ -45,7 +45,7 @@ public function propose(ContextInterface $context, StepNode $step)
{
foreach ($this->proposals as $proposal) {
if ($proposal->supports($context)) {
- return $proposal->propose($step);
+ return $proposal->propose($context, $step);
}
}
}
View
3 src/Behat/Behat/Definition/Proposal/DefinitionProposalInterface.php
@@ -31,7 +31,8 @@ function supports(ContextInterface $context);
/**
* Loads definitions and translations from provided context.
*
+ * @param Behat\Behat\Context\ContextInterface $context
* @param Behat\Gherkin\Node\StepNode $step
*/
- function propose(StepNode $step);
+ public function propose(ContextInterface $context, StepNode $step);
}

0 comments on commit f4924b4

Please sign in to comment.
Something went wrong with that request. Please try again.