Tests/Specs: Use Chai for assertions, instead of Jasmine's assertions.
[mootools.git] / Specs / Types / Number.js
blobda37bb48d84d1e4075eec1e67b13e5970831ed3e
1 /*
2 ---
3 name: Number
4 requires: ~
5 provides: ~
6 ...
7 */
9 describe("Number Methods", function(){
11         // Number.toInt
13         it('should convert a number to an integer', function(){
14                 expect((111).toInt()).to.equal(111);
15         });
17         it('should convert a number depending on the radix provided', function(){
18                 expect((111).toInt(2)).to.equal(7);
19                 expect((0x16).toInt(10)).to.equal(22); //ECMA standard, radix is optional so if starts with 0x then parsed as hexadecimal
20                 expect((016).toInt(10)).to.equal(14); //ECMA standard, radix is optional so if starts with 0 then parsed as octal
21         });
23         // Number.toFloat
25         it('should convert a number to a float', function(){
26                 expect((1.00).toFloat()).to.equal(1);
27                 expect((1.12 - 0.12).toFloat()).to.equal(1);
28                 expect((0.0010).toFloat()).to.equal(0.001);
29                 expect((Number.MIN_VALUE).toFloat()).to.equal(Number.MIN_VALUE);
30         });
32         // Number.limit
34         it('should limit a number within a range', function(){
35                 expect((-1).limit(0, 1)).to.equal(0);
36                 expect((3).limit(1, 2)).to.equal(2);
37         });
39         it('should not limit a number if within the range', function(){
40                 expect((2).limit(0,4)).to.equal(2);
41         });
43         // Number.round
45         it('should round a number to the nearest whole number if units place is not specified', function(){
46                 expect((0.01).round()).to.equal(0);
47         });
49         it('should round a number according the units place specified', function(){
50                 expect((0.01).round(2)).to.equal(0.01);
51                 expect((1).round(3)).to.equal(1);
52                 expect((-1.01).round()).to.equal(-1);
53                 expect((-1.01).round(2)).to.equal(-1.01);
54                 expect((111).round(-1)).to.equal(110);
55                 expect((-111).round(-2)).to.equal(-100);
56                 expect((100).round(-5)).to.equal(0);
57         });
59         // Number.times
61         it('should call the function for the specified number of times', function(){
62                 var found = 0;
63                 (3).times(function(i){
64                         found = i;
65                 });
67                 var found2 = -1;
68                 (0).times(function(i){
69                         found2 = i;
70                 });
72                 expect(found).to.equal(2);
73                 expect(found2).to.equal(-1);
74         });
76         it('should bind and call the function for the specified number of times', function(){
77                 var aTest = 'hi';
78                 var found3 = false;
79                 (1).times(function(i){
80                         found3 = (this == aTest);
81                 }, aTest);
82                 expect(found3).to.equal(true);
83         });
85 });
88 (function(math){
90         describe('Number Math Methods', function(){
91                 Object.each(math, function(value, key){
92                         var example = {};
93                         var b = value.test[1];
94                         it('should return the ' + value.title + ' value of the number' + ((b) ? ' and the passed number' : ''), function(){
95                                 expect(value.test[0][key](b)).to.equal(Math[key].apply(null, value.test));
96                         });
97                 });
98         });
100 })({
101         abs: { test: [-1], title: 'absolute' },
102         acos: { test: [0], title: 'arc cosine' },
103         asin: { test: [0.5], title: 'arc sine' },
104         atan: { test: [0.5], title: 'arc tangent' },
105         atan2: { test: [0.1, 0.5], title: 'arc tangent' },
106         ceil: { test: [0.6], title: 'number closest to and not less than the' },
107         cos: { test: [30], title: 'cosine' },
108         exp: { test: [2], title: 'exponent' },
109         floor: { test: [2.4], title: 'integer closet to and not greater than' },
110         log: { test: [2], title: 'log' },
111         max: { test: [5, 3], title: 'maximum' },
112         min: { test: [-4, 2], title: 'minimum' },
113         pow: { test: [2, 2], title: 'power' },
114         sin: { test: [0.5], title: 'sine' },
115         sqrt: { test: [4], title: 'square root' },
116         tan: { test: [0.3], title: 'tangent' }