Skip to content

Commit 52a7541

Browse files
authored
Merge pull request #32 from ischenko/master
Do not display "examples index" when examples are not set
2 parents 23862b7 + b2af0aa commit 52a7541

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/Codeception/Specify.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,28 @@ private function specifyInit()
2828
}
2929

3030
function specify($specification, \Closure $callable = null, $params = [])
31-
{
31+
{
3232
if (!$callable) return;
3333
$this->specifyInit();
3434

3535
$test = $callable->bindTo($this);
36-
$name = $this->getName();
37-
$this->setName($this->getName().' | '.$specification);
36+
$oldName = $this->getName();
37+
$newName = $oldName . ' | ' . $specification;
38+
39+
$this->setName($newName);
3840

3941
$properties = get_object_vars($this);
4042

4143
// prepare for execution
4244
$throws = $this->getSpecifyExpectedException($params);
4345
$examples = $this->getSpecifyExamples($params);
46+
$showExamplesIndex = $examples !== [[]];
4447

4548
foreach ($examples as $idx => $example) {
46-
$this->setName($name.' | '.$specification .' | examples index '. $idx);
49+
if ($showExamplesIndex) {
50+
$this->setName($newName . ' | examples index ' . $idx);
51+
}
52+
4753
// copy current object properties
4854
$this->specifyCloneProperties($properties);
4955

@@ -67,8 +73,8 @@ function specify($specification, \Closure $callable = null, $params = [])
6773
}
6874

6975
// restore test name
70-
$this->setName($name);
71-
}
76+
$this->setName($oldName);
77+
}
7278

7379
/**
7480
* @param $params

tests/SpecifyTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,52 @@ function testPropertyRestore()
233233
$this->assertEquals(['hello', 'world'], $this->testOne->prop);
234234
}
235235

236+
public function testExamplesIndexInName()
237+
{
238+
$name = $this->getName();
239+
240+
$this->specify('it appends index of an example to a test case name', function ($idx, $example) use ($name) {
241+
$name .= ' | it appends index of an example to a test case name';
242+
$this->assertEquals($name . ' | examples index ' . $idx, $this->getName());
243+
244+
$this->specify('nested specification without examples', function () use ($idx, $name) {
245+
$name .= ' | examples index ' . $idx;
246+
$name .= ' | nested specification without examples';
247+
$this->assertEquals($name, $this->getName());
248+
});
249+
250+
$this->specify('nested specification with examples', function () use ($idx, $name) {
251+
$name .= ' | examples index ' . $idx;
252+
$name .= ' | nested specification with examples';
253+
$name .= ' | examples index 0';
254+
$this->assertEquals($name, $this->getName());
255+
}, ['examples' => [
256+
[$example]
257+
]]);
258+
}, ['examples' => [
259+
[0, ''],
260+
[1, '0'],
261+
[2, null],
262+
[3, 'bye'],
263+
[4, 'world'],
264+
]]);
265+
266+
$this->specify('it does not append index to a test case name if there are no examples', function () use ($name) {
267+
$name .= ' | it does not append index to a test case name if there are no examples';
268+
$this->assertEquals($name, $this->getName());
269+
270+
$this->specify('nested specification without examples', function () use ($name) {
271+
$this->assertEquals($name . ' | nested specification without examples', $this->getName());
272+
});
273+
274+
$this->specify('nested specification with examples', function () use ($name) {
275+
$this->assertEquals($name . ' | nested specification with examples | examples index 0', $this->getName());
276+
}, ['examples' => [
277+
[null]
278+
]]);
279+
});
280+
}
281+
236282
// public function testFail()
237283
// {
238284
// $this->specify('this will fail', function(){

0 commit comments

Comments
 (0)