1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of StringTemplate. |
4
|
|
|
* |
5
|
|
|
* (c) 2013 Nicolò Martini |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace StringTemplate\Test; |
11
|
|
|
|
12
|
|
|
use StringTemplate\SprintfEngine; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Unit tests for class Engine |
16
|
|
|
*/ |
17
|
|
|
class SprintfEngineTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public function testRender() |
21
|
|
|
{ |
22
|
|
|
$engine = new SprintfEngine(); |
23
|
|
|
$this->assertEquals( |
24
|
|
|
sprintf('Oh! %s %s jumped onto %s (%e) table', 'The', 'cat', 1, 1), |
25
|
|
|
$engine->render( |
26
|
|
|
'Oh! {subj.det%s} {subj.np%s} {verb} onto {w.where.number%s} ({w.where.number%e}) {w.where.np}', |
27
|
|
|
array( |
28
|
|
|
'verb' => 'jumped', |
29
|
|
|
'subj' => array('det' => 'The', 'np' => 'cat'), |
30
|
|
|
'w' => array('where' => array('number' => 1, 'np' => 'table')) |
31
|
|
|
) |
32
|
|
|
) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testRenderWithObjectValues() |
37
|
|
|
{ |
38
|
|
|
$engine = new SprintfEngine(); |
39
|
|
|
$this->assertEquals( |
40
|
|
|
'foo', |
41
|
|
|
$engine->render( |
42
|
|
|
'{val%s}', |
43
|
|
|
array('val' => new ObjectMockForSprintf()) |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testRenderWithAdditionalFormatting() |
49
|
|
|
{ |
50
|
|
|
$engine = new SprintfEngine(); |
51
|
|
|
$this->assertEquals( |
52
|
|
|
'I have 1.2 (1.230000E+0) apples.', |
53
|
|
|
$engine->render( |
54
|
|
|
"I have {num%.1f} ({num%.6E}) {fruit}.", |
55
|
|
|
array( |
56
|
|
|
'num' => 1.23, |
57
|
|
|
'fruit' => 'apples' |
58
|
|
|
) |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
class ObjectMockForSprintf |
65
|
|
|
{ |
66
|
|
|
function __toString() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return 'foo'; |
69
|
|
|
} |
70
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.