/ext-4.1.0_b3/src/core/test/unit/spec/util/Format.js
JavaScript | 521 lines | 471 code | 42 blank | 8 comment | 0 complexity | 82ce6590af82fbd54e3cf064aea37a1f MD5 | raw file
- describe("Ext.util.Format", function() {
- var savedFormatLocale = {
- thousandSeparator: Ext.util.Format.thousandSeparator,
- decimalSeparator: Ext.util.Format.decimalSeparator,
- currencySign: Ext.util.Format.currencySign
- };
- describe("undef", function() {
- it("should return the value itself if defined", function() {
- expect(Ext.util.Format.undef("this is a defined value")).toBe("this is a defined value");
- expect(Ext.util.Format.undef(12345)).toBe(12345);
- expect(Ext.util.Format.undef(12345.67)).toBe(12345.67);
- expect(Ext.util.Format.undef(true)).toBe(true);
- });
- it("should return an empty string if the value is undefined", function() {
- expect(Ext.util.Format.undef(undefined)).toBe("");
- });
- });
- describe("defaultValue", function() {
- it("should return the value itself if defined", function () {
- expect(Ext.util.Format.defaultValue("value", "default value")).toBe("value");
- });
- it("should return the default value if the value is undefined", function() {
- expect(Ext.util.Format.defaultValue(undefined, "default value")).toBe("default value");
- });
- it("should return the default value if the value is empty", function() {
- expect(Ext.util.Format.defaultValue("", "default value")).toBe("default value");
- });
- });
- describe("substr", function() {
- it("should truncate the string from the start position", function() {
- expect(Ext.util.Format.substr("abc", 0, 1)).toBe("a");
- expect(Ext.util.Format.substr("abc", 1, 1)).toBe("b");
- expect(Ext.util.Format.substr("abc", 2, 1)).toBe("c");
- });
- it("should truncate the string at the specified length", function() {
- expect(Ext.util.Format.substr("abc", 0, 0)).toBe("");
- expect(Ext.util.Format.substr("abc", 0, 1)).toBe("a");
- expect(Ext.util.Format.substr("abc", 0, 2)).toBe("ab");
- expect(Ext.util.Format.substr("abc", 0, 3)).toBe("abc");
- });
- it("should convert non-string values to its string representation and then truncate", function() {
- expect(Ext.util.Format.substr(1234, 1, 2)).toBe("23");
- expect(Ext.util.Format.substr(true, 1, 2)).toBe("ru");
- });
- it("should start at the end of the string if start value is negative", function() {
- expect(Ext.util.Format.substr("abc", -1, 1)).toBe("c");
- expect(Ext.util.Format.substr("abc", -2, 1)).toBe("b");
- expect(Ext.util.Format.substr("abc", -3, 1)).toBe("a");
- expect(Ext.util.Format.substr("abc", -4, 1)).toBe("a");
- expect(Ext.util.Format.substr("abc", -5, 1)).toBe("a");
- });
- it("should return empty string if start position is out of bounds", function() {
- expect(Ext.util.Format.substr("abc", 4, 1)).toBe("");
- expect(Ext.util.Format.substr("abc", 5, 1)).toBe("");
- expect(Ext.util.Format.substr("abc", 6, 1)).toBe("");
- });
- it("should return empty string if length is negative", function() {
- expect(Ext.util.Format.substr("abc", 0, -1)).toBe("");
- expect(Ext.util.Format.substr("abc", 0, -2)).toBe("");
- expect(Ext.util.Format.substr("abc", 0, -3)).toBe("");
- });
- it("should return the whole string if specified length is greater than string length", function() {
- expect(Ext.util.Format.substr("abc", 0, 4)).toBe("abc");
- expect(Ext.util.Format.substr("abc", 0, 5)).toBe("abc");
- expect(Ext.util.Format.substr("abc", 1, 3)).toBe("bc");
- expect(Ext.util.Format.substr("abc", 2, 2)).toBe("c");
- });
- });
- describe("lowercase", function() {
- it("should preserve lowercase strings", function() {
- expect(Ext.util.Format.lowercase("lowercase string")).toBe("lowercase string");
- });
- it("should convert uppercase strings to lowercase", function() {
- expect(Ext.util.Format.lowercase("UPPERCASE STRING")).toBe("uppercase string");
- });
- it("should convert mixed lowercase/uppercase strings to lowercase", function() {
- expect(Ext.util.Format.lowercase("MIXED string")).toBe("mixed string");
- expect(Ext.util.Format.lowercase("mixed STRING")).toBe("mixed string");
- expect(Ext.util.Format.lowercase("MiXeD sTrIng")).toBe("mixed string");
- });
- it("should be null/undefined safe", function() {
- expect(Ext.util.Format.lowercase(undefined)).toBe("undefined");
- expect(Ext.util.Format.lowercase(null)).toBe("null");
- });
- it("should cast non-string values before processing", function() {
- expect(Ext.util.Format.lowercase(123)).toBe("123");
- expect(Ext.util.Format.lowercase(true)).toBe("true");
- });
- });
- describe("uppercase", function() {
- it("should preserve uppercase strings", function() {
- expect(Ext.util.Format.uppercase("UPPERCASE STRING")).toBe("UPPERCASE STRING");
- });
- it("should convert lowercase strings to uppercase", function() {
- expect(Ext.util.Format.uppercase("lowercase string")).toBe("LOWERCASE STRING");
- });
- it("should convert mixed lowercase/uppercase strings to uppercase", function() {
- expect(Ext.util.Format.uppercase("MIXED string")).toBe("MIXED STRING");
- expect(Ext.util.Format.uppercase("mixed STRING")).toBe("MIXED STRING");
- expect(Ext.util.Format.uppercase("MiXeD sTrIng")).toBe("MIXED STRING");
- });
- it("should be null/undefined safe", function() {
- expect(Ext.util.Format.uppercase(undefined)).toBe("UNDEFINED");
- expect(Ext.util.Format.uppercase(null)).toBe("NULL");
- });
- it("should cast non-string values before processing", function() {
- expect(Ext.util.Format.uppercase(123)).toBe("123");
- expect(Ext.util.Format.uppercase(true)).toBe("TRUE");
- });
- });
- describe("usMoney", function(){
- it("should format with 2 decimals, prefixed by a dollar sign", function() {
- expect(Ext.util.Format.usMoney(1234.567)).toEqual("$1,234.57");
- });
- it("should format with 2 decimals, prefixed by a negative sign, and a dollar sign", function() {
- expect(Ext.util.Format.usMoney(-1234.567)).toEqual("-$1,234.57");
- });
- it("should format with a comma as a thousand separator", function() {
- expect(Ext.util.Format.usMoney(1234567.89)).toEqual("$1,234,567.89");
- });
- });
- describe("currency in FR locale", function(){
- beforeEach(function() {
- Ext.apply(Ext.util.Format, {
- thousandSeparator: '.',
- decimalSeparator: ',',
- currencySign: '\u20ac',
- dateFormat: 'd/m/Y'
- });
- });
- afterEach(function() {
- Ext.apply(Ext.util.Format, savedFormatLocale);
- });
- it("should format with 2 decimals, prefixed by a euro sign", function() {
- expect(Ext.util.Format.currency(1234.567)).toEqual("\u20ac1.234,57");
- });
- it("should format with 2 decimals, prefixed by a negative sign, and a euro sign", function() {
- expect(Ext.util.Format.currency(-1234.567)).toEqual("-\u20ac1.234,57");
- });
- });
- describe("currency", function() {
- it("should allow 0 for a decimal value", function(){
- expect(Ext.util.Format.currency(100, '$', 0)).toBe('$100');
- });
- it("should position currency signal where specified", function() {
- expect(Ext.util.Format.currency(123.45, '$', 2)).toBe("$123.45");
- expect(Ext.util.Format.currency(123.45, '$', 2, false)).toBe("$123.45");
- expect(Ext.util.Format.currency(123.45, '$', 2, true)).toBe("123.45$");
- });
- });
- describe("number in default (US) locale", function() {
- it("should format with no decimals", function() {
- expect(Ext.util.Format.number(1, "0")).toEqual("1");
- });
- it("should format with two decimals", function() {
- expect(Ext.util.Format.number(1, "0.00")).toEqual("1.00");
- });
- it("should format+round with two decimals, and no thousand separators", function() {
- expect(Ext.util.Format.number(1234.567, "0.00")).toEqual("1234.57");
- });
- it("should format+round with two decimals, and ',' as the thousand separator", function() {
- expect(Ext.util.Format.number(1234.567, ",0.00")).toEqual("1,234.57");
- });
- it("should format+round with no decimals, and ',' as the thousand separator", function() {
- expect(Ext.util.Format.number(1234.567, ",0")).toEqual("1,235");
- });
- });
- describe("number using FR locale", function() {
- var savedFormatLocale = {
- thousandSeparator: Ext.util.Format.thousandSeparator,
- decimalSeparator: Ext.util.Format.decimalSeparator,
- currencySign: Ext.util.Format.currencySign,
- dateFormat: Ext.util.Format.dateFormat
- };
- beforeEach(function() {
- Ext.apply(Ext.util.Format, {
- thousandSeparator: '.',
- decimalSeparator: ',',
- currencySign: '\u20ac',
- dateFormat: 'd/m/Y'
- });
- });
- afterEach(function() {
- Ext.apply(Ext.util.Format, savedFormatLocale);
- });
- it("should format with no decimals", function() {
- expect(Ext.util.Format.number(1, "0")).toEqual("1");
- });
- it("should format with two decimals", function() {
- expect(Ext.util.Format.number(1, "0.00")).toEqual("1,00");
- });
- it("should format+round with two decimals, and no thousand separators", function() {
- expect(Ext.util.Format.number(1234.567, "0.00")).toEqual("1234,57");
- });
- it("should format+round with two decimals after a ',', and '.' as the thousand separator", function() {
- expect(Ext.util.Format.number(1234.567, ",0.00")).toEqual("1.234,57");
- });
- it("should format+round with no decimals, and '.' as the thousand separator", function() {
- expect(Ext.util.Format.number(1234.567, ",0")).toEqual("1.235");
- });
- });
- // In Ext4, the "/i" suffix allows you to use locale-specific separators in the format string, as opposed
- // to US/UK conventions. Output however ALWAYS follows the local settings in the Format singleton which may
- // be overridden by locale files.
- describe("number using FR locale with /i", function() {
- var savedFormatLocale = {
- thousandSeparator: Ext.util.Format.thousandSeparator,
- decimalSeparator: Ext.util.Format.decimalSeparator,
- currencySign: Ext.util.Format.currencySign,
- dateFormat: Ext.util.Format.dateFormat
- };
- // set up the FR formatting locale
- beforeEach(function() {
- Ext.apply(Ext.util.Format, {
- thousandSeparator: '.',
- decimalSeparator: ',',
- currencySign: '\u20ac',
- dateFormat: 'd/m/Y'
- });
- });
- afterEach(function() {
- Ext.apply(Ext.util.Format, savedFormatLocale);
- });
- // Demonstrate "Incorrect" use with "/i". '.' means thousand separator and ',' means decimal in FR locale.
- // Read carefully. In the formatting strings below, '.' is taken to mean thousand separator, and
- // ',' is taken to mean decimal separator
- it("should format with no decimals", function() {
- expect(Ext.util.Format.number(1, "0.00/i")).toEqual("1");
- });
- it("should format+round with no decimals, and '.' as thousand separator", function() {
-