Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / test / unit / spec / lang / Number.js
1 describe("Ext.Number", function(){
2     var Number = Ext.Number;
3     
4     describe("constraining a number", function(){
5         describe("integers", function(){
6             describe("if the number is within the constaints", function(){
7                 it("should leave the number alone if it is equal to the min and the max", function(){
8                     expect(Number.constrain(1, 1, 1)).toEqual(1);
9                 });
10                 
11                 it("should leave the number alone if it is equal to the min", function(){
12                     expect(Number.constrain(1, 1, 5)).toEqual(1);
13                 });
14                 
15                 it("should leave the number alone if it is equal to the max", function(){
16                     expect(Number.constrain(5, 1, 5)).toEqual(5);
17                 });
18                 
19                 it("should leave the number alone if it is within the min and the max", function(){
20                     expect(Number.constrain(3, 1, 5)).toEqual(3);
21                 });
22                 
23                 it("should leave a negative number alone if it is within the min and the max", function(){
24                     expect(Number.constrain(-3, -5, -1)).toEqual(-3);
25                 });
26             });
27             
28             describe("if the number is not within the constraints", function(){
29                 it("should make the number equal to the min value", function(){
30                     expect(Number.constrain(1, 3, 5)).toEqual(3);
31                 });
32                 
33                 it("should make the number equal to the max value", function(){
34                     expect(Number.constrain(100, 1, 5)).toEqual(5);
35                 });
36                 
37                 describe("and the number is negative", function(){
38                     it("should make the number equal to the min value", function(){
39                         expect(Number.constrain(-10, -50, -30)).toEqual(-30);
40                     });
41                     
42                     it("should make the number equal to the max value", function(){
43                         expect(Number.constrain(-100, -50, -30)).toEqual(-50);
44                     });
45                 });
46             });
47         });
48         
49         describe("floating point numbers", function(){
50             describe("if the number is within the constaints", function(){
51                 it("should leave the number alone", function(){
52                     expect(Number.constrain(3.3, 3.1, 3.5)).toEqual(3.3);
53                 });
54                 
55                 it("should leave a negative number alone", function(){
56                     expect(Number.constrain(-3.3, -3.5, -3.1)).toEqual(-3.3);
57                 });
58             });
59             
60             describe("and the number is negative", function(){
61                 it("should make the number equal to the min value", function(){
62                     expect(Number.constrain(-3.3, -3.1, -3)).toEqual(-3.1);
63                 });
64                 
65                 it("should make the number equal to the max value", function(){
66                     expect(Number.constrain(-2.1, -3.1, -3)).toEqual(-3);
67                 });
68             });
69         });
70     });
71     
72     describe("toFixed", function(){
73         
74         var f = Number.toFixed;
75         
76         it("should return a string", function(){
77             expect(typeof f(1)).toEqual('string');
78         });
79         
80         it("should default precision to 0", function(){
81             expect(f(1.23456)).toEqual('1');
82         });
83         
84         it("should output the correct number of decimal places", function(){
85             expect(f(1, 3)).toEqual('1.000');
86         });
87         
88         it("should round correctly", function(){
89             expect(f(1.9834657, 1)).toEqual('2.0');
90         });
91         
92         it("should round with negative numbers", function(){
93             expect(f(-3.4265, 2)).toEqual('-3.43');
94         });
95     });
96 });