Skip to content
Browse files

Merge pull request #23 from fred84/multibyteStepMatch

fix for argument position matching in multibyte definitions
  • Loading branch information...
2 parents cb237a7 + 6b539c7 commit bc2d80ced7304e95be26fb1ac293844b7bb4e401 @olbrich committed Dec 6, 2013
Showing with 64 additions and 3 deletions.
  1. +1 −1 lib/CucumberSteps.php
  2. +7 −2 lib/Cuke4Php.php
  3. +56 −0 tests/lib/OffsetTest.php
View
2 lib/CucumberSteps.php
@@ -18,7 +18,7 @@ class CucumberSteps extends PHPUnit_Framework_Assert {
protected $aGlobals;
public function __construct(&$_aGlobals) {
- $this->aGlobals =& $_aGlobals;
+ $this->aGlobals = $_aGlobals;
}
public static function markPending($sMessage = "Not Implemented") {
View
9 lib/Cuke4Php.php
@@ -4,7 +4,8 @@
*/
set_time_limit(0);
-
+mb_internal_encoding('utf-8');
+mb_regex_encoding('utf-8');
/**
* Cuke4Php implements the Cucumber wire protocol for PHP
@@ -222,7 +223,7 @@ function stepMatches($sStep) {
$aArgs = array();
array_shift($aMatches);
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']);
};
@@ -277,6 +278,10 @@ public function $sMethodName($sParams) {
EOT;
return array('success', $sMethodBody);
}
+
+ private function getCharPosition($text, $bytePos) {
+ return mb_strlen(substr($text, 0, $bytePos));
+ }
}
?>
View
56 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.
Something went wrong with that request. Please try again.