Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / core / test / unit / spec / lang / String.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.String", function() {
16
17     describe("ellipsis", function() {
18         var ellipsis = Ext.String.ellipsis,
19             shortString = "A short string",
20             longString  = "A somewhat longer string";
21         
22         it("should keep short strings intact", function() {
23             expect(ellipsis(shortString, 100)).toEqual(shortString);
24         });
25         
26         it("should truncate a longer string", function() {
27             expect(ellipsis(longString, 10)).toEqual("A somew...");
28         });
29         
30         describe("word break", function() {
31             var longStringWithDot  = "www.sencha.com",
32                 longStringWithExclamationMark = "Yeah!Yeah!Yeah!",
33                 longStringWithQuestionMark = "Who?When?What?";
34                            
35             it("should find a word break on ' '", function() {
36                 expect(ellipsis(longString, 10, true)).toEqual("A...");
37             });      
38             
39             it("should be able to break on '.'", function() {
40                 expect(ellipsis(longStringWithDot, 9, true)).toEqual("www...");
41             });  
42             
43             it("should be able to break on '!'", function() {
44                 expect(ellipsis(longStringWithExclamationMark, 9, true)).toEqual("Yeah...");
45             }); 
46             
47             it("should be able to break on '?'", function() {
48                 expect(ellipsis(longStringWithQuestionMark, 8, true)).toEqual("Who...");
49             });       
50         });
51     });
52     
53     describe("escapeRegex", function() {
54         var str,
55             escapeRegex = Ext.String.escapeRegex;
56         
57         it("should escape minus", function() {
58             str = "12 - 175";
59             
60             expect(escapeRegex(str)).toEqual("12 \\- 175");
61         });
62         
63         it("should escape dot", function() {
64             str = "Brian is in the kitchen.";
65             
66             expect(escapeRegex(str)).toEqual("Brian is in the kitchen\\.");
67         });
68         
69         it("should escape asterisk", function() {
70             str = "12 * 175";
71             
72             expect(escapeRegex(str)).toEqual("12 \\* 175");
73         });
74         
75         it("should escape plus", function() {
76             str = "12 + 175";
77             
78             expect(escapeRegex(str)).toEqual("12 \\+ 175");
79         });
80         
81         it("should escape question mark", function() {
82             str = "What else ?";
83             
84             expect(escapeRegex(str)).toEqual("What else \\?");
85         });
86         
87         it("should escape caret", function() {
88             str = "^^";
89             
90             expect(escapeRegex(str)).toEqual("\\^\\^");
91         });
92         
93         it("should escape dollar", function() {
94             str = "500$";
95             
96             expect(escapeRegex(str)).toEqual("500\\$");
97         });
98         
99         it("should escape open brace", function() {
100             str = "something{stupid";
101             
102             expect(escapeRegex(str)).toEqual("something\\{stupid");
103         });
104         
105         it("should escape close brace", function() {
106             str = "something}stupid";
107             
108             expect(escapeRegex(str)).toEqual("something\\}stupid");
109         });
110         
111         it("should escape open bracket", function() {
112             str = "something[stupid";
113             
114             expect(escapeRegex(str)).toEqual("something\\[stupid");
115         });
116         
117         it("should escape close bracket", function() {
118             str = "something]stupid";
119             
120             expect(escapeRegex(str)).toEqual("something\\]stupid");
121         });
122         
123         it("should escape open parenthesis", function() {
124             str = "something(stupid";
125             
126             expect(escapeRegex(str)).toEqual("something\\(stupid");
127         });
128         
129         it("should escape close parenthesis", function() {
130             str = "something)stupid";
131             
132             expect(escapeRegex(str)).toEqual("something\\)stupid");
133         });
134         
135         it("should escape vertival bar", function() {
136             str = "something|stupid";
137             
138             expect(escapeRegex(str)).toEqual("something\\|stupid");
139         });
140         
141         it("should escape forward slash", function() {
142             str = "something/stupid";
143             
144             expect(escapeRegex(str)).toEqual("something\\/stupid");
145         });
146         
147         it("should escape backslash", function() {
148             str = "something\\stupid";
149             
150             expect(escapeRegex(str)).toEqual("something\\\\stupid");
151         });
152     });
153     
154     describe("htmlEncode", function() {
155         var htmlEncode = Ext.String.htmlEncode,
156             str;
157         
158         it("should replace ampersands", function() {
159             str = "Fish & Chips";
160             
161             expect(htmlEncode(str)).toEqual("Fish & Chips");
162         });
163         
164         it("should replace less than", function() {
165             str = "Fish > Chips";
166             
167             expect(htmlEncode(str)).toEqual("Fish > Chips");
168         });
169         
170         it("should replace greater than", function() {
171             str = "Fish < Chips";
172             
173             expect(htmlEncode(str)).toEqual("Fish &lt; Chips");
174         });
175         
176         it("should replace double quote", function() {
177             str = 'Fish " Chips';
178             
179             expect(htmlEncode(str)).toEqual("Fish &quot; Chips");
180         });
181     });
182     
183     describe("htmlDecode", function() {
184         var htmlDecode = Ext.String.htmlDecode,
185             str;
186         
187         it("should replace ampersands", function() {
188             str = "Fish &amp; Chips";
189             
190             expect(htmlDecode(str)).toEqual("Fish & Chips");
191         });
192         
193         it("should replace less than", function() {
194             str = "Fish &gt; Chips";
195             
196             expect(htmlDecode(str)).toEqual("Fish > Chips");
197         });
198         
199         it("should replace greater than", function() {
200             str = "Fish &lt; Chips";
201             
202             expect(htmlDecode(str)).toEqual("Fish < Chips");
203         });
204         
205         it("should replace double quote", function() {
206             str = 'Fish &quot; Chips';
207             
208             expect(htmlDecode(str)).toEqual('Fish " Chips');
209         });
210     });
211     
212     describe("escaping", function() {
213         var escape = Ext.String.escape;
214         
215         it("should leave an empty string alone", function() {
216             expect(escape('')).toEqual('');
217         });
218         
219         it("should leave a non-empty string without escapable characters alone", function() {
220             expect(escape('Ed')).toEqual('Ed');
221         });
222         
223         it("should correctly escape a double backslash", function() {
224             expect(escape("\\")).toEqual("\\\\");
225         });
226         
227         it("should correctly escape a single backslash", function() {
228             expect(escape('\'')).toEqual('\\\'');
229         });
230         
231         it("should correctly escape a mixture of escape and non-escape characters", function() {
232             expect(escape('\'foo\\')).toEqual('\\\'foo\\\\');
233         });
234     });
235     
236     describe("formatting", function() {
237         var format = Ext.String.format;
238         
239         it("should leave a string without format parameters alone", function() {
240             expect(format('Ed')).toEqual('Ed');
241         });
242         
243         it("should ignore arguments that don't map to format params", function() {
244             expect(format("{0} person", 1, 123)).toEqual("1 person");
245         });
246         
247         it("should accept several format parameters", function() {
248             expect(format("{0} person {1}", 1, 'came')).toEqual('1 person came');
249         });
250     });
251     
252     describe("leftPad", function() {
253         var leftPad = Ext.String.leftPad;
254         
255         it("should pad the left side of an empty string", function() {
256             expect(leftPad("", 5)).toEqual("     ");
257         });
258         
259         it("should pad the left side of a non-empty string", function() {
260             expect(leftPad("Ed", 5)).toEqual("   Ed");
261         });
262         
263         it("should not pad a string where the character count already exceeds the pad count", function() {
264             expect(leftPad("Abraham", 5)).toEqual("Abraham");
265         });
266         
267         it("should allow a custom padding character", function() {
268             expect(leftPad("Ed", 5, "0")).toEqual("000Ed");
269         });
270     });
271     
272     describe("when toggling between two values", function() {
273         var toggle = Ext.String.toggle;
274         
275         it("should use the first toggle value if the string is not already one of the toggle values", function() {
276             expect(toggle("Aaron", "Ed", "Abe")).toEqual("Ed");
277         });
278         
279         it("should toggle to the second toggle value if the string is currently the first", function() {
280             expect(toggle("Ed", "Ed", "Abe")).toEqual("Abe");
281         });
282         
283         it("should toggle to the first toggle value if the string is currently the second", function() {
284             expect(toggle("Abe", "Ed", "Abe")).toEqual("Ed");
285         });
286     });
287     
288     describe("trimming", function() {
289         var trim = Ext.String.trim;
290         
291         it("should not modify an empty string", function() {
292             expect(trim("")).toEqual("");
293         });
294         
295         it("should not modify a string with no whitespace", function() {
296             expect(trim("Abe")).toEqual("Abe");
297         });
298         
299         it("should trim a whitespace-only string", function() {
300             expect(trim("     ")).toEqual("");
301         });
302         
303         it("should trim leading whitespace", function() {
304             expect(trim("  Ed")).toEqual("Ed");
305         });
306         
307         it("should trim trailing whitespace", function() {
308             expect(trim("Ed   ")).toEqual("Ed");
309         });
310         
311         it("should trim leading and trailing whitespace", function() {
312             expect(trim("   Ed  ")).toEqual("Ed");
313         });
314         
315         it("should not trim whitespace between words", function() {
316             expect(trim("Fish and chips")).toEqual("Fish and chips");
317             expect(trim("   Fish and chips  ")).toEqual("Fish and chips");
318         });
319         
320         it("should trim tabs", function() {
321             expect(trim("\tEd")).toEqual("Ed");
322         });
323         
324         it("should trim a mixture of tabs and whitespace", function() {
325             expect(trim("\tEd   ")).toEqual("Ed");
326         });
327     });
328     
329     describe("urlAppend", function(){
330         var urlAppend = Ext.String.urlAppend;
331         
332         it("should leave the string untouched if the second argument is empty", function(){
333             expect(urlAppend('sencha.com')).toEqual('sencha.com');    
334         });
335         
336         it("should append a ? if one doesn't exist", function(){
337             expect(urlAppend('sencha.com', 'foo=bar')).toEqual('sencha.com?foo=bar');
338         });
339         
340         it("should append any new values with & if a ? exists", function(){
341             expect(urlAppend('sencha.com?x=y', 'foo=bar')).toEqual('sencha.com?x=y&foo=bar');
342         });
343     });
344     
345     describe("capitalize", function(){
346         var capitalize = Ext.String.capitalize;
347         
348         it("should handle an empty string", function(){
349             expect(capitalize('')).toEqual('');
350         });
351         
352         it("should capitalize the first letter of the string", function(){
353             expect(capitalize('open')).toEqual('Open');
354         });
355         
356         it("should leave the first letter capitalized if it is already capitalized", function(){
357             expect(capitalize('Closed')).toEqual('Closed');
358         });
359         
360         it("should capitalize a single letter", function(){
361             expect(capitalize('a')).toEqual('A');
362         });
363         
364         it("should not capitalize even when spaces are included", function(){
365             expect(capitalize('this is a sentence')).toEqual('This is a sentence');
366         });
367     });
368 });
369