Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #23 from fred84/multibyteStepMatch
Browse files Browse the repository at this point in the history
fix for argument position matching in multibyte definitions
  • Loading branch information
olbrich committed Dec 6, 2013
2 parents cb237a7 + 6b539c7 commit bc2d80c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/CucumberSteps.php
Expand Up @@ -18,7 +18,7 @@ class CucumberSteps extends PHPUnit_Framework_Assert {
protected $aGlobals; protected $aGlobals;


public function __construct(&$_aGlobals) { public function __construct(&$_aGlobals) {
$this->aGlobals =& $_aGlobals; $this->aGlobals = $_aGlobals;
} }


public static function markPending($sMessage = "Not Implemented") { public static function markPending($sMessage = "Not Implemented") {
Expand Down
9 changes: 7 additions & 2 deletions lib/Cuke4Php.php
Expand Up @@ -4,7 +4,8 @@
*/ */


set_time_limit(0); set_time_limit(0);

mb_internal_encoding('utf-8');
mb_regex_encoding('utf-8');


/** /**
* Cuke4Php implements the Cucumber wire protocol for PHP * Cuke4Php implements the Cucumber wire protocol for PHP
Expand Down Expand Up @@ -222,7 +223,7 @@ function stepMatches($sStep) {
$aArgs = array(); $aArgs = array();
array_shift($aMatches); array_shift($aMatches);
foreach ($aMatches as $aMatch) { foreach ($aMatches as $aMatch) {
$aArgs[] = array('val' => $aMatch[0][0], 'pos' => $aMatch[0][1]); $aArgs[] = array('val' => $aMatch[0][0], 'pos' => $this->getCharPosition($sStep, $aMatch[0][1]));
} }
$aSteps[] = array('id' => $i, 'args' => $aArgs, 'source' => $aStep['filename'] . ":" . $aStep['startline']); $aSteps[] = array('id' => $i, 'args' => $aArgs, 'source' => $aStep['filename'] . ":" . $aStep['startline']);
}; };
Expand Down Expand Up @@ -277,6 +278,10 @@ public function $sMethodName($sParams) {
EOT; EOT;
return array('success', $sMethodBody); return array('success', $sMethodBody);
} }

private function getCharPosition($text, $bytePos) {
return mb_strlen(substr($text, 0, $bytePos));
}
} }


?> ?>
56 changes: 56 additions & 0 deletions tests/lib/OffsetTest.php
@@ -0,0 +1,56 @@
<?php

require_once dirname(__FILE__) . "/../../lib/Cucumber.php";

class OffsetTest extends \PHPUnit_Framework_TestCase {

const FEATURE_FILE = '1.feature';
const STARTLINE = 1;

private $cuke;

public function setUp() {
$this->cuke = new Cuke4Php(dirname(__FILE__) . "/../../features");
}

public function testOffsetRussian() {
$this->populateWorld("/^Я ввожу складываю (\d+) и (\d+)$/");
$sStep = "Я ввожу складываю 1 и 2";

$this->assertEquals(
$this->generateExpectation(array( array('val' => '1', 'pos' => 18), array('val' => '2', 'pos' => 22) )),
$this->cuke->stepMatches($sStep)
);
}

public function testOffsetEnglish() {
$this->populateWorld("/^I add (\d+) to (\d+)$/");
$sStep = "I add 1 to 2";

$this->assertEquals(
$this->generateExpectation(array( array('val' => '1', 'pos' => 6), array('val' => '2', 'pos' => 11) )),
$this->cuke->stepMatches($sStep)
);
}

private function populateWorld($regexp) {
$this->cuke->aWorld = array('steps' =>
array(
array('regexp' => $regexp, 'filename' => self::FEATURE_FILE, 'startline' => self::STARTLINE)
)
);
}

private function generateExpectation(array $args) {
return array(
0 => 'success',
1 => array(
0 => array(
'id' => 0,
'args' => $args,
'source' => self::FEATURE_FILE . ':' . self::STARTLINE
)
)
);
}
}

0 comments on commit bc2d80c

Please sign in to comment.