X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/0494b8d9b9bb03ab6c22b34dae81261e3cd7e3e6..7a654f8d43fdb43d78b63d90528bed6e86b608cc:/src/core/test/unit/spec/lang/Number.js diff --git a/src/core/test/unit/spec/lang/Number.js b/src/core/test/unit/spec/lang/Number.js new file mode 100644 index 00000000..28a01bbf --- /dev/null +++ b/src/core/test/unit/spec/lang/Number.js @@ -0,0 +1,96 @@ +describe("Ext.Number", function(){ + var Number = Ext.Number; + + describe("constraining a number", function(){ + describe("integers", function(){ + describe("if the number is within the constaints", function(){ + it("should leave the number alone if it is equal to the min and the max", function(){ + expect(Number.constrain(1, 1, 1)).toEqual(1); + }); + + it("should leave the number alone if it is equal to the min", function(){ + expect(Number.constrain(1, 1, 5)).toEqual(1); + }); + + it("should leave the number alone if it is equal to the max", function(){ + expect(Number.constrain(5, 1, 5)).toEqual(5); + }); + + it("should leave the number alone if it is within the min and the max", function(){ + expect(Number.constrain(3, 1, 5)).toEqual(3); + }); + + it("should leave a negative number alone if it is within the min and the max", function(){ + expect(Number.constrain(-3, -5, -1)).toEqual(-3); + }); + }); + + describe("if the number is not within the constraints", function(){ + it("should make the number equal to the min value", function(){ + expect(Number.constrain(1, 3, 5)).toEqual(3); + }); + + it("should make the number equal to the max value", function(){ + expect(Number.constrain(100, 1, 5)).toEqual(5); + }); + + describe("and the number is negative", function(){ + it("should make the number equal to the min value", function(){ + expect(Number.constrain(-10, -50, -30)).toEqual(-30); + }); + + it("should make the number equal to the max value", function(){ + expect(Number.constrain(-100, -50, -30)).toEqual(-50); + }); + }); + }); + }); + + describe("floating point numbers", function(){ + describe("if the number is within the constaints", function(){ + it("should leave the number alone", function(){ + expect(Number.constrain(3.3, 3.1, 3.5)).toEqual(3.3); + }); + + it("should leave a negative number alone", function(){ + expect(Number.constrain(-3.3, -3.5, -3.1)).toEqual(-3.3); + }); + }); + + describe("and the number is negative", function(){ + it("should make the number equal to the min value", function(){ + expect(Number.constrain(-3.3, -3.1, -3)).toEqual(-3.1); + }); + + it("should make the number equal to the max value", function(){ + expect(Number.constrain(-2.1, -3.1, -3)).toEqual(-3); + }); + }); + }); + }); + + describe("toFixed", function(){ + + var f = Number.toFixed; + + it("should return a string", function(){ + expect(typeof f(1)).toEqual('string'); + }); + + it("should default precision to 0", function(){ + expect(f(1.23456)).toEqual('1'); + }); + + it("should output the correct number of decimal places", function(){ + expect(f(1, 3)).toEqual('1.000'); + }); + + it("should round correctly", function(){ + expect(f(1.9834657, 1)).toEqual('2.0'); + }); + + it("should round with negative numbers", function(){ + expect(f(-3.4265, 2)).toEqual('-3.43'); + }); + }); +});