Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / core / test / unit / spec / lang / Number.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 describe("Ext.Number", function(){
16     var Number = Ext.Number;
17     
18     describe("constraining a number", function(){
19         describe("integers", function(){
20             describe("if the number is within the constaints", function(){
21                 it("should leave the number alone if it is equal to the min and the max", function(){
22                     expect(Number.constrain(1, 1, 1)).toEqual(1);
23                 });
24                 
25                 it("should leave the number alone if it is equal to the min", function(){
26                     expect(Number.constrain(1, 1, 5)).toEqual(1);
27                 });
28                 
29                 it("should leave the number alone if it is equal to the max", function(){
30                     expect(Number.constrain(5, 1, 5)).toEqual(5);
31                 });
32                 
33                 it("should leave the number alone if it is within the min and the max", function(){
34                     expect(Number.constrain(3, 1, 5)).toEqual(3);
35                 });
36                 
37                 it("should leave a negative number alone if it is within the min and the max", function(){
38                     expect(Number.constrain(-3, -5, -1)).toEqual(-3);
39                 });
40             });
41             
42             describe("if the number is not within the constraints", function(){
43                 it("should make the number equal to the min value", function(){
44                     expect(Number.constrain(1, 3, 5)).toEqual(3);
45                 });
46                 
47                 it("should make the number equal to the max value", function(){
48                     expect(Number.constrain(100, 1, 5)).toEqual(5);
49                 });
50                 
51                 describe("and the number is negative", function(){
52                     it("should make the number equal to the min value", function(){
53                         expect(Number.constrain(-10, -50, -30)).toEqual(-30);
54                     });
55                     
56                     it("should make the number equal to the max value", function(){
57                         expect(Number.constrain(-100, -50, -30)).toEqual(-50);
58                     });
59                 });
60             });
61         });
62         
63         describe("floating point numbers", function(){
64             describe("if the number is within the constaints", function(){
65                 it("should leave the number alone", function(){
66                     expect(Number.constrain(3.3, 3.1, 3.5)).toEqual(3.3);
67                 });
68                 
69                 it("should leave a negative number alone", function(){
70                     expect(Number.constrain(-3.3, -3.5, -3.1)).toEqual(-3.3);
71                 });
72             });
73             
74             describe("and the number is negative", function(){
75                 it("should make the number equal to the min value", function(){
76                     expect(Number.constrain(-3.3, -3.1, -3)).toEqual(-3.1);
77                 });
78                 
79                 it("should make the number equal to the max value", function(){
80                     expect(Number.constrain(-2.1, -3.1, -3)).toEqual(-3);
81                 });
82             });
83         });
84     });
85     
86     describe("toFixed", function(){
87         
88         var f = Number.toFixed;
89         
90         it("should return a string", function(){
91             expect(typeof f(1)).toEqual('string');
92         });
93         
94         it("should default precision to 0", function(){
95             expect(f(1.23456)).toEqual('1');
96         });
97         
98         it("should output the correct number of decimal places", function(){
99             expect(f(1, 3)).toEqual('1.000');
100         });
101         
102         it("should round correctly", function(){
103             expect(f(1.9834657, 1)).toEqual('2.0');
104         });
105         
106         it("should round with negative numbers", function(){
107             expect(f(-3.4265, 2)).toEqual('-3.43');
108         });
109     });
110 });
111