summaryrefslogtreecommitdiff
path: root/tests/unit/I18N/core/NumberFormatTest.php
blob: af6a06cab0b191142199e58849ac5aebc22d8297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
require_once dirname(__FILE__).'/../../phpunit2.php';

//NOTE: This page require UTF-8 aware editors
Prado::using('System.I18N.core.NumberFormat');

/**
 * @package System.I18N.core
 */
class NumberFormatTest extends PHPUnit2_Framework_TestCase {
  function testDefaultFormats() {
    $formatter = new NumberFormat();
    $number = '123456789.125156';
    $wanted = '123,456,789.125156';
    $this->assertEquals($wanted, $formatter->format($number));
    
    //currency
    $wanted = 'US$123,456,789.13';
    $this->assertEquals($wanted, $formatter->format($number,'c'));
  }

  function testLocalizedCurrencyFormats() {
    $fr = new NumberFormat('fr');
    $de = new NumberFormat('de');
    $ja = new NumberFormat('ja_JP');
    
    $number = '123456789.125156';
    
    //french
    $wanted = '123 456 789,13 F';
    $this->assertEquals($wanted, $fr->format($number,'c','FRF'));
    
    //german
    $wanted = 'DES 123.456.789,13';
    $this->assertEquals($wanted, $de->format($number,'c','DES'));
    
    //japanese
    $wanted = '¥123,456,789';
    $this->assertEquals($wanted, $ja->format($number,'c','JPY'));
    
    //custom/unkown currency
    $wanted = 'DLL123,456,789';
    $this->assertEquals($wanted, $ja->format($number,'c','DLL'));
  }

  function testCustomFormat() {
    $formatter = new NumberFormat();
    $number = '123456789.125156';
    
    //primay and secondary grouping test
    $pattern = '#,###,##.###';
    $wanted = '1,234,567,89.125156';
    $this->assertEquals($wanted, $formatter->format($number, $pattern));
    
    //4 digits grouping test
    $pattern = '#,####.###';
    $wanted = '1,2345,6789.125156';
    $this->assertEquals($wanted, $formatter->format($number, $pattern));
    
    //custom percentage
    $pattern = '#,###.00%';
    $wanted = '123,456,789.13%';
    $this->assertEquals($wanted, $formatter->format($number, $pattern));
  }
  
  function testPercentageFormat() {
    $formatter = new NumberFormat();
    $number = '0.125156';
    $wanted = '12%';
    $this->assertEquals($wanted, $formatter->format($number, 'p'));
  }
  
  function testQuotes() {
    $formatter = new NumberFormat();
    $number = '123456789.125156';
    
    $pattern = "# o'clock";
    $wanted = "123456789 o'clock";
    $this->assertEquals($wanted, $formatter->format($number, $pattern));
    
  }
  
  function testPadding() {
    $formatter = new NumberFormat();
    $number = '5';
    
    $pattern = '0000';
    $wanted = '0005';
    
    //this should fail!!!
    $this->assertNotEquals($wanted, $formatter->format($number, $pattern));
  }
  
  function testFormatWithANegativeValue() {
    $formatter = new NumberFormat();
    $number = "-1.2";
    
    $wanted = "-1.2";
    $this->assertEquals($wanted, $formatter->format($number));
  }

  public function testFormatWithAScientificPattern() {
    $formatter = new NumberFormat();
    $number = "10";
    $expected = "10E";
    $this->assertEquals('10E', $formatter->format($number, 'e'));
  }

}

?>