3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
15 describe("Ext.String", function() {
17 describe("ellipsis", function() {
18 var ellipsis = Ext.String.ellipsis,
19 shortString = "A short string",
20 longString = "A somewhat longer string";
22 it("should keep short strings intact", function() {
23 expect(ellipsis(shortString, 100)).toEqual(shortString);
26 it("should truncate a longer string", function() {
27 expect(ellipsis(longString, 10)).toEqual("A somew...");
30 describe("word break", function() {
31 var longStringWithDot = "www.sencha.com",
32 longStringWithExclamationMark = "Yeah!Yeah!Yeah!",
33 longStringWithQuestionMark = "Who?When?What?";
35 it("should find a word break on ' '", function() {
36 expect(ellipsis(longString, 10, true)).toEqual("A...");
39 it("should be able to break on '.'", function() {
40 expect(ellipsis(longStringWithDot, 9, true)).toEqual("www...");
43 it("should be able to break on '!'", function() {
44 expect(ellipsis(longStringWithExclamationMark, 9, true)).toEqual("Yeah...");
47 it("should be able to break on '?'", function() {
48 expect(ellipsis(longStringWithQuestionMark, 8, true)).toEqual("Who...");
53 describe("escapeRegex", function() {
55 escapeRegex = Ext.String.escapeRegex;
57 it("should escape minus", function() {
60 expect(escapeRegex(str)).toEqual("12 \\- 175");
63 it("should escape dot", function() {
64 str = "Brian is in the kitchen.";
66 expect(escapeRegex(str)).toEqual("Brian is in the kitchen\\.");
69 it("should escape asterisk", function() {
72 expect(escapeRegex(str)).toEqual("12 \\* 175");
75 it("should escape plus", function() {
78 expect(escapeRegex(str)).toEqual("12 \\+ 175");
81 it("should escape question mark", function() {
84 expect(escapeRegex(str)).toEqual("What else \\?");
87 it("should escape caret", function() {
90 expect(escapeRegex(str)).toEqual("\\^\\^");
93 it("should escape dollar", function() {
96 expect(escapeRegex(str)).toEqual("500\\$");
99 it("should escape open brace", function() {
100 str = "something{stupid";
102 expect(escapeRegex(str)).toEqual("something\\{stupid");
105 it("should escape close brace", function() {
106 str = "something}stupid";
108 expect(escapeRegex(str)).toEqual("something\\}stupid");
111 it("should escape open bracket", function() {
112 str = "something[stupid";
114 expect(escapeRegex(str)).toEqual("something\\[stupid");
117 it("should escape close bracket", function() {
118 str = "something]stupid";
120 expect(escapeRegex(str)).toEqual("something\\]stupid");
123 it("should escape open parenthesis", function() {
124 str = "something(stupid";
126 expect(escapeRegex(str)).toEqual("something\\(stupid");
129 it("should escape close parenthesis", function() {
130 str = "something)stupid";
132 expect(escapeRegex(str)).toEqual("something\\)stupid");
135 it("should escape vertival bar", function() {
136 str = "something|stupid";
138 expect(escapeRegex(str)).toEqual("something\\|stupid");
141 it("should escape forward slash", function() {
142 str = "something/stupid";
144 expect(escapeRegex(str)).toEqual("something\\/stupid");
147 it("should escape backslash", function() {
148 str = "something\\stupid";
150 expect(escapeRegex(str)).toEqual("something\\\\stupid");
154 describe("htmlEncode", function() {
155 var htmlEncode = Ext.String.htmlEncode,
158 it("should replace ampersands", function() {
159 str = "Fish & Chips";
161 expect(htmlEncode(str)).toEqual("Fish & Chips");
164 it("should replace less than", function() {
165 str = "Fish > Chips";
167 expect(htmlEncode(str)).toEqual("Fish > Chips");
170 it("should replace greater than", function() {
171 str = "Fish < Chips";
173 expect(htmlEncode(str)).toEqual("Fish < Chips");
176 it("should replace double quote", function() {
177 str = 'Fish " Chips';
179 expect(htmlEncode(str)).toEqual("Fish " Chips");
183 describe("htmlDecode", function() {
184 var htmlDecode = Ext.String.htmlDecode,
187 it("should replace ampersands", function() {
188 str = "Fish & Chips";
190 expect(htmlDecode(str)).toEqual("Fish & Chips");
193 it("should replace less than", function() {
194 str = "Fish > Chips";
196 expect(htmlDecode(str)).toEqual("Fish > Chips");
199 it("should replace greater than", function() {
200 str = "Fish < Chips";
202 expect(htmlDecode(str)).toEqual("Fish < Chips");
205 it("should replace double quote", function() {
206 str = 'Fish " Chips';
208 expect(htmlDecode(str)).toEqual('Fish " Chips');
212 describe("escaping", function() {
213 var escape = Ext.String.escape;
215 it("should leave an empty string alone", function() {
216 expect(escape('')).toEqual('');
219 it("should leave a non-empty string without escapable characters alone", function() {
220 expect(escape('Ed')).toEqual('Ed');
223 it("should correctly escape a double backslash", function() {
224 expect(escape("\\")).toEqual("\\\\");
227 it("should correctly escape a single backslash", function() {
228 expect(escape('\'')).toEqual('\\\'');
231 it("should correctly escape a mixture of escape and non-escape characters", function() {
232 expect(escape('\'foo\\')).toEqual('\\\'foo\\\\');
236 describe("formatting", function() {
237 var format = Ext.String.format;
239 it("should leave a string without format parameters alone", function() {
240 expect(format('Ed')).toEqual('Ed');
243 it("should ignore arguments that don't map to format params", function() {
244 expect(format("{0} person", 1, 123)).toEqual("1 person");
247 it("should accept several format parameters", function() {
248 expect(format("{0} person {1}", 1, 'came')).toEqual('1 person came');
252 describe("leftPad", function() {
253 var leftPad = Ext.String.leftPad;
255 it("should pad the left side of an empty string", function() {
256 expect(leftPad("", 5)).toEqual(" ");
259 it("should pad the left side of a non-empty string", function() {
260 expect(leftPad("Ed", 5)).toEqual(" Ed");
263 it("should not pad a string where the character count already exceeds the pad count", function() {
264 expect(leftPad("Abraham", 5)).toEqual("Abraham");
267 it("should allow a custom padding character", function() {
268 expect(leftPad("Ed", 5, "0")).toEqual("000Ed");
272 describe("when toggling between two values", function() {
273 var toggle = Ext.String.toggle;
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");
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");
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");
288 describe("trimming", function() {
289 var trim = Ext.String.trim;
291 it("should not modify an empty string", function() {
292 expect(trim("")).toEqual("");
295 it("should not modify a string with no whitespace", function() {
296 expect(trim("Abe")).toEqual("Abe");
299 it("should trim a whitespace-only string", function() {
300 expect(trim(" ")).toEqual("");
303 it("should trim leading whitespace", function() {
304 expect(trim(" Ed")).toEqual("Ed");
307 it("should trim trailing whitespace", function() {
308 expect(trim("Ed ")).toEqual("Ed");
311 it("should trim leading and trailing whitespace", function() {
312 expect(trim(" Ed ")).toEqual("Ed");
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");
320 it("should trim tabs", function() {
321 expect(trim("\tEd")).toEqual("Ed");
324 it("should trim a mixture of tabs and whitespace", function() {
325 expect(trim("\tEd ")).toEqual("Ed");
329 describe("urlAppend", function(){
330 var urlAppend = Ext.String.urlAppend;
332 it("should leave the string untouched if the second argument is empty", function(){
333 expect(urlAppend('sencha.com')).toEqual('sencha.com');
336 it("should append a ? if one doesn't exist", function(){
337 expect(urlAppend('sencha.com', 'foo=bar')).toEqual('sencha.com?foo=bar');
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');
345 describe("capitalize", function(){
346 var capitalize = Ext.String.capitalize;
348 it("should handle an empty string", function(){
349 expect(capitalize('')).toEqual('');
352 it("should capitalize the first letter of the string", function(){
353 expect(capitalize('open')).toEqual('Open');
356 it("should leave the first letter capitalized if it is already capitalized", function(){
357 expect(capitalize('Closed')).toEqual('Closed');
360 it("should capitalize a single letter", function(){
361 expect(capitalize('a')).toEqual('A');
364 it("should not capitalize even when spaces are included", function(){
365 expect(capitalize('this is a sentence')).toEqual('This is a sentence');