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